use of jakarta.persistence.OneToMany in project hibernate-orm by hibernate.
the class CollectionBinder method bindOneToManySecondPass.
protected void bindOneToManySecondPass(Collection collection, Map<String, PersistentClass> persistentClasses, AnnotatedJoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName);
}
if (buildingContext == null) {
throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings");
}
org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany(buildingContext, collection.getOwner());
collection.setElement(oneToMany);
oneToMany.setReferencedEntityName(collectionType.getName());
oneToMany.setIgnoreNotFound(ignoreNotFound);
String assocClass = oneToMany.getReferencedEntityName();
PersistentClass associatedClass = persistentClasses.get(assocClass);
handleJpaOrderBy(collection, associatedClass);
Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins(assocClass);
if (associatedClass == null) {
throw new MappingException(String.format("Association [%s] for entity [%s] references unmapped class [%s]", propertyName, propertyHolder.getClassName(), assocClass));
}
oneToMany.setAssociatedClass(associatedClass);
for (AnnotatedJoinColumn column : fkJoinColumns) {
column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
column.setJoins(joins);
collection.setCollectionTable(column.getTable());
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
}
bindFilters(false);
handleWhere(false);
bindCollectionSecondPass(collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
if (!collection.isInverse() && !collection.getKey().isNullable()) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
String entityName = oneToMany.getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
Backref prop = new Backref();
prop.setName('_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref");
prop.setUpdateable(false);
prop.setSelectable(false);
prop.setCollectionRole(collection.getRole());
prop.setEntityName(collection.getOwner().getEntityName());
prop.setValue(collection.getKey());
referenced.addProperty(prop);
}
}
use of jakarta.persistence.OneToMany in project hibernate-orm by hibernate.
the class ColumnsBuilder method extractMetadata.
public ColumnsBuilder extractMetadata() {
columns = null;
joinColumns = buildExplicitJoinColumns(property, inferredData);
Comment comment = property.getAnnotation(Comment.class);
if (property.isAnnotationPresent(Column.class)) {
columns = buildColumnFromAnnotation(property.getAnnotation(Column.class), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
} else if (property.isAnnotationPresent(Formula.class)) {
columns = buildFormulaFromAnnotation(getOverridableAnnotation(property, Formula.class, buildingContext), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
} else if (property.isAnnotationPresent(Columns.class)) {
columns = buildColumnsFromAnnotations(property.getAnnotation(Columns.class).columns(), comment, 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);
joinColumns = AnnotatedJoinColumn.buildJoinColumns(null, comment, oneToMany != null ? oneToMany.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 = buildColumnFromNoAnnotation(comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
if (nullability == Nullability.FORCED_NOT_NULL) {
// force columns to not null
for (AnnotatedColumn col : columns) {
col.forceNotNull();
}
}
return this;
}
use of jakarta.persistence.OneToMany in project hibernate-orm by hibernate.
the class Ejb3XmlOneToManyTest method testNoChildren.
@Test
public void testNoChildren() throws Exception {
reader = getReader(Entity2.class, "field1", "one-to-many.orm1.xml");
assertAnnotationPresent(OneToMany.class);
assertAnnotationNotPresent(OrderBy.class);
assertAnnotationNotPresent(OrderColumn.class);
assertAnnotationNotPresent(MapKey.class);
assertAnnotationNotPresent(MapKeyClass.class);
assertAnnotationNotPresent(MapKeyTemporal.class);
assertAnnotationNotPresent(MapKeyEnumerated.class);
assertAnnotationNotPresent(MapKeyColumn.class);
assertAnnotationNotPresent(MapKeyJoinColumns.class);
assertAnnotationNotPresent(MapKeyJoinColumn.class);
assertAnnotationNotPresent(JoinTable.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinColumn.class);
assertAnnotationNotPresent(Access.class);
OneToMany relAnno = reader.getAnnotation(OneToMany.class);
assertEquals(0, relAnno.cascade().length);
assertEquals(FetchType.LAZY, relAnno.fetch());
assertEquals("", relAnno.mappedBy());
assertFalse(relAnno.orphanRemoval());
assertEquals(void.class, relAnno.targetEntity());
}
use of jakarta.persistence.OneToMany in project hibernate-orm by hibernate.
the class Ejb3XmlOneToManyTest method testCascadeAll.
@Test
public void testCascadeAll() throws Exception {
reader = getReader(Entity2.class, "field1", "one-to-many.orm20.xml");
assertAnnotationPresent(OneToMany.class);
OneToMany relAnno = reader.getAnnotation(OneToMany.class);
assertEquals(1, relAnno.cascade().length);
assertEquals(CascadeType.ALL, relAnno.cascade()[0]);
}
use of jakarta.persistence.OneToMany in project eclipselink by eclipse-ee4j.
the class AbstractFieldMapping method getMemberType.
@Override
protected Class<?> getMemberType() {
Field field = getMember();
// One to One
OneToOne oneToOne = field.getAnnotation(OneToOne.class);
if (oneToOne != null) {
Class<?> targetEntity = oneToOne.targetEntity();
if (targetEntity != void.class) {
return targetEntity;
}
}
// Many to One
ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
if (manyToOne != null) {
Class<?> targetEntity = manyToOne.targetEntity();
if (targetEntity != void.class) {
return targetEntity;
}
}
// Many to Many
ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
if (manyToMany != null) {
Class<?> targetEntity = manyToMany.targetEntity();
if (targetEntity != void.class) {
return targetEntity;
}
}
// One to Many
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
if (oneToMany != null) {
Class<?> targetEntity = oneToMany.targetEntity();
if (targetEntity != void.class) {
return targetEntity;
}
}
return field.getType();
}
Aggregations