Example usage for org.hibernate.type Type isEntityType

List of usage examples for org.hibernate.type Type isEntityType

Introduction

In this page you can find the example usage for org.hibernate.type Type isEntityType.

Prototype

boolean isEntityType();

Source Link

Document

Return true if the implementation is castable to EntityType .

Usage

From source file:com.autobizlogic.abl.logic.LogicSvcs.java

License:Open Source License

/**
 * // ww  w  . j av  a  2s.  c  o m
 * @param aSourceHibernateBean
 * @param aLogicRunner - just for context
 * @return 2nd instance of aSourceHibernateBean, with non-collection properties
 */
public static Object beanCopyOf(Object aSourceHibernateBean, LogicRunner aLogicRunner) {
    Object rtnTargetHibernateBean = null;
    LogicTransactionContext context = aLogicRunner.getContext();

    BigDecimal defaultValue = null; //new BigDecimal("0.0");  // an experiment
    Converter bdc = new BigDecimalConverter(defaultValue);
    ConvertUtils.register(bdc, BigDecimal.class);

    Class<?> hibernateDomainBeanClass = HibernateUtil.getEntityClassForBean(aSourceHibernateBean);
    ClassMetadata entityMeta = context.getSession().getSessionFactory()
            .getClassMetadata(hibernateDomainBeanClass);
    Type propertyTypes[] = entityMeta.getPropertyTypes();
    String propertyNames[] = entityMeta.getPropertyNames(); // hope names/types share index...

    String className = hibernateDomainBeanClass.getName();
    try {
        rtnTargetHibernateBean = ClassLoaderManager.getInstance().getClassFromName(className).newInstance();
    } catch (Exception e) {
        throw new LogicException("Unable to instantatiate new Bean like: " + aSourceHibernateBean, e);
    }
    BeanMap rtnBeanMap = new BeanMap(rtnTargetHibernateBean);
    BeanMap srcBeanMap = new BeanMap(aSourceHibernateBean);
    Object eachValue = null;
    for (int i = 0; i < propertyNames.length; i++) {
        String propertyName = propertyNames[i];
        try {
            Type type = propertyTypes[i];
            if (!type.isCollectionType() && !type.isEntityType()) { // NB - not moving collections!
                eachValue = srcBeanMap.get(propertyName);
                rtnBeanMap.put(propertyName, eachValue);
            } else {
                BeanUtils.setProperty(rtnTargetHibernateBean, propertyNames[i], null); // prevent ClassCastException: HashSet cannot be cast to PersistentCollection
            }
        } catch (Exception e) {
            throw new LogicException("Cannot set Property: " + propertyNames[i] + " = " + eachValue + ", on "
                    + rtnTargetHibernateBean);
        }
    }
    // TODO - IMPORTANT - set the key field(s), presumably using Hibernate meta data      
    return rtnTargetHibernateBean;
}

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

License:Open Source License

/**
 * Read all the properties from Hibernate metadata
 *//*  w  w  w .j a  va  2  s. co  m*/
private void loadAllProperties() {
    if (allPropsRetrieved)
        return;

    synchronized (this) {
        if (!allPropsRetrieved) {
            metaAttributes = new HashMap<String, MetaAttribute>();
            metaRoles = new HashMap<String, MetaRole>();
            ClassMetadata meta = persister.getClassMetadata();
            String[] propNames = meta.getPropertyNames();
            for (String propName : propNames) {
                Type type;
                try {
                    type = persister.getClassMetadata().getPropertyType(propName);
                } catch (QueryException ex) {
                    throw new RuntimeException("Unable to determine type for property " + propName
                            + " of entity " + persister.getEntityName());
                }
                if (type.isComponentType()) {
                    // Do nothing
                } else if (type.isCollectionType() || type.isEntityType() || type.isAssociationType()) {
                    boolean isParentToChild = type.isCollectionType();
                    MetaRole mr = new HibMetaRole(this, propName, isParentToChild);
                    metaRoles.put(propName, mr);
                } else {
                    MetaAttribute ma = new HibMetaAttribute(propName, type.getReturnedClass(), false);
                    metaAttributes.put(propName, ma);
                }
            }

            // Often the primary attribute(s) is not returned by ClassMetadata.getPropertyNames
            // So we add it by hand here
            String pkName = meta.getIdentifierPropertyName();

            if (pkName == null) { // Can happen for composite keys
                Type pkType = meta.getIdentifierType();
                if (pkType.isComponentType()) {
                    ComponentType ctype = (ComponentType) pkType;
                    String[] pnames = ctype.getPropertyNames();
                    for (String pname : pnames) {
                        MetaAttribute ma = new HibMetaAttribute(pname,
                                meta.getPropertyType(pname).getReturnedClass(), false);
                        metaAttributes.put(pname, ma);
                    }
                } else
                    throw new RuntimeException(
                            "Unexpected: anonymous PK is not composite - class " + meta.getEntityName());
            } else if (!metaAttributes.containsKey(pkName)) {
                MetaAttribute ma = new HibMetaAttribute(pkName, meta.getIdentifierType().getReturnedClass(),
                        false);
                metaAttributes.put(pkName, ma);
            }

            allPropsRetrieved = true;
        }
    }
}

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

