mirror of https://github.com/apache/openjpa.git
ClassMappingInfo.addUnique() accepts a Unique instance rather than an array of column names
git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@495800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b7c7a47aac
commit
218a000b84
|
@ -322,22 +322,12 @@ public class ClassMappingInfo
|
|||
_uniques = new ArrayList(cinfo._uniques);
|
||||
}
|
||||
|
||||
public void addUnique(String name, String[] columnNames) {
|
||||
if (columnNames == null || columnNames.length == 0)
|
||||
public void addUnique(Unique unique) {
|
||||
if (unique == null)
|
||||
return;
|
||||
if (_uniques == null)
|
||||
_uniques = new ArrayList();
|
||||
Unique uniqueConstraint = new Unique();
|
||||
uniqueConstraint.setName(name);
|
||||
for (int i=0; i<columnNames.length; i++) {
|
||||
if (StringUtils.isEmpty(columnNames[i]))
|
||||
throw new UserException(_loc.get("empty-unique-column",
|
||||
getClassName()));
|
||||
Column column = new Column();
|
||||
column.setName(columnNames[i]);
|
||||
uniqueConstraint.addColumn(column);
|
||||
}
|
||||
_uniques.add(uniqueConstraint);
|
||||
_uniques.add(unique);
|
||||
}
|
||||
|
||||
public Unique[] getUniques(ClassMapping cm, boolean adapt) {
|
||||
|
|
|
@ -403,7 +403,4 @@ num-cols-path: Result path "{2}" in result type "{1}" of mapping "{0}" \
|
|||
attempts to map a field that does not have exactly 1 column.
|
||||
missing-unique-column: A unique constraint specified in mapping of class "{0}" \
|
||||
to table "{1}" includes a column "{2}". However, the column does not \
|
||||
exist in "{1}" table.
|
||||
empty-unique-column: A unique constraint specified in mapping of class "{0}" \
|
||||
includes an empty column "{2}".
|
||||
|
||||
exist in "{1}" table.
|
||||
|
|
|
@ -66,6 +66,7 @@ import org.apache.openjpa.jdbc.meta.strats.FlatClassStrategy;
|
|||
import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
|
||||
import org.apache.openjpa.jdbc.meta.strats.VerticalClassStrategy;
|
||||
import org.apache.openjpa.jdbc.schema.Column;
|
||||
import org.apache.openjpa.jdbc.schema.Unique;
|
||||
import org.apache.openjpa.lib.log.Log;
|
||||
import org.apache.openjpa.lib.util.Localizer;
|
||||
import org.apache.openjpa.meta.ClassMetaData;
|
||||
|
@ -76,6 +77,7 @@ import static org.apache.openjpa.persistence.jdbc.MappingTag.*;
|
|||
import org.apache.openjpa.util.InternalException;
|
||||
import org.apache.openjpa.util.MetaDataException;
|
||||
import org.apache.openjpa.util.UnsupportedException;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
|
||||
/**
|
||||
* Persistence annotation mapping parser.
|
||||
|
@ -460,9 +462,9 @@ public class AnnotationPersistenceMappingParser
|
|||
if (tableName != null)
|
||||
cm.getMappingInfo().setTableName(tableName);
|
||||
|
||||
for (UniqueConstraint unique:table.uniqueConstraints()) {
|
||||
((ClassMappingInfo)cm.getMappingInfo())
|
||||
.addUnique(null, unique.columnNames());
|
||||
for (UniqueConstraint uniqueConstraint:table.uniqueConstraints()) {
|
||||
Unique unique = newUnique(cm, null, uniqueConstraint.columnNames());
|
||||
cm.getMappingInfo().addUnique(unique);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -682,14 +684,15 @@ public class AnnotationPersistenceMappingParser
|
|||
/**
|
||||
* Set unique data on the given mapping info.
|
||||
*/
|
||||
private void parseUnique(FieldMapping fm, Unique anno) {
|
||||
private void parseUnique(FieldMapping fm,
|
||||
org.apache.openjpa.persistence.jdbc.Unique anno) {
|
||||
ValueMappingInfo info = fm.getValueInfo();
|
||||
if (!anno.enabled()) {
|
||||
info.setCanUnique(false);
|
||||
return;
|
||||
}
|
||||
|
||||
org.apache.openjpa.jdbc.schema.Unique unq =
|
||||
org.apache.openjpa.jdbc.schema.Unique unq =
|
||||
new org.apache.openjpa.jdbc.schema.Unique();
|
||||
if (!StringUtils.isEmpty(anno.name()))
|
||||
unq.setName(anno.name());
|
||||
|
@ -879,7 +882,8 @@ public class AnnotationPersistenceMappingParser
|
|||
fm.getValueInfo().setStrategy(((Strategy) anno).value());
|
||||
break;
|
||||
case UNIQUE:
|
||||
parseUnique(fm, (Unique) anno);
|
||||
parseUnique(fm,
|
||||
(org.apache.openjpa.persistence.jdbc.Unique) anno);
|
||||
break;
|
||||
case X_JOIN_COL:
|
||||
parseXJoinColumns(fm, fm.getValueInfo(), true,
|
||||
|
@ -1325,4 +1329,21 @@ public class AnnotationPersistenceMappingParser
|
|||
col.setFlag (Column.FLAG_UNUPDATABLE, !join.updatable ());
|
||||
return col;
|
||||
}
|
||||
|
||||
private static Unique newUnique(ClassMapping cm, String name,
|
||||
String[] columnNames) {
|
||||
if (columnNames == null || columnNames.length == 0)
|
||||
return null;
|
||||
Unique uniqueConstraint = new Unique();
|
||||
uniqueConstraint.setName(name);
|
||||
for (int i=0; i<columnNames.length; i++) {
|
||||
if (StringUtils.isEmpty(columnNames[i]))
|
||||
throw new UserException(_loc.get("empty-unique-column",
|
||||
Arrays.toString(columnNames), cm));
|
||||
Column column = new Column();
|
||||
column.setName(columnNames[i]);
|
||||
uniqueConstraint.addColumn(column);
|
||||
}
|
||||
return uniqueConstraint;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,3 +43,5 @@ second-version: Version property "{0}" cannot map to a secondary table column. \
|
|||
Version columns must always be in the primary table of the class.
|
||||
not-embedded: Attempt to declare mapping overrides on non-embedded field "{0}".
|
||||
no-gen-table: No generated table found at "{0}".
|
||||
empty-unique-column: A unique constraint "{0}" specified in mapping of class \
|
||||
"{1}" includes an empty column.
|
Loading…
Reference in New Issue