Example usage for org.hibernate.persister.entity EntityPersister getEntityName

List of usage examples for org.hibernate.persister.entity EntityPersister getEntityName

Introduction

In this page you can find the example usage for org.hibernate.persister.entity EntityPersister getEntityName.

Prototype

String getEntityName();

Source Link

Document

The entity name which this persister maps.

Usage

From source file:com.autobizlogic.abl.data.hibernate.HibPersistentBean.java

License:Open Source License

/**
 * Create from a persistent bean, either Pojo or Map.
 * @param bean A Hibernate persistent bean (can be a proxy)
 * @param pk The primary key for the given bean
 * @param persister The persister for the given bean.
 *///w  w w.  j a v a2 s.c o m
@SuppressWarnings("unchecked")
protected HibPersistentBean(Object bean, Serializable pk, EntityPersister persister, Session session) {

    if (bean instanceof Map) {
        try {
            this.map = (Map<String, Object>) bean;
        } catch (Exception ex) {
            throw new RuntimeException("Map is not Map<String, Object> for type " + persister.getEntityName());
        }
    } else {
        this.bean = getRawBean(bean);
        beanMap = new BeanMap(this.bean);
        map = beanMap;
    }
    this.pk = pk;

    this.metaEntity = MetaModelFactory.getHibernateMetaModel(persister.getFactory())
            .getMetaEntity(persister.getEntityName());
    this.session = session;
}

From source file:com.autobizlogic.abl.data.hibernate.HibPersistentBeanCopy.java

License:Open Source License

/**
 * Create from a persistent bean.//from   w  w  w  . j  a  v  a2 s  . co  m
 * @param bean A Hibernate persistent bean (can be a proxy) - either Pojo or Map, or even
 * a PersistentBean.
 * @param pk The primary key for the given bean
 * @param persister The persister for the given bean.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected HibPersistentBeanCopy(Object bean, Serializable pk, EntityPersister persister) {
    this.pk = pk;
    this.metaEntity = MetaModelFactory.getHibernateMetaModel(persister.getFactory())
            .getMetaEntity(persister.getEntityName());
    if (metaEntity == null)
        throw new RuntimeException("Could not find metadata from entity " + persister.getEntityName());

    if (bean instanceof HibPersistentBean) {
        HibPersistentBean pb = (HibPersistentBean) bean;
        this.bean = pb.bean;
        this.map = pb.map;
        this.beanMap = pb.beanMap;
    } else if (bean instanceof HibPersistentBeanCopy) {
        throw new RuntimeException("It makes no sense to make a copy of a HibPersistentBeanCopy");
    } else if (bean instanceof Map) {
        beanMap = (Map) bean;
        this.map = (Map<String, Object>) bean;
    } else {
        beanMap = new BeanMap(bean);
        this.bean = bean;
        map = beanMap;
    }

    if (map == null)
        throw new RuntimeException("No map defined for HibPersistentBeanCopy");

    // Copy all attributes
    for (MetaAttribute metaAttrib : metaEntity.getMetaAttributes()) {
        Object value = map.get(metaAttrib.getName());
        values.put(metaAttrib.getName(), value);
    }

    // Copy single-valued relationships
    for (MetaRole metaRole : metaEntity.getRolesFromChildToParents()) {
        String roleName = metaRole.getRoleName();
        Object value = map.get(roleName);
        values.put(roleName, value);
    }
}

From source file:com.autobizlogic.abl.data.hibernate.HibPersistentBeanCopy.java

License:Open Source License

/**
 * Create from a state array./*from  w w w .ja va 2  s.c o m*/
 * @param The state (typically from a Hibernate event)
 * @param pk The primary key
 * @param persister The persister for the object
 */
