Search in sources :

Example 1 with Backref

use of org.hibernate.mapping.Backref in project hibernate-orm by hibernate.

the class CollectionBinder method bindOneToManySecondPass.

protected void bindOneToManySecondPass(Collection collection, Map persistentClasses, Ejb3JoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
    final boolean debugEnabled = LOG.isDebugEnabled();
    if (debugEnabled) {
        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 = (PersistentClass) persistentClasses.get(assocClass);
    if (jpaOrderBy != null) {
        final String orderByFragment = buildOrderByClauseFromHql(jpaOrderBy.value(), associatedClass, collection.getRole());
        if (StringHelper.isNotEmpty(orderByFragment)) {
            collection.setOrderBy(orderByFragment);
        }
    }
    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 (Ejb3JoinColumn column : fkJoinColumns) {
        column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
        column.setJoins(joins);
        collection.setCollectionTable(column.getTable());
    }
    if (debugEnabled) {
        LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
    }
    bindFilters(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);
    }
}
Also used : AssertionFailure(org.hibernate.annotations.common.AssertionFailure) Join(org.hibernate.mapping.Join) Backref(org.hibernate.mapping.Backref) OneToMany(javax.persistence.OneToMany) MappingException(org.hibernate.MappingException) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

OneToMany (javax.persistence.OneToMany)1 MappingException (org.hibernate.MappingException)1 AssertionFailure (org.hibernate.annotations.common.AssertionFailure)1 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)1 Backref (org.hibernate.mapping.Backref)1 Join (org.hibernate.mapping.Join)1 PersistentClass (org.hibernate.mapping.PersistentClass)1