use of javax.persistence.JoinColumns in project hibernate-orm by hibernate.
the class AnnotationBinder method bindManyToOne.
private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, boolean unique, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
// All FK columns should be in the same table
org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne(context, columns[0].getTable());
// This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne
if (unique) {
value.markAsLogicalOneToOne();
}
value.setReferencedEntityName(ToOneBinder.getReferenceEntityName(inferredData, targetEntity, context));
final XProperty property = inferredData.getProperty();
defineFetchingStrategy(value, property);
// value.setFetchMode( fetchMode );
value.setIgnoreNotFound(ignoreNotFound);
value.setCascadeDeleteEnabled(cascadeOnDelete);
// value.setLazy( fetchMode != FetchMode.JOIN );
if (!optional) {
for (Ejb3JoinColumn column : columns) {
column.setNullable(false);
}
}
if (property.isAnnotationPresent(MapsId.class)) {
// read only
for (Ejb3JoinColumn column : columns) {
column.setInsertable(false);
column.setUpdatable(false);
}
}
final JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
final JoinColumns joinColumns = property.getAnnotation(JoinColumns.class);
// Make sure that JPA1 key-many-to-one columns are read only tooj
boolean hasSpecjManyToOne = false;
if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
String columnName = "";
for (XProperty prop : inferredData.getDeclaringClass().getDeclaredProperties(AccessType.FIELD.getType())) {
if (prop.isAnnotationPresent(Id.class) && prop.isAnnotationPresent(Column.class)) {
columnName = prop.getAnnotation(Column.class).name();
}
if (property.isAnnotationPresent(ManyToOne.class) && joinColumn != null && !BinderHelper.isEmptyAnnotationValue(joinColumn.name()) && joinColumn.name().equals(columnName) && !property.isAnnotationPresent(MapsId.class)) {
hasSpecjManyToOne = true;
for (Ejb3JoinColumn column : columns) {
column.setInsertable(false);
column.setUpdatable(false);
}
}
}
}
value.setTypeName(inferredData.getClassOrElementName());
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection(propertyHolder.getClassName(), propertyName);
if ((joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) || (joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT)) {
// not ideal...
value.setForeignKeyName("none");
} else {
final ForeignKey fk = property.getAnnotation(ForeignKey.class);
if (fk != null && StringHelper.isNotEmpty(fk.name())) {
value.setForeignKeyName(fk.name());
} else {
final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), propertyName));
if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
value.setForeignKeyName("none");
} else if (fkOverride != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
} else if (joinColumns != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumns.foreignKey().name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumns.foreignKey().foreignKeyDefinition()));
} else if (joinColumn != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
}
}
}
String path = propertyHolder.getPath() + "." + propertyName;
FkSecondPass secondPass = new ToOneFkSecondPass(value, columns, // cannot have nullable and unique on certain DBs like Derby
!optional && unique, propertyHolder.getEntityOwnerClassName(), path, context);
if (inSecondPass) {
secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
} else {
context.getMetadataCollector().addSecondPass(secondPass);
}
Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
// PropertyBinder binder = new PropertyBinder();
propertyBinder.setName(propertyName);
propertyBinder.setValue(value);
// binder.setCascade(cascadeStrategy);
if (isIdentifierMapper) {
propertyBinder.setInsertable(false);
propertyBinder.setUpdatable(false);
} else if (hasSpecjManyToOne) {
propertyBinder.setInsertable(false);
propertyBinder.setUpdatable(false);
} else {
propertyBinder.setInsertable(columns[0].isInsertable());
propertyBinder.setUpdatable(columns[0].isUpdatable());
}
propertyBinder.setColumns(columns);
propertyBinder.setAccessType(inferredData.getDefaultAccess());
propertyBinder.setCascade(cascadeStrategy);
propertyBinder.setProperty(property);
propertyBinder.setXToMany(true);
propertyBinder.makePropertyAndBind();
}
use of javax.persistence.JoinColumns in project hibernate-orm by hibernate.
the class ColumnsBuilder method extractMetadata.
public ColumnsBuilder extractMetadata() {
columns = null;
joinColumns = buildExplicitJoinColumns(property, inferredData);
if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Formula.class)) {
Column ann = property.getAnnotation(Column.class);
Formula formulaAnn = property.getAnnotation(Formula.class);
columns = Ejb3Column.buildColumnFromAnnotation(new Column[] { ann }, formulaAnn, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
} else if (property.isAnnotationPresent(Columns.class)) {
Columns anns = property.getAnnotation(Columns.class);
columns = Ejb3Column.buildColumnFromAnnotation(anns.columns(), null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
// set default values if needed
if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
} else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
OneToMany oneToMany = property.getAnnotation(OneToMany.class);
String mappedBy = oneToMany != null ? oneToMany.mappedBy() : "";
joinColumns = Ejb3JoinColumn.buildJoinColumns(null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
} else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
}
if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
// useful for collection of embedded elements
columns = Ejb3Column.buildColumnFromAnnotation(null, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
if (nullability == Nullability.FORCED_NOT_NULL) {
// force columns to not null
for (Ejb3Column col : columns) {
col.forceNotNull();
}
}
return this;
}
Aggregations