License:Open Source License

/**
 * Get the MetaEntity at the other end of this role.
 *///from  w w  w .j a  va2s . c o  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 opposite role of the given role, if it exists
 * @param entityName The name of the entity who owns the role, e.g. com.foo.Customer
 * @param rName The role name, e.g. orders
 * @return Null if the role has no known inverse, or [entity name, role name] of the inverse
 *///w w  w  .j  av  a 2s.c o  m
private String[] getInverseOfRole(String entityName, String rName) {

    SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory();
    ClassMetadata classMeta = sessFact.getClassMetadata(entityName);
    Type propType = classMeta.getPropertyType(rName);
    if (propType.isCollectionType()) {
        return getInverseOfCollectionRole(entityName, rName);
    }

    if (propType.isEntityType()) {
        return getInverseOfSingleRole(entityName, rName);
    }

    log.debug("Role " + entityName + "." + rName + " is neither a collection type "
            + "nor an entity type, and will be assumed to have no inverse.");
    return null;
}

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//from   ww w .  j ava 2 s .com
 * @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:com.ejushang.steward.common.genericdao.search.hibernate.HibernateEntityMetadata.java

License:Apache License

public Metadata getPropertyType(String property) {
    Type pType = metadata.getPropertyType(property);
    Class<?> pCollectionType = null;
    if (pType.isCollectionType()) {
        pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
        pCollectionType = pType.getReturnedClass();
    }/* ww w .ja  va  2  s .co  m*/

    if (pType.isEntityType()) {
        return new HibernateEntityMetadata(sessionFactory,
                sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
    } else {
        return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
    }
}

From source file:com.ejushang.steward.common.genericdao.search.hibernate.HibernateNonEntityMetadata.java

License:Apache License

public Metadata getPropertyType(String property) {
    if (!type.isComponentType())
        return null;

    int i = getPropertyIndex(property);
    if (i == -1) {
        return null;
    } else {//  w  ww  .ja v  a  2s .co m
        Type pType = ((ComponentType) type).getSubtypes()[i];
        Class<?> pCollectionType = null;
        if (pType.isCollectionType()) {
            pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
            pCollectionType = pType.getReturnedClass();
        }
        if (pType.isEntityType()) {
            return new HibernateEntityMetadata(sessionFactory,
                    sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
        } else {
            return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
        }
    }
}

From source file:com.fiveamsolutions.nci.commons.audit.AuditLogInterceptor.java

License:Open Source License

private boolean needsAuditing(Object auditableObj, Type type, Object newValue, Object oldValue,
        String property) {// w w w.j ava2 s  . c  om
    if (type.isCollectionType()) {
        return collectionNeedsAuditing(auditableObj, newValue, oldValue, property);
    }
    if (type.isEntityType()) {
        if (newValue != null && !processor.isAuditableEntity(newValue)) {
            return false;
        }
        if (oldValue != null && !processor.isAuditableEntity(oldValue)) {
            return false;
        }
    }

    return !ObjectUtils.equals(newValue, oldValue);
}

From source file:com.gonzasilve.puntoventas.pvcore.dao.hibernate.HibernateEntityMetadata.java

License:Apache License

public IMetadata getPropertyType(String property) {
    Type pType = metadata.getPropertyType(property);
    Class<?> pCollectionType = null;
    if (pType.isCollectionType()) {
        pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
        pCollectionType = pType.getReturnedClass();
    }//from w w  w  . j ava2 s.  com

    if (pType.isEntityType()) {
        return new HibernateEntityMetadata(sessionFactory,
                sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
    } else {
        return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
    }
}

From source file:com.gonzasilve.puntoventas.pvcore.dao.hibernate.HibernateNonEntityMetadata.java

License:Apache License

public IMetadata getPropertyType(String property) {
    if (!type.isComponentType())
        return null;

    int i = getPropertyIndex(property);
    if (i == -1) {
        return null;
    } else {//w ww.j av  a2 s . c  o m
        Type pType = ((ComponentType) type).getSubtypes()[i];
        Class<?> pCollectionType = null;
        if (pType.isCollectionType()) {
            pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory);
            pCollectionType = pType.getReturnedClass();
        }
        if (pType.isEntityType()) {
            return new HibernateEntityMetadata(sessionFactory,
                    sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType);
        } else {
            return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType);
        }
    }
}