Example usage for org.hibernate EntityMode POJO

List of usage examples for org.hibernate EntityMode POJO

Introduction

In this page you can find the example usage for org.hibernate EntityMode POJO.

Prototype

EntityMode POJO

To view the source code for org.hibernate EntityMode POJO.

Click Source Link

Document

The pojo entity mode describes an entity model made up of entity classes (loosely) following the java bean convention.

Usage

From source file:com.mpe.common.validation.BeanValidationEventListener.java

License:Open Source License

private <T> void validate(T object, EntityMode mode, EntityPersister persister,
        SessionFactoryImplementor sessionFactory, GroupsPerOperation.Operation operation) {
    if (object == null || mode != EntityMode.POJO) {
        return;/*www. j av a2s. c om*/
    }
    TraversableResolver tr = new HibernateTraversableResolver(persister, associationsPerEntityPersister,
            sessionFactory);
    Validator validator = factory.usingContext()
            .messageInterpolator(
                    new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator("globalMessages")))
            .traversableResolver(tr).getValidator();
    final Class<?>[] groups = groupsPerOperation.get(operation);
    if (groups.length > 0) {
        final Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, groups);
        if (constraintViolations.size() > 0) {
            /*Set<ConstraintViolation<?>> propagatedViolations =
                  new HashSet<ConstraintViolation<?>>( constraintViolations.size() );
            Set<String> classNames = new HashSet<String>();
            for ( ConstraintViolation<?> violation : constraintViolations ) {
               LOG.trace( violation );
               propagatedViolations.add( violation );
               classNames.add( violation.getLeafBean().getClass().getName() );
            }
            StringBuilder builder = new StringBuilder();
            builder.append( "Validation failed for classes " );
            builder.append( classNames );
            builder.append( " during " );
            builder.append( operation.getName() );
            builder.append( " time for groups " );
            builder.append( toString( groups ) );
            builder.append( "\nList of constraint violations:[\n" );
            for (ConstraintViolation<?> violation : constraintViolations) {
               builder.append( "\t" ).append( violation.toString() ).append("\n");
            }
            builder.append( "]" );
                    
            throw new ConstraintViolationException(
                  builder.toString(), propagatedViolations
            );*/
            Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>(
                    constraintViolations.size());
            StringBuilder builder = new StringBuilder();
            for (ConstraintViolation<?> violation : constraintViolations) {
                builder.append(violation.getMessage()).append("\n");
            }

            logger.info("CCC >> " + builder.toString());

            throw new ConstraintViolationException(builder.toString(), propagatedViolations);

        }
    }
}

From source file:com.ncs.gsyt.core.base.search.hibernate.HibernateEntityMetadata.java

License:Apache License

public Serializable getIdValue(Object object) {
    if (object instanceof HibernateProxy) {
        return ((HibernateProxy) object).getHibernateLazyInitializer().getIdentifier();
    } else {//from   w  w w.  j  a  va  2 s .c om
        return metadata.getIdentifier(object, EntityMode.POJO);
    }
}

From source file:com.ncs.gsyt.core.base.search.hibernate.HibernateEntityMetadata.java

License:Apache License

public Class<?> getJavaClass() {
    return metadata.getMappedClass(EntityMode.POJO);
}

From source file:com.ncs.gsyt.core.base.search.hibernate.HibernateEntityMetadata.java

License:Apache License

public Object getPropertyValue(Object object, String property) {
    if (getIdProperty().equals(property))
        return metadata.getIdentifier(object, EntityMode.POJO);
    else//from w ww. j a v  a2s.  c  o  m
        return metadata.getPropertyValue(object, property, EntityMode.POJO);
}

From source file:com.redhat.rhn.common.hibernate.HibernateFactory.java

License:Open Source License

/**
 * Util to reload an object using Hibernate
 * @param obj to be reloaded/*from   ww  w.  j a v a2 s. c  o  m*/
 * @return Object found if not, null
 * @throws HibernateException if something bad happens.
 */
