use of jakarta.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.
the class AnnotationBinder method handleForeignKeys.
private static void handleForeignKeys(XClass clazzToProcess, MetadataBuildingContext context, DependantValue key) {
ForeignKey foreignKey = clazzToProcess.getAnnotation(ForeignKey.class);
if (foreignKey != null && !BinderHelper.isEmptyAnnotationValue(foreignKey.name())) {
key.setForeignKeyName(foreignKey.name());
} else {
PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumn.class);
PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation(PrimaryKeyJoinColumns.class);
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
if (pkJoinColumns != null && (pkJoinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || pkJoinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault)) {
// don't apply a constraint based on ConstraintMode
key.disableForeignKey();
} else if (pkJoinColumns != null && !StringHelper.isEmpty(pkJoinColumns.foreignKey().name())) {
key.setForeignKeyName(pkJoinColumns.foreignKey().name());
if (!BinderHelper.isEmptyAnnotationValue(pkJoinColumns.foreignKey().foreignKeyDefinition())) {
key.setForeignKeyDefinition(pkJoinColumns.foreignKey().foreignKeyDefinition());
}
} else if (pkJoinColumn != null && (pkJoinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || pkJoinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault)) {
// don't apply a constraint based on ConstraintMode
key.disableForeignKey();
} else if (pkJoinColumn != null && !StringHelper.isEmpty(pkJoinColumn.foreignKey().name())) {
key.setForeignKeyName(pkJoinColumn.foreignKey().name());
if (!BinderHelper.isEmptyAnnotationValue(pkJoinColumn.foreignKey().foreignKeyDefinition())) {
key.setForeignKeyDefinition(pkJoinColumn.foreignKey().foreignKeyDefinition());
}
}
}
}
use of jakarta.persistence.PrimaryKeyJoinColumn 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);
}
use of jakarta.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.
the class Ejb3XmlOneToOneTest method testSinglePrimaryKeyJoinColumn.
/**
* When there's a single primary key join column, we still wrap it with
* a PrimaryKeyJoinColumns annotation.
*/
@Test
public void testSinglePrimaryKeyJoinColumn() throws Exception {
reader = getReader(Entity1.class, "field1", "one-to-one.orm2.xml");
assertAnnotationPresent(OneToOne.class);
assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
assertAnnotationPresent(PrimaryKeyJoinColumns.class);
PrimaryKeyJoinColumns joinColumnsAnno = reader.getAnnotation(PrimaryKeyJoinColumns.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinColumn.class);
assertAnnotationNotPresent(JoinTable.class);
PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals(1, joinColumns.length);
assertEquals("col1", joinColumns[0].name());
assertEquals("col2", joinColumns[0].referencedColumnName());
assertEquals("int", joinColumns[0].columnDefinition());
}
use of jakarta.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method getPrimaryKeyJoinColumns.
/**
* @param mergeWithAnnotations Whether to use Java annotations for this
* element, if present and not disabled by the XMLContext defaults.
* In some contexts (such as an association mapping) merging with
*/
private PrimaryKeyJoinColumns getPrimaryKeyJoinColumns(List<JaxbPrimaryKeyJoinColumn> elements, XMLContext.Default defaults, boolean mergeWithAnnotations) {
PrimaryKeyJoinColumn[] columns = buildPrimaryKeyJoinColumns(elements);
if (mergeWithAnnotations) {
if (columns.length == 0 && defaults.canUseJavaAnnotations()) {
PrimaryKeyJoinColumn annotation = getPhysicalAnnotation(PrimaryKeyJoinColumn.class);
if (annotation != null) {
columns = new PrimaryKeyJoinColumn[] { annotation };
} else {
PrimaryKeyJoinColumns annotations = getPhysicalAnnotation(PrimaryKeyJoinColumns.class);
columns = annotations != null ? annotations.value() : columns;
}
}
}
if (columns.length > 0) {
AnnotationDescriptor ad = new AnnotationDescriptor(PrimaryKeyJoinColumns.class);
ad.setValue("value", columns);
return AnnotationFactory.create(ad);
} else {
return null;
}
}
use of jakarta.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method buildPrimaryKeyJoinColumns.
private PrimaryKeyJoinColumn[] buildPrimaryKeyJoinColumns(List<JaxbPrimaryKeyJoinColumn> elements) {
PrimaryKeyJoinColumn[] pkJoinColumns = new PrimaryKeyJoinColumn[elements.size()];
int i = 0;
for (JaxbPrimaryKeyJoinColumn element : elements) {
AnnotationDescriptor pkAnn = new AnnotationDescriptor(PrimaryKeyJoinColumn.class);
copyAttribute(pkAnn, "name", element.getName(), false);
copyAttribute(pkAnn, "referenced-column-name", element.getReferencedColumnName(), false);
copyAttribute(pkAnn, "column-definition", element.getColumnDefinition(), false);
pkJoinColumns[i++] = AnnotationFactory.create(pkAnn);
}
return pkJoinColumns;
}
Aggregations