use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.
the class Ejb3JoinColumn method buildJoinColumn.
/**
* Build JoinColumn for a JOINED hierarchy
*/
public static Ejb3JoinColumn buildJoinColumn(PrimaryKeyJoinColumn pkJoinAnn, JoinColumn joinAnn, Value identifier, Map<String, Join> joins, PropertyHolder propertyHolder, MetadataBuildingContext context) {
final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
Column col = (Column) identifier.getColumnIterator().next();
String defaultName = context.getMetadataCollector().getLogicalColumnName(identifier.getTable(), col.getQuotedName());
if (pkJoinAnn != null || joinAnn != null) {
String colName;
String columnDefinition;
String referencedColumnName;
if (pkJoinAnn != null) {
colName = pkJoinAnn.name();
columnDefinition = pkJoinAnn.columnDefinition();
referencedColumnName = pkJoinAnn.referencedColumnName();
} else {
colName = joinAnn.name();
columnDefinition = joinAnn.columnDefinition();
referencedColumnName = joinAnn.referencedColumnName();
}
final String sqlType;
if (columnDefinition.equals("")) {
sqlType = null;
} else {
sqlType = normalizer.toDatabaseIdentifierText(columnDefinition);
}
final String name;
if ("".equals(colName)) {
name = normalizer.normalizeIdentifierQuotingAsString(defaultName);
} else {
name = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(colName);
}
return new Ejb3JoinColumn(sqlType, name, false, false, true, true, referencedColumnName, null, joins, propertyHolder, null, null, false, context);
} else {
defaultName = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(defaultName);
return new Ejb3JoinColumn(null, defaultName, false, false, true, true, null, null, joins, propertyHolder, null, null, true, context);
}
}
use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.
the class Ejb3JoinColumn method buildJoinTableJoinColumns.
public static Ejb3JoinColumn[] buildJoinTableJoinColumns(JoinColumn[] annJoins, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, String propertyName, String mappedBy, MetadataBuildingContext buildingContext) {
Ejb3JoinColumn[] joinColumns;
if (annJoins == null) {
Ejb3JoinColumn currentJoinColumn = new Ejb3JoinColumn();
currentJoinColumn.setImplicit(true);
//I break the spec, but it's for good
currentJoinColumn.setNullable(false);
currentJoinColumn.setPropertyHolder(propertyHolder);
currentJoinColumn.setJoins(secondaryTables);
currentJoinColumn.setBuildingContext(buildingContext);
currentJoinColumn.setPropertyName(BinderHelper.getRelativePath(propertyHolder, propertyName));
currentJoinColumn.setMappedBy(mappedBy);
currentJoinColumn.bind();
joinColumns = new Ejb3JoinColumn[] { currentJoinColumn };
} else {
joinColumns = new Ejb3JoinColumn[annJoins.length];
JoinColumn annJoin;
int length = annJoins.length;
for (int index = 0; index < length; index++) {
annJoin = annJoins[index];
Ejb3JoinColumn currentJoinColumn = new Ejb3JoinColumn();
currentJoinColumn.setImplicit(true);
currentJoinColumn.setPropertyHolder(propertyHolder);
currentJoinColumn.setJoins(secondaryTables);
currentJoinColumn.setBuildingContext(buildingContext);
currentJoinColumn.setPropertyName(BinderHelper.getRelativePath(propertyHolder, propertyName));
currentJoinColumn.setMappedBy(mappedBy);
currentJoinColumn.setJoinAnnotation(annJoin, propertyName);
//I break the spec, but it's for good
currentJoinColumn.setNullable(false);
//done afterQuery the annotation to override it
currentJoinColumn.bind();
joinColumns[index] = currentJoinColumn;
}
}
return joinColumns;
}
use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.
the class ColumnsBuilder method buildExplicitJoinColumns.
Ejb3JoinColumn[] buildExplicitJoinColumns(XProperty property, PropertyData inferredData) {
//process @JoinColumn(s) beforeQuery @Column(s) to handle collection of entities properly
JoinColumn[] joinColumnAnnotations = null;
if (property.isAnnotationPresent(JoinColumn.class)) {
joinColumnAnnotations = new JoinColumn[] { property.getAnnotation(JoinColumn.class) };
} else if (property.isAnnotationPresent(JoinColumns.class)) {
JoinColumns joinColumnAnnotation = property.getAnnotation(JoinColumns.class);
joinColumnAnnotations = joinColumnAnnotation.value();
int length = joinColumnAnnotations.length;
if (length == 0) {
throw new AnnotationException("Cannot bind an empty @JoinColumns");
}
}
if (joinColumnAnnotations != null) {
return Ejb3JoinColumn.buildJoinColumns(joinColumnAnnotations, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
}
JoinColumnOrFormula[] joinColumnOrFormulaAnnotations = null;
if (property.isAnnotationPresent(JoinColumnOrFormula.class)) {
joinColumnOrFormulaAnnotations = new JoinColumnOrFormula[] { property.getAnnotation(JoinColumnOrFormula.class) };
} else if (property.isAnnotationPresent(JoinColumnsOrFormulas.class)) {
JoinColumnsOrFormulas joinColumnsOrFormulasAnnotations = property.getAnnotation(JoinColumnsOrFormulas.class);
joinColumnOrFormulaAnnotations = joinColumnsOrFormulasAnnotations.value();
int length = joinColumnOrFormulaAnnotations.length;
if (length == 0) {
throw new AnnotationException("Cannot bind an empty @JoinColumnsOrFormulas");
}
}
if (joinColumnOrFormulaAnnotations != null) {
return Ejb3JoinColumn.buildJoinColumnsOrFormulas(joinColumnOrFormulaAnnotations, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
}
if (property.isAnnotationPresent(JoinFormula.class)) {
JoinFormula ann = property.getAnnotation(JoinFormula.class);
Ejb3JoinColumn[] ejb3JoinColumns = new Ejb3JoinColumn[1];
ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinFormula(ann, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
return ejb3JoinColumns;
}
return null;
}
use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.
the class EntityBinder method createPrimaryColumnsToSecondaryTable.
private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {
Ejb3JoinColumn[] ejb3JoinColumns;
PrimaryKeyJoinColumn[] pkColumnsAnn = null;
JoinColumn[] joinColumnsAnn = null;
if (uncastedColumn instanceof PrimaryKeyJoinColumn[]) {
pkColumnsAnn = (PrimaryKeyJoinColumn[]) uncastedColumn;
}
if (uncastedColumn instanceof JoinColumn[]) {
joinColumnsAnn = (JoinColumn[]) uncastedColumn;
}
if (pkColumnsAnn == null && joinColumnsAnn == null) {
ejb3JoinColumns = new Ejb3JoinColumn[1];
ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
} else {
int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
if (nbrOfJoinColumns == 0) {
ejb3JoinColumns = new Ejb3JoinColumn[1];
ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
} else {
ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns];
if (pkColumnsAnn != null) {
for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
}
} else {
for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
}
}
}
}
for (Ejb3JoinColumn joinColumn : ejb3JoinColumns) {
joinColumn.forceNotNull();
}
bindJoinToPersistentClass(join, ejb3JoinColumns, context);
}
use of javax.persistence.JoinColumn 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());
}
Aggregations