public static Object reload(Object obj) throws HibernateException {
    // assertNotNull(obj);
    ClassMetadata cmd = connectionManager.getMetadata(obj);
    Serializable id = cmd.getIdentifier(obj, EntityMode.POJO);
    Session session = getSession();
    session.flush();
    session.evict(obj);
    /*
     * In hibernate 3, the following doesn't work:
     * session.load(obj.getClass(), id);
     * load returns the proxy class instead of the persisted class, ie,
     * Filter$$EnhancerByCGLIB$$9bcc734d_2 instead of Filter.
     * session.get is set to not return the proxy class, so that is what we'll use.
     */
    Object result = session.get(obj.getClass(), id);
    // assertNotSame(obj, result);
    return result;
}

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

License:Apache License

/**
 * Resolve entities if not defined in skipEntities_ collection
 * //from  w  w  w  .java2  s  .  co  m
 * @param entity_
 * @param arrayList
 * @return
 */
@SuppressWarnings("unchecked")
private Object resolveDeepProxies(final Object entity_, final ArrayList<Class<?>> arrayList,
        final Collection<String> resolvedEntities_) {
    // if not found object
    if (entity_ == null) {
        return entity_;
    }

    Class<? extends Object> clazz;
    if (entity_ instanceof HibernateProxy) {
        clazz = HibernateProxyHelper.getClassWithoutInitializingProxy(entity_);
    } else {
        clazz = entity_.getClass();
    }

    loadLazyObject(entity_);

    final ClassMetadata metadata = this.getSessionFactory().getClassMetadata(clazz);
    if (metadata != null) {
        final String[] propertyNames = metadata.getPropertyNames();
        final Type[] propertyTypes = metadata.getPropertyTypes();
        String propName = null;
        Type propType = null;
        // recursively resolve all child associations
        for (int i = 0; i < propertyNames.length; i++) {
            propName = propertyNames[i];
            propType = propertyTypes[i];

            // many-to-one and one-to-one associations
            if (propType.isEntityType()) {
                Object assoc = metadata.getPropertyValue(entity_, propName, EntityMode.POJO);
                Hibernate.initialize(assoc);
                if (assoc != null) {
                    assoc = resolveDeepProxies(assoc, arrayList, resolvedEntities_);
                    metadata.setPropertyValue(entity_, propName, assoc, EntityMode.POJO);
                }
            }
            // one-to-many associations
            else if (propType.isCollectionType()) {
                // PersistentMap (Collection<Object>)
                final Object children = metadata.getPropertyValue(entity_, propName, EntityMode.POJO);
                if (children instanceof Collection) {
                    Hibernate.initialize(children);
                    if (children != null) {
                        final Collection<Object> resolvedChildren = new ArrayList();
                        for (final Object child : ((Collection) children)) {
                            final Object resolvedChild = resolveDeepProxies(child, arrayList,
                                    resolvedEntities_);
                            resolvedChildren.add(resolvedChild);
                        }
                        metadata.setPropertyValue(entity_, propName, resolvedChildren, EntityMode.POJO);
                    }
                }
            }
        }
    }

    // resolve Parent object
    return loadLazyObject(entity_);
}

From source file:com.yahoo.elide.datastores.hibernate3.HibernateStore.java

License:Apache License

@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
    /* bind all entities */
    for (ClassMetadata meta : sessionFactory.getAllClassMetadata().values()) {
        dictionary.bindEntity(meta.getMappedClass(EntityMode.POJO));
    }// w ww. j  ava  2s.  c  o  m
}

From source file:common.util.db.MonthEqExpression.java

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    return new TypedValue[] {
            new TypedValue(criteriaQuery.getIdentifierType(criteria), month, EntityMode.POJO) };
}

From source file:common.util.db.MonthGroupExpression.java

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    return new TypedValue[] { new TypedValue(criteriaQuery.getIdentifierType(criteria), EntityMode.POJO) };
}

From source file:common.util.db.YearEqExpression.java

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    return new TypedValue[] {
            new TypedValue(criteriaQuery.getIdentifierType(criteria), year, EntityMode.POJO) };
}