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:org.seasar.hibernate.jpa.metadata.HibernateAttributeDesc.java

License:Apache License

public void setValue(final Object entity, final Object value) {
    if (id) {/*from   ww  w. j ava 2  s .  c  om*/
        metadata.setIdentifier(entity, Serializable.class.cast(value), EntityMode.POJO);
    } else {
        metadata.setPropertyValue(entity, name, value, EntityMode.POJO);
    }
}

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

License:Apache License

/**
 * <code>value</code>?ID???<code>allValues</code>?????
 * /*from w w  w  . j a v  a2s. 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 a2  s  .co  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));
    }
}

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

License:Apache License

/**
 * ??????????????????//from  w  ww  .  j ava  2s  . co  m
 * 
 * @param owner
 *            ???????
 * @return ????????
 */
protected Object[] getValues(final Object owner) {
    return componentType.getPropertyValues(owner, EntityMode.POJO);
}

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

License:Apache License

public void setValue(final Object owner, final Object value) {
    final Object[] values = getValues(owner);
    values[index] = value;//from w w w.  java 2s . c o m
    componentType.setPropertyValues(owner, values, EntityMode.POJO);
}

From source file:org.shept.org.springframework.orm.hibernate3.IgnoreCaseCriteriaBuilder.java

License:Apache License

public Criteria buildCriteria(String entityName, Object exampleEntity, SortDefinition sortDef,
        Session session) {//w w w.j ava  2 s  .co m

    Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName)
            : session.createCriteria(exampleEntity.getClass()));
    executableCriteria.add(Example.create(exampleEntity).ignoreCase());

    String sortPropRoot = "";

    if (sortDef != null & StringUtils.hasText(sortDef.getProperty())) {
        // alias is needed to support sorting by associated entites properties e.g. assEntity.name
        // maybe we need to generate a surrogate rootProperty alias name (instead of reusing the given property name) ???
        int rootIdx = sortDef.getProperty().indexOf(".");
        if (rootIdx > 0) {
            sortPropRoot = sortDef.getProperty().substring(0, rootIdx);
            executableCriteria.createAlias(sortPropRoot, sortPropRoot);
        }
        executableCriteria.addOrder(
                sortDef.isAscending() ? Order.asc(sortDef.getProperty()) : Order.desc(sortDef.getProperty()));
    }

    ClassMetadata meta = session.getSessionFactory().getClassMetadata(exampleEntity.getClass());
    // add subcriteria 
    //      List results = session.createCriteria(Parent.class)
    //         .add( Example.create(parent).ignoreCase() )
    //         .createCriteria("child")
    //         .add( Example.create( parent.getChild() ) )
    //         .list();
    String[] propNames = meta.getPropertyNames();
    for (String propName : propNames) {
        if (meta.getPropertyType(propName).isEntityType()) {
            Object propVal = meta.getPropertyValue(exampleEntity, propName, EntityMode.POJO);
            // Unfortunately the Hibernate criteria implementation isn't perfect, so if we have
            // both a sort subcriteria and a filter subcriteria then we get errors from hibernate
            // "org.hibernate.QueryException: duplicate association path"
            // workaround here is to exclude sorts from the filter [ && !propName.equals(sortPropRoot) ] 
            // which may give unexpected results
            if (propVal != null && !propName.equals(sortPropRoot)) {
                executableCriteria.createCriteria(propName).add(Example.create(propVal).ignoreCase());
            }
        }
    }
    return executableCriteria;
}

From source file:org.shept.persistence.provider.DaoUtils.java

License:Apache License

public static Object getIdValue(DaoSupport dao, Object entityModelTemplate) {
    ClassMetadata modelMeta = getClassMetadata(dao, entityModelTemplate);
    if (null == modelMeta) {
        return null;
    }/*from w  w w  .jav a  2s  .  c om*/
    return modelMeta.getIdentifier(entityModelTemplate, EntityMode.POJO);
}

From source file:org.shept.persistence.provider.DaoUtils.java

License:Apache License

/**
 * Checking if it is a new model (identifier == null) If the index is a
 * compound index we must check all components if just one of them is null
 * //w  ww  . j a va 2  s . c  o m
 * @param index
 * @return
 */
