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:ar.com.zauber.commons.repository.SpringHibernateRepository.java

License:Apache License

/** @see Repository#getPersistibleClasses() */
@SuppressWarnings("unchecked")
public final Collection<Class<?>> getPersistibleClasses() {
    final Collection<Class<?>> classes = new ArrayList<Class<?>>();
    final Collection<ClassMetadata> classMetadatas = getSessionFactory().getAllClassMetadata().values();

    for (final ClassMetadata classMetadata : classMetadatas) {
        classes.add(classMetadata.getMappedClass(EntityMode.POJO));
    }//  w w w  .j  a v a  2  s .  c om

    return classes;
}

From source file:ariba.ui.meta.jpa.HibernateContext.java

License:Apache License

public Object getPrimaryKey(Object o) {
    Session session = getSession();/*from www .  j av  a 2  s  . co m*/
    ClassMetadata classMeta = session.getSessionFactory().getClassMetadata(o.getClass());
    return classMeta.getIdentifier(o, EntityMode.POJO);
}

From source file:ariba.ui.meta.jpa.HibernateContext.java

License:Apache License

protected <T> T getIfAlreadyLoaded(java.lang.Class<T> tClass, Object primaryKey) {
    SessionImpl session = (SessionImpl) getSession();
    ClassMetadata classMeta = session.getSessionFactory().getClassMetadata(tClass);
    String entityName = classMeta.getEntityName();
    EntityPersister persister = session.getFactory().getEntityPersister(entityName);
    EntityKey entityKey = new EntityKey((Serializable) primaryKey, persister, EntityMode.POJO);

    return (T) ((SessionImpl) getSession()).getPersistenceContext().getEntity(entityKey);
    //  return find(tClass, primaryKey);
    // return (T)getSession().get(tClass, (Serializable)primaryKey);
}

From source file:br.com.arsmachina.dao.hibernate.WriteableDAOImpl.java

License:Apache License

/**
 * Returns <code>true</code> if the primary key field (identifier) of the given object is not
 * null. Its value is obtained via {@link ClassMetadata#getIdentifier(Object, EntityMode)}.
 * /*w w w  .j  a  v a2  s  .c o m*/
 * @see br.com.arsmachina.dao.WriteableDAO#isPersistent(java.lang.Object)
 * @throws IllegalArgumentException if <code>object</code> is null.
 */
public boolean isPersistent(T object) {

    if (object == null) {
        throw new IllegalArgumentException("Parameter object cannot be null");
    }

    return getClassMetadata().getIdentifier(object, EntityMode.POJO) != null;

}

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

License:Open Source License

/**
 * Create a PersistentBean from a Pojo or Map entity.
 *///from   ww w .  j  av  a2  s. c  om
