Example usage for org.hibernate.type EntityType getAssociatedEntityName

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

Introduction

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

Prototype

public final String getAssociatedEntityName() 

Source Link

Document

The name of the associated entity.

Usage

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

License:Open Source License

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

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    AbstractCollectionPersister parentMeta = (AbstractCollectionPersister) sessFact
            .getCollectionMetadata(entityName + "." + rName);
    if (parentMeta == null) { // Could be inherited -- search through superclasses
        while (parentMeta == null) {
            Class<?> cls = null;
            if (getMetaEntity().getEntityType() == MetaEntity.EntityType.POJO)
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.POJO);
            else
                cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.MAP);
            Class<?> superCls = cls.getSuperclass();
            if (superCls.getName().equals("java.lang.Object"))
                throw new RuntimeException(
                        "Unable to retrieve Hibernate information for collection " + entityName + "." + rName);
            ClassMetadata clsMeta = sessFact.getClassMetadata(superCls);
            if (clsMeta == null)
                throw new RuntimeException("Unable to retrieve Hibernate information for collection "
                        + entityName + "." + rName + ", even from superclass(es)");
            entityName = clsMeta.getEntityName();
            parentMeta = (AbstractCollectionPersister) sessFact.getCollectionMetadata(entityName + "." + rName);
        }
    }
    String[] colNames = parentMeta.getKeyColumnNames();
    String childName = parentMeta.getElementType().getName();

    AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(childName);
    String[] propNames = childMeta.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        Type type = childMeta.getPropertyType(propNames[i]);
        if (!type.isEntityType())
            continue;
        EntityType entType = (EntityType) type;
        if (!entType.getAssociatedEntityName().equals(entityName))
            continue;
        String[] cnames = childMeta.getPropertyColumnNames(i);
        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] = childName;
            result[1] = propNames[i];
            return result;
        }
    }

    return null;
}

From source file:org.seasar.hibernate.jpa.metadata.HibernateAttributeDesc.java

License:Apache License

/**
 * <code>value</code>?ID???<code>allValues</code>?????
 * //from  ww  w . j av a 2 s  .  c o m
 * @param allValues
 *            ID??
 * @param type
 *            Hibernate?
 * @param value
 *            ?ID???????ID?
 */
protected void gatherIdAttributeValues(final List<Object> allValues, final Type type, final Object value) {

    if (type.isEntityType()) {
        final EntityType entityType = EntityType.class.cast(type);
        final String name = entityType.getAssociatedEntityName();
        final EntityPersister ep = factory.getEntityPersister(name);
        final Type idType = ep.getIdentifierType();
        final Serializable id = ep.getIdentifier(value, EntityMode.POJO);
        gatherIdAttributeValues(allValues, idType, id);

    } else if (type.isComponentType()) {
        final AbstractComponentType componentType = AbstractComponentType.class.cast(type);
        final Object[] subvalues = componentType.getPropertyValues(value, EntityMode.POJO);
        final Type[] subtypes = componentType.getSubtypes();
        for (int i = 0; i < subtypes.length; i++) {
            gatherIdAttributeValues(allValues, subtypes[i], subvalues[i]);
        }

    } else {
        allValues.add(convert(type, value));
    }
}

From source file:org.seasar.hibernate.jpa.metadata.HibernateAttributeDesc.java

License:Apache License

/**
 * <code>value</code>????<code>allValues</code>?????
 * /* www .  j  a  v  a 2  s . c  o  m*/
 * @param allValues
 *            ??
 * @param type
 *            Hibernate?
 * @param value
 *            ????????
 */
protected void gatherAttributeValues(final List<Object> allValues, final Type type, final Object value) {

    if (value == null) {
        allValues.add(null);
        return;
    }
    if (!isReadTargetType(type)) {
        return;
    }

    if (type.isEntityType()) {
        final EntityType entityType = EntityType.class.cast(type);

        if (entityType.isReferenceToPrimaryKey()) {
            gatherIdAttributeValues(allValues, entityType, value);
        } else {
            final String name = entityType.getAssociatedEntityName();
            final EntityPersister ep = factory.getEntityPersister(name);
            final Type[] subtypes = ep.getPropertyTypes();
            final Object[] subvalue = ep.getPropertyValues(value, EntityMode.POJO);
            for (int i = 0; i < subtypes.length; i++) {
                gatherAttributeValues(allValues, subtypes[i], subvalue[i]);
            }
        }

    } else if (type.isComponentType()) {
        final AbstractComponentType componentType = AbstractComponentType.class.cast(type);
        final Object[] subvalues = componentType.getPropertyValues(value, EntityMode.POJO);
        final Type[] subtypes = componentType.getSubtypes();
        for (int i = 0; i < subtypes.length; i++) {
            gatherAttributeValues(allValues, subtypes[i], subvalues[i]);
        }

    } else {
        allValues.add(convert(type, value));
    }
}