@SuppressWarnings("unchecked")
protected HibPersistentBeanCopy(Object[] state, Object entity, Serializable pk, EntityPersister persister,
        Session session) {
    if (entity instanceof Map)
        map = (Map<String, Object>) entity;
    else {
        bean = entity;
        beanMap = new BeanMap(entity);
    }
    this.pk = pk;
    this.metaEntity = MetaModelFactory.getHibernateMetaModel(persister.getFactory())
            .getMetaEntity(persister.getEntityName());

    ClassMetadata metadata = persister.getClassMetadata();
    String[] propNames = metadata.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        String propName = propNames[i];
        if (metaEntity.getMetaProperty(propName).isCollection())
            continue;

        MetaRole metaRole = metaEntity.getMetaRole(propName);
        if (metaRole == null) { // Not a relationship -- must be an attribute
            if (state[i] != null)
                values.put(propName, state[i]);
        } else if (!metaRole.isCollection()) {
            // In the case of old values, when we are handed the state, it contains the pk for associations,
            // and not (as you'd expect) the object itself. So we check whether the value is a real object,
            // and if it's not, we grab it from the object.
            if (state[i] == null || session.contains(state[i])) {
                values.put(propName, state[i]);
            } else {
                // We have a pk instead of a proxy -- ask Hibernate to create a proxy for it.
                String className = metadata.getPropertyType(propName).getReturnedClass().getName();
                PersistenceContext persContext = HibernateSessionUtil.getPersistenceContextForSession(session);
                EntityPersister entityPersister = HibernateSessionUtil.getEntityPersister(session, className,
                        bean);
                EntityKey entityKey = new EntityKey((Serializable) state[i], entityPersister,
                        session.getEntityMode());

                // Has a proxy already been created for this session?
                Object proxy = persContext.getProxy(entityKey);
                if (proxy == null) {
                    // There is no proxy anywhere for this object, so ask Hibernate to create one for us
                    proxy = entityPersister.createProxy((Serializable) state[i], (SessionImplementor) session);
                    persContext.getBatchFetchQueue().addBatchLoadableEntityKey(entityKey);
                    persContext.addProxy(entityKey, proxy);
                }
                values.put(propName, proxy);
            }
        }
    }
}

From source file:com.autobizlogic.abl.hibernate.HibernateUtil.java

License:Open Source License

/**
 * Get the value of the identifier for the given entity, whatever its type (Pojo or Map).
 * @param entity The entity//w  ww  .j  ava 2  s . co  m
 * @param persister The entity's persister
 * @return The identifier for the entity
 */
public static Serializable getPrimaryKeyForEntity(Object entity, EntityPersister persister) {

    String entityName = persister.getEntityName();
    MetaModel metaModel = MetaModelFactory.getHibernateMetaModel(persister.getFactory());
    MetaEntity metaEntity = metaModel.getMetaEntity(entityName);
    String pkName = metaEntity.getIdentifierName();
    if (entity instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) entity;
        return (Serializable) map.get(pkName);
    }
    BeanMap beanMap = new BeanMap(entity);
    return (Serializable) beanMap.get(pkName);
}

From source file:com.gisgraphy.dao.hibernate.HibernateConfigurationTest.java

License:Open Source License

@SuppressWarnings("unchecked")
public void testColumnMapping() throws Exception {
    Session session = sessionFactory.openSession();
    try {/*from  w w  w.  j  ava  2 s.c  o m*/
        Map metadata = sessionFactory.getAllClassMetadata();
        for (Object o : metadata.values()) {
            EntityPersister persister = (EntityPersister) o;
            String className = persister.getEntityName();
            log.debug("Trying select * from: " + className);
            Query q = session.createQuery("from " + className + " c");
            q.iterate();
            log.debug("ok: " + className);
        }
    } finally {
        session.close();
    }
}

From source file:com.googlecode.hibernate.audit.listener.AuditListener.java

License:Open Source License

public boolean requiresPostCommitHanding(EntityPersister persister) {
    return auditConfiguration.getExtensionManager().getAuditableInformationProvider()
            .isAuditable(persister.getEntityName());
}

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

License:Open Source License

/**
 * Cascade an action from the parent entity instance to all its children.  This
 * form is typicaly called from within cascade actions.
 *
 * @param persister The parent's entity persister
 * @param parent The parent reference./*from w ww . ja  v a 2 s .  c o m*/
 * @param anything Anything ;)   Typically some form of cascade-local cache
 * which is specific to each CascadingAction type
 * @throws HibernateException
 */