public PersistentBean createPersistentBeanFromObject(Object bean, EntityPersister ep) {
    if (bean == null)
        return null;
    if (bean instanceof PersistentBean)
        throw new RuntimeException("Cannot create a PersistentBean from a PersistentBean: " + bean);
    if (!(bean instanceof Map)) {
        Class<?> epCls = ep.getMappedClass(EntityMode.POJO);
        Class<?> beanCls = bean.getClass();
        if (!epCls.isAssignableFrom(beanCls))
            throw new RuntimeException("Bean is of wrong type for the given persister: " + beanCls.getName()
                    + " vs " + epCls.getName());
    }
    Serializable pk;
    try {
        pk = ep.getIdentifier(bean, (SessionImplementor) session);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return new HibPersistentBean(bean, pk, ep, session);
}

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

License:Open Source License

@Override
public Class<?> getEntityClass() {
    return persister.getMappedClass(EntityMode.POJO);
}

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

License:Open Source License

public EntityType getEntityType() {

    if (entityType != null)
        return entityType;

    EntityMode entityMode = ((SessionFactoryImpl) sessionFactory).getSettings().getDefaultEntityMode();
    if (entityMode.equals(EntityMode.POJO))
        entityType = EntityType.POJO;//from  w  w  w  .  ja  v a 2 s  . c o m
    else if (entityMode.equals(EntityMode.MAP))
        entityType = EntityType.MAP;
    else
        throw new RuntimeException("Hibernate session factory has a default entity mode of " + entityMode
                + ", which is neither POJO nor MAP. This is not supported.");

    return entityType;
}

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   w ww  . j  ava2 s  . 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:com.autobizlogic.abl.mgmt.ClassMetadataService.java

License:Open Source License

public static Map<String, Object> getMetadataForClass(Map<String, String> args) {
    String sessionFactoryId = args.get("sessionFactoryId");
    String name = args.get("className");
    HashMap<String, Object> result = new HashMap<String, Object>();

    SessionFactory factory = HibernateConfiguration.getSessionFactoryById(sessionFactoryId);
    if (factory == null)
        return null;

    ClassMetadata meta = factory.getClassMetadata(name);
    if (meta == null)
        return null;

    result.put("sessionFactoryId", sessionFactoryId);
    result.put("className", meta.getEntityName());
    result.put("identifierPropertyName", meta.getIdentifierPropertyName());

    Class<?> cls = meta.getMappedClass(EntityMode.POJO);
    Class<?> supercls = cls.getSuperclass();
    while (ProxyFactory.isProxyClass(supercls))
        supercls = supercls.getSuperclass();
    result.put("superclassName", supercls.getName());

    Map<String, String> properties = new HashMap<String, String>();
    Map<String, Object> collections = new HashMap<String, Object>();
    Map<String, String> associations = new HashMap<String, String>();

    String[] propNames = meta.getPropertyNames();
    Type[] propTypes = meta.getPropertyTypes();
    int i = 0;/*from www.java2  s  . c  o m*/
    for (String propName : propNames) {
        if (propTypes[i].isCollectionType()) {
            CollectionType collType = (CollectionType) propTypes[i];
            Type elementType = collType.getElementType((SessionFactoryImplementor) factory);
            HashMap<String, String> collEntry = new HashMap<String, String>();
            collEntry.put("collectionType", collType.getReturnedClass().getName());
            collEntry.put("elementType", elementType.getName());
            collections.put(propName, collEntry);
        } else if (propTypes[i].isAssociationType()) {
            AssociationType assType = (AssociationType) propTypes[i];
            String assName = assType.getAssociatedEntityName((SessionFactoryImplementor) factory);
            associations.put(propName, assName);
        } else {
            properties.put(propName, propTypes[i].getName());
        }
        i++;
    }
    result.put("properties", properties);
    result.put("associations", associations);
    result.put("collections", collections);

    MetaModel metaModel = MetaModelFactory.getHibernateMetaModel(factory);
    LogicGroup logicGroup = RuleManager.getInstance(metaModel).getLogicGroupForClassName(name);
    if (logicGroup != null) {

        // Operations are actually actions and constraints
        List<Map<String, Object>> operations = new Vector<Map<String, Object>>();
        result.put("operations", operations);

        Set<ActionRule> actions = logicGroup.getActions();
        if (actions != null && actions.size() > 0) {
            for (ActionRule a : actions) {
                Map<String, Object> op = new HashMap<String, Object>();
                op.put("name", a.getLogicMethodName());
                op.put("type", "action");
                operations.add(op);
            }
        }

        Set<EarlyActionRule> eactions = logicGroup.getEarlyActions();
        if (eactions != null && eactions.size() > 0) {
            for (EarlyActionRule a : eactions) {
                Map<String, Object> op = new HashMap<String, Object>();
                op.put("name", a.getLogicMethodName());
                op.put("type", "early action");
                operations.add(op);
            }
        }

        Set<CommitActionRule> cactions = logicGroup.getCommitActions();
        if (cactions != null && cactions.size() > 0) {
            for (CommitActionRule a : cactions) {
                Map<String, Object> op = new HashMap<String, Object>();
                op.put("name", a.getLogicMethodName());
                op.put("type", "commit action");
                operations.add(op);
            }
        }

        Set<ConstraintRule> constraints = logicGroup.getConstraints();
        if (constraints != null && constraints.size() > 0) {
            for (ConstraintRule constraint : constraints) {
                Map<String, Object> op = new HashMap<String, Object>();
                op.put("name", constraint.getLogicMethodName());
                op.put("type", "constraint");
                operations.add(op);
            }
        }

        Set<CommitConstraintRule> cconstraints = logicGroup.getCommitConstraints();
        if (cconstraints != null && cconstraints.size() > 0) {
            for (ConstraintRule cconstraint : cconstraints) {
                Map<String, Object> op = new HashMap<String, Object>();
                op.put("name", cconstraint.getLogicMethodName());
                op.put("type", "commit constraint");
                operations.add(op);
            }
        }

        // Derivations are derived attributes
        Map<String, Object> derivations = new HashMap<String, Object>();
        result.put("derivations", derivations);

        Set<AbstractAggregateRule> aggregates = logicGroup.getAggregates();
        if (aggregates != null && aggregates.size() > 0) {
            for (AbstractAggregateRule aggregate : aggregates) {
                Map<String, Object> agg = new HashMap<String, Object>();
                if (aggregate instanceof CountRule)
                    agg.put("type", "count");
                else if (aggregate instanceof SumRule)
                    agg.put("type", "sum");
                else
                    agg.put("type", "unknown");
                agg.put("methodName", aggregate.getLogicMethodName());
                derivations.put(aggregate.getBeanAttributeName(), agg);
            }
        }

        List<FormulaRule> formulas = logicGroup.getFormulas();
        if (formulas != null && formulas.size() > 0) {
            for (FormulaRule formula : formulas) {
                Map<String, Object> form = new HashMap<String, Object>();
                form.put("type", "formula");
                form.put("methodName", formula.getLogicMethodName());
                derivations.put(formula.getBeanAttributeName(), form);
            }
        }

        Set<ParentCopyRule> pcRules = logicGroup.getParentCopies();
        if (pcRules != null && pcRules.size() > 0) {
            for (ParentCopyRule pcRule : pcRules) {
                Map<String, Object> parentCopy = new HashMap<String, Object>();
                parentCopy.put("type", "parent copy");
                parentCopy.put("methodName", pcRule.getLogicMethodName());
                derivations.put(pcRule.getChildAttributeName(), parentCopy);
            }
        }
    }

    HashMap<String, Object> finalResult = new HashMap<String, Object>();
    finalResult.put("data", result);

    return finalResult;
}

From source file:com.court.controller.HomeFXMLController.java

private Criterion filterByMonthCriterion(final String propertyName) {

    return new Criterion() {

        final int month = new Date().getMonth() + 1;

        @Override// w  ww  .j a v a2s  . c  o m
        public String toSqlString(Criteria crtr, CriteriaQuery cq) throws HibernateException {
            String[] columns = cq.getColumns(propertyName, crtr);
            if (columns.length != 1) {
                throw new HibernateException("monthEq may only be used with single-column properties");
            }
            return "month(" + columns[0] + ") = ?";
        }

        @Override
        public TypedValue[] getTypedValues(Criteria crtr, CriteriaQuery cq) throws HibernateException {
            return new TypedValue[] { new TypedValue(IntegerType.INSTANCE, month, EntityMode.POJO) };
        }

        @Override
        public String toString() {
            return "month(" + propertyName + ") = " + month;
        }

    };
}