Example usage for org.hibernate.type CollectionType getAssociatedEntityName

List of usage examples for org.hibernate.type CollectionType getAssociatedEntityName

Introduction

In this page you can find the example usage for org.hibernate.type CollectionType getAssociatedEntityName.

Prototype

@Override
    public String getAssociatedEntityName(SessionFactoryImplementor factory) throws MappingException 

Source Link

Usage

From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java

License:Open Source License

/**
 * Get the MetaEntity at the other end of this role.
 */// www.  j  a  v  a 2  s . co m
@Override
public HibMetaEntity getOtherMetaEntity() {

    if (otherMetaEntity != null)
        return otherMetaEntity;

    SessionFactoryImplementor sfi = (SessionFactoryImplementor) metaEntity.getMetaModel().getSessionFactory();
    EntityPersister thisPers = metaEntity.getPersister();
    Type type = thisPers.getPropertyType(roleName);
    if (type.isCollectionType() && !isCollection)
        throw new RuntimeException("Internal metadata inconsistency: role name " + roleName + " of "
                + metaEntity.getEntityName() + " is and isn't a collection");
    else if (type.isEntityType() && isCollection)
        throw new RuntimeException("Internal metadata inconsistency: role name " + roleName + " of "
                + metaEntity.getEntityName() + " is and isn't an entity");

    String otherEntityName = null;
    if (isCollection) {
        CollectionType ctype = (CollectionType) type;
        otherEntityName = ctype.getAssociatedEntityName(sfi);
    } else {
        EntityType etype = (EntityType) type;
        otherEntityName = etype.getAssociatedEntityName(sfi);
    }

    otherMetaEntity = (HibMetaEntity) metaEntity.getMetaModel().getMetaEntity(otherEntityName);
    if (otherMetaEntity == null)
        throw new RuntimeException("Unable to find entity " + otherEntityName + ", which is the value of role "
                + metaEntity.getEntityName() + "." + roleName);

    return otherMetaEntity;
}

From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java

License:Open Source License

/**
 * Get the inverse of a many-to-one role
 * @param entityName The entity owning the role
 * @param rName The role name/*  w ww  . j  a  v a2  s  .c o m*/
 * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role
 */
private String[] getInverseOfSingleRole(String entityName, String rName) {
    String[] result = new String[2];

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName);
    int propIdx = childMeta.getPropertyIndex(rName);
    String[] cnames = childMeta.getPropertyColumnNames(propIdx);
    Type parentType = childMeta.getPropertyType(rName);
    if (parentType instanceof OneToOneType)
        return getInverseOfOneToOneRole(entityName, rName);
    if (!(parentType instanceof ManyToOneType))
        throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName
                + " is neither single-valued not multi-valued");
    ManyToOneType manyType = (ManyToOneType) parentType;
    String parentEntityName = manyType.getAssociatedEntityName();

    AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName);
    String[] propNames = parentMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = parentMeta.getPropertyType(propNames[i]);
        if (!type.isCollectionType())
            continue;
        CollectionType collType = (CollectionType) type;
        if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName))
            continue;

        AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact
                .getCollectionMetadata(parentEntityName + "." + propNames[i]);
        String[] colNames = persister.getKeyColumnNames();
        if (cnames.length != colNames.length)
            continue;
        boolean columnMatch = true;
        for (int j = 0; j < cnames.length; j++) {
            if (!cnames[j].equals(colNames[j])) {
                columnMatch = false;
                break;
            }
        }
        if (columnMatch) {
            result[0] = parentEntityName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java

License:Open Source License

/**
 * Cascade to the collection elements// w  w  w  .  j  a  va 2s .  c  o m
 */
private void cascadeCollectionElements(final Object child, final CollectionType collectionType,
        final CascadeStyle style, final Type elemType, final Object anything,
        final boolean isCascadeDeleteEnabled) throws HibernateException {
    // we can't cascade to non-embedded elements
    boolean embeddedElements = eventSource.getEntityMode() != EntityMode.DOM4J
            || ((EntityType) collectionType.getElementType(eventSource.getFactory())).isEmbeddedInXML();

    boolean reallyDoCascade = style.reallyDoCascade(action) && embeddedElements
            && child != CollectionType.UNFETCHED_COLLECTION;

    if (reallyDoCascade) {
        if (log.isTraceEnabled()) {
            log.trace("cascade " + action + " for collection: " + collectionType.getRole());
        }

        Iterator iter = action.getCascadableChildrenIterator(eventSource, collectionType, child);
        while (iter.hasNext()) {
            cascadeProperty(iter.next(), elemType, style, anything, isCascadeDeleteEnabled);
        }

        if (log.isTraceEnabled()) {
            log.trace("done cascade " + action + " for collection: " + collectionType.getRole());
        }
    }

    final boolean deleteOrphans = hasOrphanDelete(style) && action.deleteOrphans() && elemType.isEntityType()
            && child instanceof PersistentCollection; //a newly instantiated collection can't have orphans

    if (deleteOrphans) { // handle orphaned entities!!
        if (log.isTraceEnabled()) {
            log.trace("deleting orphans for collection: " + collectionType.getRole());
        }

        // we can do the cast since orphan-delete does not apply to:
        // 1. newly instantiated collections
        // 2. arrays (we can't track orphans for detached arrays)
        final String entityName = collectionType.getAssociatedEntityName(eventSource.getFactory());
        deleteOrphans(entityName, (PersistentCollection) child);

        if (log.isTraceEnabled()) {
            log.trace("done deleting orphans for collection: " + collectionType.getRole());
        }
    }
}