use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.
the class AnnotatedJoinColumn method buildJoinTableJoinColumns.
public static AnnotatedJoinColumn[] buildJoinTableJoinColumns(JoinColumn[] annJoins, Map<String, Join> secondaryTables, PropertyHolder propertyHolder, String propertyName, String mappedBy, MetadataBuildingContext buildingContext) {
AnnotatedJoinColumn[] joinColumns;
if (annJoins == null) {
AnnotatedJoinColumn currentJoinColumn = new AnnotatedJoinColumn();
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 AnnotatedJoinColumn[] { currentJoinColumn };
} else {
joinColumns = new AnnotatedJoinColumn[annJoins.length];
JoinColumn annJoin;
int length = annJoins.length;
for (int index = 0; index < length; index++) {
annJoin = annJoins[index];
AnnotatedJoinColumn currentJoinColumn = new AnnotatedJoinColumn();
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 after the annotation to override it
currentJoinColumn.bind();
joinColumns[index] = currentJoinColumn;
}
}
return joinColumns;
}
use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method getCollectionTable.
private void getCollectionTable(List<Annotation> annotationList, JaxbCollectionTable element, XMLContext.Default defaults) {
if (element != null) {
AnnotationDescriptor annotation = new AnnotationDescriptor(CollectionTable.class);
copyAttribute(annotation, "name", element.getName(), false);
copyAttribute(annotation, "catalog", element.getCatalog(), false);
if (StringHelper.isNotEmpty(defaults.getCatalog()) && StringHelper.isEmpty((String) annotation.valueOf("catalog"))) {
annotation.setValue("catalog", defaults.getCatalog());
}
copyAttribute(annotation, "schema", element.getSchema(), false);
if (StringHelper.isNotEmpty(defaults.getSchema()) && StringHelper.isEmpty((String) annotation.valueOf("schema"))) {
annotation.setValue("schema", defaults.getSchema());
}
JoinColumn[] joinColumns = getJoinColumns(element.getJoinColumn(), false);
if (joinColumns.length > 0) {
annotation.setValue("joinColumns", joinColumns);
}
buildUniqueConstraints(annotation, element.getUniqueConstraint());
buildIndex(annotation, element.getIndex());
annotationList.add(AnnotationFactory.create(annotation));
}
}
use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method buildJoinColumns.
private void buildJoinColumns(List<Annotation> annotationList, List<JaxbJoinColumn> elements) {
JoinColumn[] joinColumns = getJoinColumns(elements, false);
if (joinColumns.length > 0) {
AnnotationDescriptor ad = new AnnotationDescriptor(JoinColumns.class);
ad.setValue("value", joinColumns);
annotationList.add(AnnotationFactory.create(ad));
}
}
use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.
the class CollectionBinder method handleCollectionOfEntities.
private ManyToOne handleCollectionOfEntities(Collection collValue, XClass collType, boolean ignoreNotFound, XProperty property, MetadataBuildingContext buildingContext, PersistentClass collectionEntity, String hqlOrderBy) {
ManyToOne element;
element = new ManyToOne(buildingContext, collValue.getCollectionTable());
collValue.setElement(element);
element.setReferencedEntityName(collType.getName());
// element.setFetchMode( fetchMode );
// element.setLazy( fetchMode != FetchMode.JOIN );
// make the second join non lazy
element.setFetchMode(FetchMode.JOIN);
element.setLazy(false);
element.setIgnoreNotFound(ignoreNotFound);
// as per 11.1.38 of JPA 2.0 spec, default to primary key if no column is specified by @OrderBy.
if (hqlOrderBy != null) {
collValue.setManyToManyOrdering(buildOrderByClauseFromHql(hqlOrderBy, collectionEntity));
}
final ForeignKey fk = property.getAnnotation(ForeignKey.class);
if (fk != null && !isEmptyAnnotationValue(fk.name())) {
element.setForeignKeyName(fk.name());
} else {
final JoinTable joinTableAnn = property.getAnnotation(JoinTable.class);
if (joinTableAnn != null) {
String foreignKeyName = joinTableAnn.inverseForeignKey().name();
String foreignKeyDefinition = joinTableAnn.inverseForeignKey().foreignKeyDefinition();
if (joinTableAnn.inverseJoinColumns().length != 0) {
final JoinColumn joinColumnAnn = joinTableAnn.inverseJoinColumns()[0];
if (foreignKeyName != null && foreignKeyName.isEmpty()) {
foreignKeyName = joinColumnAnn.foreignKey().name();
foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition();
}
}
if (joinTableAnn.inverseForeignKey().value() == ConstraintMode.NO_CONSTRAINT || joinTableAnn.inverseForeignKey().value() == ConstraintMode.PROVIDER_DEFAULT && buildingContext.getBuildingOptions().isNoConstraintByDefault()) {
element.disableForeignKey();
} else {
element.setForeignKeyName(StringHelper.nullIfEmpty(foreignKeyName));
element.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKeyDefinition));
}
}
}
return element;
}
use of jakarta.persistence.JoinColumn in project hibernate-orm by hibernate.
the class EntityBinder method createPrimaryColumnsToSecondaryTable.
private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {
AnnotatedJoinColumn[] annotatedJoinColumns;
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) {
annotatedJoinColumns = new AnnotatedJoinColumn[1];
annotatedJoinColumns[0] = AnnotatedJoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
} else {
int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
if (nbrOfJoinColumns == 0) {
annotatedJoinColumns = new AnnotatedJoinColumn[1];
annotatedJoinColumns[0] = AnnotatedJoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
} else {
annotatedJoinColumns = new AnnotatedJoinColumn[nbrOfJoinColumns];
if (pkColumnsAnn != null) {
for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
annotatedJoinColumns[colIndex] = AnnotatedJoinColumn.buildJoinColumn(pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
}
} else {
for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
annotatedJoinColumns[colIndex] = AnnotatedJoinColumn.buildJoinColumn(null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
}
}
}
}
for (AnnotatedJoinColumn joinColumn : annotatedJoinColumns) {
joinColumn.forceNotNull();
}
bindJoinToPersistentClass(join, annotatedJoinColumns, context);
}
Aggregations