public static boolean isNewModel(DaoSupport dao, Object model) {
    ClassMetadata modelMeta = getClassMetadata(dao, model);
    if (null == modelMeta) {
        return false;
    }

    Object idValue = modelMeta.getIdentifier(model, EntityMode.POJO);

    if (idValue == null) {
        return true;
    }
    Type type = modelMeta.getIdentifierType();
    if (!(type instanceof ComponentType)) {
        return false;
    }

    // didn't manage to get the individual objects of a compound index out of
    // the Hibernate metaModel API although that should be possible ...
    PropertyDescriptor[] desc = BeanUtils.getPropertyDescriptors(idValue.getClass());
    for (int i = 0; i < desc.length; i++) {
        Method mth = desc[i].getReadMethod();
        Object val = ReflectionUtils.invokeMethod(mth, idValue);
        if (null == val) {
            return true;
        }
    }

    return false;
}

From source file:org.shept.persistence.provider.DaoUtils.java

License:Apache License

/**
 * This deepCopy will copy all properties of the template model but ignore
 * the id. In case the id is a compound id the the resulting copy will get a
 * deepCopy of the id if the composite id is only partially filled
 * //w ww  . j a v a2 s .c o m
 * NOTE that the method will only create shallow copies except for component
 * properties which will be deep copied This means that collections and
 * associations will NOT be deep copied
 * 
 * @param dao
 * @param entityModelTemplate
 * @return a copy of the entityModelTemplate
 */
public static Object deepCopyModel(DaoSupport dao, Object entityModelTemplate) {
    ClassMetadata modelMeta = getClassMetadata(dao, entityModelTemplate);
    if (null == modelMeta) {
        return null;
    }
    Object modelCopy = shallowCopyModel(dao, entityModelTemplate, false);

    // Ids of new models are either null or composite ids and will be copied
    // if new
    boolean isCopyId = isNewModel(dao, entityModelTemplate);
    Object idValue = modelMeta.getIdentifier(entityModelTemplate, EntityMode.POJO);
    if (null != idValue && isCopyId) {
        String idName = modelMeta.getIdentifierPropertyName();
        Object idCopy = BeanUtils.instantiateClass(idValue.getClass());
        BeanUtils.copyProperties(idValue, idCopy, new String[] { idName });
        modelMeta.setIdentifier(modelCopy, (Serializable) idCopy, EntityMode.POJO);
    }

    String[] names = modelMeta.getPropertyNames();
    Type[] types = modelMeta.getPropertyTypes();
    for (int i = 0; i < modelMeta.getPropertyNames().length; i++) {
        if (types[i].isComponentType()) {
            String propName = names[i];
            Object propValue = modelMeta.getPropertyValue(entityModelTemplate, propName, EntityMode.POJO);
            Object propCopy = shallowCopyModel(dao, propValue, true);
            modelMeta.setPropertyValue(modelCopy, propName, propCopy, EntityMode.POJO);
        }
    }
    return modelCopy;
}

From source file:org.shept.persistence.provider.DaoUtils.java

License:Apache License

private static Object copyTemplate_Experimental(HibernateDaoSupport dao, Object entityModelTemplate) {
    ClassMetadata modelMeta = getClassMetadata(dao, entityModelTemplate);
    if (null == modelMeta) {
        return null;
    }//from w w  w  .jav  a 2s .  co  m
    String idName = modelMeta.getIdentifierPropertyName();
    Object modelCopy = BeanUtils.instantiateClass(entityModelTemplate.getClass());
    BeanUtils.copyProperties(entityModelTemplate, modelCopy, new String[] { idName });

    Type idType = modelMeta.getIdentifierType();
    if (null == idType || !idType.isComponentType()) {
        return modelCopy;
    }

    Object idValue = modelMeta.getPropertyValue(entityModelTemplate, idName, EntityMode.POJO);
    if (null == idValue) {
        return modelCopy;
    }

    Object idCopy = BeanUtils.instantiate(idValue.getClass());
    BeanUtils.copyProperties(idValue, idCopy);

    if (null == idValue || (null != idType)) {
        return modelCopy;
    }

    Method idMth = ReflectionUtils.findMethod(entityModelTemplate.getClass(),
            "set" + StringUtils.capitalize(idName), new Class[] {});
    if (idMth != null) {
        ReflectionUtils.invokeMethod(idMth, modelCopy, idCopy);
    }

    return modelCopy;
}