use of javax.persistence.UniqueConstraint in project hibernate-orm by hibernate.
the class AnnotationBinder method bindJoinedTableAssociation.
//TODO move that to collection binder?
private static void bindJoinedTableAssociation(XProperty property, MetadataBuildingContext buildingContext, EntityBinder entityBinder, CollectionBinder collectionBinder, PropertyHolder propertyHolder, PropertyData inferredData, String mappedBy) {
TableBinder associationTableBinder = new TableBinder();
JoinColumn[] annJoins;
JoinColumn[] annInverseJoins;
JoinTable assocTable = propertyHolder.getJoinTable(property);
CollectionTable collectionTable = property.getAnnotation(CollectionTable.class);
if (assocTable != null || collectionTable != null) {
final String catalog;
final String schema;
final String tableName;
final UniqueConstraint[] uniqueConstraints;
final JoinColumn[] joins;
final JoinColumn[] inverseJoins;
final javax.persistence.Index[] jpaIndexes;
//JPA 2 has priority
if (collectionTable != null) {
catalog = collectionTable.catalog();
schema = collectionTable.schema();
tableName = collectionTable.name();
uniqueConstraints = collectionTable.uniqueConstraints();
joins = collectionTable.joinColumns();
inverseJoins = null;
jpaIndexes = collectionTable.indexes();
} else {
catalog = assocTable.catalog();
schema = assocTable.schema();
tableName = assocTable.name();
uniqueConstraints = assocTable.uniqueConstraints();
joins = assocTable.joinColumns();
inverseJoins = assocTable.inverseJoinColumns();
jpaIndexes = assocTable.indexes();
}
collectionBinder.setExplicitAssociationTable(true);
if (jpaIndexes != null && jpaIndexes.length > 0) {
associationTableBinder.setJpaIndex(jpaIndexes);
}
if (!BinderHelper.isEmptyAnnotationValue(schema)) {
associationTableBinder.setSchema(schema);
}
if (!BinderHelper.isEmptyAnnotationValue(catalog)) {
associationTableBinder.setCatalog(catalog);
}
if (!BinderHelper.isEmptyAnnotationValue(tableName)) {
associationTableBinder.setName(tableName);
}
associationTableBinder.setUniqueConstraints(uniqueConstraints);
associationTableBinder.setJpaIndex(jpaIndexes);
//set check constaint in the second pass
annJoins = joins.length == 0 ? null : joins;
annInverseJoins = inverseJoins == null || inverseJoins.length == 0 ? null : inverseJoins;
} else {
annJoins = null;
annInverseJoins = null;
}
Ejb3JoinColumn[] joinColumns = Ejb3JoinColumn.buildJoinTableJoinColumns(annJoins, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappedBy, buildingContext);
Ejb3JoinColumn[] inverseJoinColumns = Ejb3JoinColumn.buildJoinTableJoinColumns(annInverseJoins, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappedBy, buildingContext);
associationTableBinder.setBuildingContext(buildingContext);
collectionBinder.setTableBinder(associationTableBinder);
collectionBinder.setJoinColumns(joinColumns);
collectionBinder.setInverseJoinColumns(inverseJoinColumns);
}
use of javax.persistence.UniqueConstraint in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method buildUniqueConstraints.
private static void buildUniqueConstraints(AnnotationDescriptor annotation, Element element) {
List uniqueConstraintElementList = element.elements("unique-constraint");
UniqueConstraint[] uniqueConstraints = new UniqueConstraint[uniqueConstraintElementList.size()];
int ucIndex = 0;
Iterator ucIt = uniqueConstraintElementList.listIterator();
while (ucIt.hasNext()) {
Element subelement = (Element) ucIt.next();
List<Element> columnNamesElements = subelement.elements("column-name");
String[] columnNames = new String[columnNamesElements.size()];
int columnNameIndex = 0;
Iterator it = columnNamesElements.listIterator();
while (it.hasNext()) {
Element columnNameElt = (Element) it.next();
columnNames[columnNameIndex++] = columnNameElt.getTextTrim();
}
AnnotationDescriptor ucAnn = new AnnotationDescriptor(UniqueConstraint.class);
copyStringAttribute(ucAnn, subelement, "name", false);
ucAnn.setValue("columnNames", columnNames);
uniqueConstraints[ucIndex++] = AnnotationFactory.create(ucAnn);
}
annotation.setValue("uniqueConstraints", uniqueConstraints);
}
use of javax.persistence.UniqueConstraint in project hibernate-orm by hibernate.
the class Ejb3XmlElementCollectionTest method testMultipleAssociationOverridesJoinColumns.
@Test
public void testMultipleAssociationOverridesJoinColumns() throws Exception {
reader = getReader(Entity3.class, "field1", "element-collection.orm25.xml");
assertAnnotationPresent(ElementCollection.class);
assertAnnotationNotPresent(AssociationOverride.class);
assertAnnotationPresent(AssociationOverrides.class);
AssociationOverrides overridesAnno = reader.getAnnotation(AssociationOverrides.class);
AssociationOverride[] overrides = overridesAnno.value();
assertEquals(2, overrides.length);
//First, an association using join table
assertEquals("association1", overrides[0].name());
assertEquals(0, overrides[0].joinColumns().length);
JoinTable joinTableAnno = overrides[0].joinTable();
assertEquals("catalog1", joinTableAnno.catalog());
assertEquals("table1", joinTableAnno.name());
assertEquals("schema1", joinTableAnno.schema());
//JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals(2, joinColumns.length);
assertEquals("", joinColumns[0].name());
assertEquals("", joinColumns[0].referencedColumnName());
assertEquals("", joinColumns[0].table());
assertEquals("", joinColumns[0].columnDefinition());
assertTrue(joinColumns[0].insertable());
assertTrue(joinColumns[0].updatable());
assertTrue(joinColumns[0].nullable());
assertFalse(joinColumns[0].unique());
assertEquals("col1", joinColumns[1].name());
assertEquals("col2", joinColumns[1].referencedColumnName());
assertEquals("table2", joinColumns[1].table());
assertEquals("int", joinColumns[1].columnDefinition());
assertFalse(joinColumns[1].insertable());
assertFalse(joinColumns[1].updatable());
assertFalse(joinColumns[1].nullable());
assertTrue(joinColumns[1].unique());
//InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals(2, inverseJoinColumns.length);
assertEquals("", inverseJoinColumns[0].name());
assertEquals("", inverseJoinColumns[0].referencedColumnName());
assertEquals("", inverseJoinColumns[0].table());
assertEquals("", inverseJoinColumns[0].columnDefinition());
assertTrue(inverseJoinColumns[0].insertable());
assertTrue(inverseJoinColumns[0].updatable());
assertTrue(inverseJoinColumns[0].nullable());
assertFalse(inverseJoinColumns[0].unique());
assertEquals("col3", inverseJoinColumns[1].name());
assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
assertEquals("table3", inverseJoinColumns[1].table());
assertEquals("int", inverseJoinColumns[1].columnDefinition());
assertFalse(inverseJoinColumns[1].insertable());
assertFalse(inverseJoinColumns[1].updatable());
assertFalse(inverseJoinColumns[1].nullable());
assertTrue(inverseJoinColumns[1].unique());
//UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
assertEquals(2, uniqueConstraints.length);
assertEquals("", uniqueConstraints[0].name());
assertEquals(1, uniqueConstraints[0].columnNames().length);
assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
assertEquals("uq1", uniqueConstraints[1].name());
assertEquals(2, uniqueConstraints[1].columnNames().length);
assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
//Second, an association using join columns
assertEquals("association2", overrides[1].name());
//JoinColumns
joinColumns = overrides[1].joinColumns();
assertEquals(2, joinColumns.length);
assertEquals("", joinColumns[0].name());
assertEquals("", joinColumns[0].referencedColumnName());
assertEquals("", joinColumns[0].table());
assertEquals("", joinColumns[0].columnDefinition());
assertTrue(joinColumns[0].insertable());
assertTrue(joinColumns[0].updatable());
assertTrue(joinColumns[0].nullable());
assertFalse(joinColumns[0].unique());
assertEquals("col8", joinColumns[1].name());
assertEquals("col9", joinColumns[1].referencedColumnName());
assertEquals("table4", joinColumns[1].table());
assertEquals("int", joinColumns[1].columnDefinition());
assertFalse(joinColumns[1].insertable());
assertFalse(joinColumns[1].updatable());
assertFalse(joinColumns[1].nullable());
assertTrue(joinColumns[1].unique());
}
use of javax.persistence.UniqueConstraint in project hibernate-orm by hibernate.
the class Ejb3XmlOneToOneTest method testJoinTableAllChildren.
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader(Entity1.class, "field1", "one-to-one.orm7.xml");
assertAnnotationPresent(OneToOne.class);
assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
assertAnnotationPresent(JoinTable.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinColumn.class);
JoinTable joinTableAnno = reader.getAnnotation(JoinTable.class);
assertEquals("cat1", joinTableAnno.catalog());
assertEquals("table1", joinTableAnno.name());
assertEquals("schema1", joinTableAnno.schema());
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals(2, joinColumns.length);
assertEquals("", joinColumns[0].name());
assertEquals("", joinColumns[0].referencedColumnName());
assertEquals("", joinColumns[0].table());
assertEquals("", joinColumns[0].columnDefinition());
assertTrue(joinColumns[0].insertable());
assertTrue(joinColumns[0].updatable());
assertTrue(joinColumns[0].nullable());
assertFalse(joinColumns[0].unique());
assertEquals("col1", joinColumns[1].name());
assertEquals("col2", joinColumns[1].referencedColumnName());
assertEquals("table2", joinColumns[1].table());
assertEquals("int", joinColumns[1].columnDefinition());
assertFalse(joinColumns[1].insertable());
assertFalse(joinColumns[1].updatable());
assertFalse(joinColumns[1].nullable());
assertTrue(joinColumns[1].unique());
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals(2, inverseJoinColumns.length);
assertEquals("", inverseJoinColumns[0].name());
assertEquals("", inverseJoinColumns[0].referencedColumnName());
assertEquals("", inverseJoinColumns[0].table());
assertEquals("", inverseJoinColumns[0].columnDefinition());
assertTrue(inverseJoinColumns[0].insertable());
assertTrue(inverseJoinColumns[0].updatable());
assertTrue(inverseJoinColumns[0].nullable());
assertFalse(inverseJoinColumns[0].unique());
assertEquals("col3", inverseJoinColumns[1].name());
assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
assertEquals("table3", inverseJoinColumns[1].table());
assertEquals("int", inverseJoinColumns[1].columnDefinition());
assertFalse(inverseJoinColumns[1].insertable());
assertFalse(inverseJoinColumns[1].updatable());
assertFalse(inverseJoinColumns[1].nullable());
assertTrue(inverseJoinColumns[1].unique());
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
assertEquals(2, uniqueConstraints.length);
assertEquals("", uniqueConstraints[0].name());
assertEquals(1, uniqueConstraints[0].columnNames().length);
assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
assertEquals("uq1", uniqueConstraints[1].name());
assertEquals(2, uniqueConstraints[1].columnNames().length);
assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
}
use of javax.persistence.UniqueConstraint in project hibernate-orm by hibernate.
the class Ejb3XmlOneToManyTest method testJoinTableAllChildren.
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader(Entity2.class, "field1", "one-to-many.orm17.xml");
assertAnnotationPresent(OneToMany.class);
assertAnnotationPresent(JoinTable.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinColumn.class);
JoinTable joinTableAnno = reader.getAnnotation(JoinTable.class);
assertEquals("cat1", joinTableAnno.catalog());
assertEquals("table1", joinTableAnno.name());
assertEquals("schema1", joinTableAnno.schema());
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals(2, joinColumns.length);
assertEquals("", joinColumns[0].name());
assertEquals("", joinColumns[0].referencedColumnName());
assertEquals("", joinColumns[0].table());
assertEquals("", joinColumns[0].columnDefinition());
assertTrue(joinColumns[0].insertable());
assertTrue(joinColumns[0].updatable());
assertTrue(joinColumns[0].nullable());
assertFalse(joinColumns[0].unique());
assertEquals("col1", joinColumns[1].name());
assertEquals("col2", joinColumns[1].referencedColumnName());
assertEquals("table2", joinColumns[1].table());
assertEquals("int", joinColumns[1].columnDefinition());
assertFalse(joinColumns[1].insertable());
assertFalse(joinColumns[1].updatable());
assertFalse(joinColumns[1].nullable());
assertTrue(joinColumns[1].unique());
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals(2, inverseJoinColumns.length);
assertEquals("", inverseJoinColumns[0].name());
assertEquals("", inverseJoinColumns[0].referencedColumnName());
assertEquals("", inverseJoinColumns[0].table());
assertEquals("", inverseJoinColumns[0].columnDefinition());
assertTrue(inverseJoinColumns[0].insertable());
assertTrue(inverseJoinColumns[0].updatable());
assertTrue(inverseJoinColumns[0].nullable());
assertFalse(inverseJoinColumns[0].unique());
assertEquals("col3", inverseJoinColumns[1].name());
assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
assertEquals("table3", inverseJoinColumns[1].table());
assertEquals("int", inverseJoinColumns[1].columnDefinition());
assertFalse(inverseJoinColumns[1].insertable());
assertFalse(inverseJoinColumns[1].updatable());
assertFalse(inverseJoinColumns[1].nullable());
assertTrue(inverseJoinColumns[1].unique());
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
assertEquals(2, uniqueConstraints.length);
assertEquals("", uniqueConstraints[0].name());
assertEquals(1, uniqueConstraints[0].columnNames().length);
assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
assertEquals("uq1", uniqueConstraints[1].name());
assertEquals(2, uniqueConstraints[1].columnNames().length);
assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
}
Aggregations