public void cascade(final EntityPersister persister, final Object parent, final Object anything)
        throws HibernateException {

    if (persister.hasCascades() || action.requiresNoCascadeChecking()) { // performance opt
        if (log.isTraceEnabled()) {
            log.trace("processing cascade " + action + " for: " + persister.getEntityName());
        }

        Type[] types = persister.getPropertyTypes();
        CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
        EntityMode entityMode = eventSource.getEntityMode();
        boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties(parent, entityMode);
        for (int i = 0; i < types.length; i++) {
            CascadeStyle style = cascadeStyles[i];
            if (hasUninitializedLazyProperties && persister.getPropertyLaziness()[i]
                    && !action.performOnLazyProperty()) {
                //do nothing to avoid a lazy property initialization
                continue;
            }

            if (style.doCascade(action)) {
                cascadeProperty(persister.getPropertyValue(parent, i, entityMode), types[i], style, anything,
                        false);
            } else if (action.requiresNoCascadeChecking()) {
                action.noCascade(eventSource, persister.getPropertyValue(parent, i, entityMode), parent,
                        persister, i);
            }
        }

        if (log.isTraceEnabled()) {
            log.trace("done processing cascade " + action + " for: " + persister.getEntityName());
        }
    }
}

From source file:com.syncnapsis.tests.HibernateConfigurationTestCase.java

License:Open Source License

@SuppressWarnings("rawtypes")
public void testColumnMapping() throws Exception {
    logger.debug("testing column mapping...");

    Map metadata = HibernateUtil.currentSession().getSessionFactory().getAllClassMetadata();
    for (Object o : metadata.values()) {
        EntityPersister persister = (EntityPersister) o;
        String className = persister.getEntityName();
        logger.debug("Trying select * from: " + className);
        Query q = HibernateUtil.currentSession().createQuery("from " + className + " c");
        q.iterate();/*from w ww .j  a  va2 s. c  o m*/
        logger.debug("ok: " + className);
    }
}

From source file:com.syncnapsis.tests.HibernateConfigurationTestCase.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testEqualsAndHashCode() throws Exception {
    logger.debug("testing column mapping...");

    Class contactClass = null;// w ww. j  a  v a2s  .c  o m
    try {
        contactClass = Class.forName("com.syncnapsis.data.model.Contact");
    } catch (ClassNotFoundException e) {
        logger.debug("Class 'Contact' not found!");
    }

    Map metadata = HibernateUtil.currentSession().getSessionFactory().getAllClassMetadata();
    for (Object o : metadata.values()) {
        EntityPersister persister = (EntityPersister) o;
        String className = persister.getEntityName();

        logger.debug("Testing for hashcode and equals: " + className);
        Class<?> cls = Class.forName(className);

        if (contactClass != null && contactClass.isAssignableFrom(cls)) {
            logger.debug("Ignored subclass of Contact: " + className);
            continue;
        }

        boolean found = false;
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals("equals") && m.getParameterTypes().length == 1
                    && m.getParameterTypes()[0].equals(Object.class)) {
                found = true;
                break;
            }
        }
        assertTrue("no equals found: " + className, found);
        found = false;
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals("hashCode") && m.getParameterTypes().length == 0) {
                found = true;
                break;
            }
        }
        assertTrue("no hashCode found: " + className, found);
    }
}

From source file:com.utest.dao.UtestDeleteEventListener.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from w ww  . j  ava  2 s .co m
/**
 * Handle the given delete event.  This is the cascaded form.
 *
 * @param event The delete event.
 * @param transientEntities The cache of entities already deleted
 *
 * @throws HibernateException
 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
    final EventSource source = event.getSession();

    final PersistenceContext persistenceContext = source.getPersistenceContext();
    Object entity = persistenceContext.unproxyAndReassociate(event.getObject());
    EntityEntry entityEntry = persistenceContext.getEntry(entity);

    final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
    final Object version;

    if (persister.isVersioned()) {
        version = persister.getVersion(entity, source.getEntityMode());
        // Make sure version has not changed on deleted entities
        if ((entity instanceof TimelineEntity) && !((TimelineEntity) entity).isNew()) {
            if (!persister.getVersionType().isEqual(version, entityEntry.getVersion())) {
                throw new StaleObjectStateException(persister.getEntityName(), entityEntry.getId());
            }
        }
    }
    super.onDelete(event, transientEntities);

}