Example usage for org.hibernate.metadata ClassMetadata getIdentifierPropertyName

List of usage examples for org.hibernate.metadata ClassMetadata getIdentifierPropertyName

Introduction

In this page you can find the example usage for org.hibernate.metadata ClassMetadata getIdentifierPropertyName.

Prototype

String getIdentifierPropertyName();

Source Link

Document

Get the name of the identifier property (or return null)

Usage

From source file:be.shad.tsqb.helper.TypeSafeQueryHelperImpl.java

License:Apache License

/**
 * Creates data based on the hibernate metadata for the given <code>property</code>.
 *//*from   w ww  . ja v  a2s.c  o  m*/
TypeSafeQueryProxyData createChildData(TypeSafeQueryInternal query, TypeSafeQueryProxyData parent,
        String property) {
    Type propertyType = getTargetType(parent, property);
    Class<?> targetClass = getTargetEntityClass(propertyType);
    ClassMetadata metadata = getMetaData(targetClass);
    if (metadata == null && !propertyType.isComponentType()) {
        return query.getDataTree().createData(parent, property, targetClass);
    }
    TypeSafeQueryProxyType proxyType = null;
    if (metadata != null) {
        proxyType = propertyType.isCollectionType() ? EntityCollectionType : EntityType;
    } else {
        proxyType = propertyType instanceof ComponentType ? ComponentType : CompositeType;
    }
    TypeSafeQueryProxy proxy = (TypeSafeQueryProxy) proxyFactory.getProxy(targetClass, proxyType);
    TypeSafeQueryProxyData data = query.getDataTree().createData(parent, property, targetClass, proxyType,
            metadata == null ? null : metadata.getIdentifierPropertyName(), proxy);
    setEntityProxyMethodListener(query, proxy, data);
    return data;
}

From source file:be.shad.tsqb.helper.TypeSafeQueryHelperImpl.java

License:Apache License

/**
 * {@inheritDoc}/* www .  java2 s  . c  om*/
 */
private TypeSafeQueryProxyData createClassJoinProxy(TypeSafeQueryInternal query, TypeSafeQueryProxyData parent,
        Class<?> targetClass) {
    ClassMetadata metadata = getMetaData(targetClass);
    TypeSafeQueryProxyType proxyType = TypeSafeQueryProxyType.EntityType;
    TypeSafeQueryProxy proxy = (TypeSafeQueryProxy) proxyFactory.getProxy(targetClass, proxyType);
    TypeSafeQueryProxyData data = query.getDataTree().createData(parent, null, targetClass, proxyType,
            metadata.getIdentifierPropertyName(), proxy);
    setEntityProxyMethodListener(query, proxy, data);
    return data;
}

From source file:cc.cnfc.core.orm.hibernate.SimpleHibernateDao.java

License:Open Source License

/**
 * ???./*from   ww  w  .  ja va2s .c  o m*/
 */
public String getIdName() {
    ClassMetadata meta = getSessionFactory().getClassMetadata(entityClass);
    return meta.getIdentifierPropertyName();
}

From source file:cc.cnfc.core.service.BaseService.java

License:Open Source License

/**
 * ???./*from w ww . j av  a2 s .co m*/
 */
public String getIdName(Class<?> clazz) {
    ClassMetadata meta = sessionFactory.getClassMetadata(clazz);
    return meta.getIdentifierPropertyName();
}

From source file:com.agaramtech.lims.dao.support.AgaramDaoSupport.java

License:Open Source License

public void assignField(Object object, int value) throws Exception {
    ClassMetadata classMetadata = getSessionFactory().getClassMetadata(object.getClass());
    String identifierPropertyName = classMetadata.getIdentifierPropertyName();
    Field fieldValue = object.getClass().getDeclaredField(identifierPropertyName);
    fieldValue.setAccessible(true);//from  ww w . ja va  2  s.c  o  m
    fieldValue.setInt(object, value);
}

From source file:com.agaramtech.lims.dao.support.AgaramDaoSupport.java

License:Open Source License

public void assignField(Object object, String value) throws Exception {
    ClassMetadata classMetadata = getSessionFactory().getClassMetadata(object.getClass());
    String identifierPropertyName = classMetadata.getIdentifierPropertyName();
    Field fieldValue = object.getClass().getDeclaredField(identifierPropertyName);
    fieldValue.setAccessible(true);/*from   w w w.j a  va 2 s.c  om*/
    fieldValue.set(object, value);
}

From source file:com.agaramtech.lims.dao.support.AgaramDaoSupport.java

License:Open Source License

public String getIdField(Object object) throws Exception {
    ClassMetadata classMetadata = getSessionFactory().getClassMetadata(object.getClass());
    String identifierPropertyName = classMetadata.getIdentifierPropertyName();
    return identifierPropertyName;
}

From source file:com.agaramtech.lims.dao.support.AgaramDaoSupport.java

License:Open Source License

public String getIdField(Class<?> classes) throws Exception {
    ClassMetadata classMetadata = getSessionFactory().getClassMetadata(classes);
    String identifierPropertyName = classMetadata.getIdentifierPropertyName();
    return identifierPropertyName;
}

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

License:Open Source License

/**
 * Read all the properties from Hibernate metadata
 *//*from w w w.j  a v a  2 s.c om*/
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.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  ww w.  ja v a2 s.co  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;
}