Example usage for org.hibernate.mapping Property getType

List of usage examples for org.hibernate.mapping Property getType

Introduction

In this page you can find the example usage for org.hibernate.mapping Property getType.

Prototype

public Type getType() throws MappingException 

Source Link

Usage

From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java

License:Open Source License

/**
 * Retrieve the list of collection properties of the provided persisted class.
 * @param pClass the persisted class to analyse
 * @return a list of hibernate collections
 *///from  www  .  j  a v  a2s .c o m
private List<org.hibernate.mapping.Collection> getCollection(PersistentClass pClass) {
    List<org.hibernate.mapping.Collection> list = new ArrayList<org.hibernate.mapping.Collection>();

    if (pClass != null) {
        @SuppressWarnings("unchecked")
        Iterator<Property> it = pClass.getPropertyIterator();
        while (it.hasNext()) {
            Property property = it.next();
            if (property.getType().isCollectionType()) {
                list.add((org.hibernate.mapping.Collection) property.getValue());
            }
        }
    }

    return list;
}

From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java

License:Open Source License

/**
 * Retrieve a list of tables used to store the given persistent class, that need to be processed for FK constraints.
 * The list include the main table use to persist the class, if this table has FK, as well as, all the collection
 * table used for storing this persisted class properties.
 *
 * @param pClass the persistent class to analyze
 * @return a list of table/*from  w ww  .  j a v a  2 s  . com*/
 */
private List<Table> getForeignKeyTables(PersistentClass pClass) {
    List<Table> list = new ArrayList<Table>();

    if (pClass != null) {
        Table table = pClass.getTable();
        if (checkFKtoPKinTable(table)) {
            list.add(table);
        }

        @SuppressWarnings("unchecked")
        Iterator<Property> it = pClass.getPropertyIterator();
        while (it.hasNext()) {
            Property property = it.next();
            if (property.getType().isCollectionType()) {
                org.hibernate.mapping.Collection coll = (org.hibernate.mapping.Collection) property.getValue();
                Table collTable = coll.getCollectionTable();
                if (checkFKtoPKinTable(collTable)) {
                    list.add(collTable);
                }
            }
        }
    }

    return list;
}

From source file:com.zutubi.pulse.master.hibernate.MutableConfiguration.java

License:Apache License

public Mapping getMapping() {
    return new Mapping() {
        public IdentifierGeneratorFactory getIdentifierGeneratorFactory() {
            return null;
        }/* ww w . j av  a 2s.  c  om*/

        public Type getIdentifierType(String persistentClass) throws MappingException {
            PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            return pc.getIdentifier().getType();
        }

        public String getIdentifierPropertyName(String persistentClass) throws MappingException {
            final PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            if (!pc.hasIdentifierProperty())
                return null;
            return pc.getIdentifierProperty().getName();
        }

        public Type getReferencedPropertyType(String persistentClass, String propertyName)
                throws MappingException {
            final PersistentClass pc = classes.get(persistentClass);
            if (pc == null) {
                throw new MappingException(I18N.format("unknown.persistent.class", persistentClass));
            }
            Property prop = pc.getReferencedProperty(propertyName);
            if (prop == null) {
                throw new MappingException(
                        I18N.format("unknown.persistent.class.property", persistentClass + '.' + propertyName));
            }
            return prop.getType();
        }
    };
}

From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateMapping.java

License:Apache License

public Type getReferencedPropertyType(String persistentClass, String propertyName) throws MappingException {
    final PersistentClass pc = configuration.getClassMapping(persistentClass);
    if (pc == null) {
        throw new MappingException("persistent class not known: " + persistentClass);
    }/*from  www  . j  a va  2 s. c  o  m*/
    Property prop = pc.getReferencedProperty(propertyName);
    if (prop == null) {
        throw new MappingException("property not known: " + persistentClass + '.' + propertyName);
    }
    return prop.getType();
}

From source file:gov.nih.nci.system.dao.impl.orm.ORMDAOImpl.java

License:Open Source License

private void getFieldList(PersistentClass pclass, List<Field> fieldList) throws Exception {
    Iterator<Property> properties = pclass.getPropertyIterator();
    while (properties.hasNext()) {
        Property prop = properties.next();
        if (!prop.getType().isAssociationType()) {
            String fieldName = prop.getName();
            Field field = pclass.getMappedClass().getDeclaredField(fieldName);
            field.setAccessible(true);// www  . jav  a  2  s  . com
            fieldList.add(field);
        }
    }
}

From source file:gov.nih.nci.system.util.ClassCache.java

License:BSD License

private List<Object> getPersistentFieldsForISOObject(Property prop, Configuration cfg) {
    List<Object> isoObjectPsFields = new ArrayList<Object>();
    String idUserType = prop.getType().getName();
    String identifierUserType = "gov.nih.nci.iso21090.hibernate.usertype.IiUserType";
    if (identifierUserType.equals(idUserType)) {
        isoObjectPsFields.add("extension");
    } else if ("id".equals(prop.getName())) {
        isoObjectPsFields.add(prop.getName());
    } else if (prop.getType().isComponentType() && !(prop.getValue() instanceof Any)) {
        processIfComponentMapping(prop, isoObjectPsFields, cfg);

        String componentClassName = ((Component) prop.getValue()).getComponentClassName();
        if (componentClassName != null && (componentClassName.indexOf("Adxp") > 0)) {
            String adxpType = componentClassName.substring(componentClassName.indexOf("Adxp") + 4)
                    .toUpperCase();//  ww w.ja v a2  s  . c om
            Map<String, List<Object>> nestedComponent = new HashMap<String, List<Object>>();
            List<Object> adxpTypeList = new ArrayList<Object>();
            adxpTypeList.add(adxpType);
            nestedComponent.put("type", adxpTypeList);
            isoObjectPsFields.add(nestedComponent);
        } else if (componentClassName != null && (componentClassName.indexOf("Enxp") > 0)) {
            String roleName = ((Component) prop.getValue()).getRoleName();
            String rootKlassAttr = roleName.substring(0, roleName.lastIndexOf('.'));

            ComplexNode complexNode = tuplizerHelper.getComplexNodeBean(rootKlassAttr);
            List<Node> nodes = complexNode.getInnerNodes();
            Map<String, List<Object>> nestedComponent = new HashMap<String, List<Object>>();
            List<Object> enxpTypeList = new ArrayList<Object>();
            //            String value=null;
            for (Node node : nodes) {
                if (node instanceof ConstantNode) {
                    continue;
                }
                ComplexNode innerComplexNode = (ComplexNode) node;
                //               value = innerComplexNode.getName();
                String key = null;
                List<Node> innerNodes = innerComplexNode.getInnerNodes();
                for (Node innerNode : innerNodes) {
                    if (innerNode instanceof ConstantNode && innerNode.getName().equals("type")) {
                        ConstantNode constantNode = (ConstantNode) innerNode;
                        key = constantNode.getConstantValue();
                        enxpTypeList.add(key);
                        break;
                    }
                }
            }

            nestedComponent.put("type", enxpTypeList);
            isoObjectPsFields.add(nestedComponent);
        }
    } else if (prop.getType().isAssociationType() && (prop.getValue() instanceof ManyToOne)) {
        ManyToOne manyToOne = (ManyToOne) prop.getValue();
        String many2OnePClassName = manyToOne.getReferencedEntityName();
        if (!many2OnePClassName.startsWith("_xxEntityxx_gov_nih_nci_cacoresdk_domain_other_datatype")) {
            return isoObjectPsFields;
        }
        PersistentClass many2OnePClass = cfg.getClassMapping(many2OnePClassName);
        Map<String, List<Object>> map = getISOPropertiesForObject(many2OnePClass, cfg);
        Iterator<String> keyItr = map.keySet().iterator();
        while (keyItr.hasNext()) {
            String key = keyItr.next();
            Map<String, List<Object>> tempMap = new HashMap<String, List<Object>>();
            tempMap.put(key, map.get(key));
            isoObjectPsFields.add(tempMap);
        }
    } else {
        String fieldName = prop.getName();
        isoObjectPsFields.add(fieldName);
    }
    return isoObjectPsFields;
}

From source file:gov.nih.nci.system.util.ClassCache.java

License:BSD License

@SuppressWarnings("unchecked")
private void processIfComponentMapping(Property prop, List<Object> isoObjectPsFields, Configuration cfg) {
    Component isoComponent = (Component) prop.getValue();
    Iterator<Property> itr = isoComponent.getPropertyIterator();
    while (itr.hasNext()) {
        Property property = itr.next();
        String fieldName = property.getName();
        if (property.getType().isComponentType()) {
            List<Object> innerPersistentFields = getPersistentFieldsForISOObject(property);

            Map<String, List<Object>> nestedComponent = new HashMap<String, List<Object>>();
            nestedComponent.put(fieldName, innerPersistentFields);
            isoObjectPsFields.add(nestedComponent);
        } else if (property.getType().isAssociationType()) {
            Map<String, List<Object>> nestedComponent = processIfAssociationType(property, fieldName, cfg);
            isoObjectPsFields.add(nestedComponent);
        } else {//from ww  w . ja  v  a  2 s  . c  om
            isoObjectPsFields.add(fieldName);
        }
    }
}

From source file:it.eng.qbe.datasource.hibernate.HibernatePersistenceManager.java

License:Mozilla Public License

public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf) {

    SessionFactory sf = dataSource.getHibernateSessionFactory();
    Configuration cfg = dataSource.getHibernateConfiguration();
    Session aSession = null;/*from   w  w  w  .  j  a  va  2 s  .c o m*/
    Transaction tx = null;
    try {
        aSession = sf.openSession();
        tx = aSession.beginTransaction();
        String entityName = registryConf.getEntity();
        PersistentClass classMapping = cfg.getClassMapping(entityName);
        ClassMetadata classMetadata = sf.getClassMetadata(entityName);
        String keyName = classMetadata.getIdentifierPropertyName();
        Object key = aRecord.get(keyName);
        Property propertyId = classMapping.getProperty(keyName);

        //casts the id to the appropriate java type
        Object keyConverted = this.convertValue(key, propertyId);

        Object obj = aSession.load(entityName, (Serializable) keyConverted);
        Iterator it = aRecord.keys();
        while (it.hasNext()) {
            String aKey = (String) it.next();
            if (keyName.equals(aKey)) {
                continue;
            }
            Column c = registryConf.getColumnConfiguration(aKey);
            if (c.getSubEntity() != null) {
                // case of foreign key
                Property property = classMapping.getProperty(c.getSubEntity());
                Type propertyType = property.getType();
                if (propertyType instanceof ManyToOneType) {
                    ManyToOneType manyToOnePropertyType = (ManyToOneType) propertyType;
                    String entityType = manyToOnePropertyType.getAssociatedEntityName();
                    Object referenced = getReferencedObject(aSession, entityType, c.getField(),
                            aRecord.get(aKey));
                    Setter setter = property.getSetter(obj.getClass());
                    setter.getMethod().invoke(obj, referenced);
                } else {
                    throw new SpagoBIRuntimeException(
                            "Property " + c.getSubEntity() + " is not a many-to-one relation");
                }
            } else {
                // case of property
                Property property = classMapping.getProperty(aKey);
                Setter setter = property.getSetter(obj.getClass());
                Object valueObj = aRecord.get(aKey);
                if (valueObj != null && !valueObj.equals("")) {
                    Object valueConverted = this.convertValue(valueObj, property);
                    setter.getMethod().invoke(obj, valueConverted);
                }

            }

        }
        aSession.saveOrUpdate(obj);
        tx.commit();

    } catch (Exception e) {

        if (tx != null) {
            tx.rollback();
        }
        throw new RuntimeException(e);
    } finally {
        if (aSession != null) {
            if (aSession.isOpen())
                aSession.close();
        }
    }
}

From source file:it.eng.qbe.datasource.hibernate.HibernatePersistenceManager.java

License:Mozilla Public License

private Object convertValue(Object valueObj, Property property) {
    if (valueObj == null) {
        return null;
    }/*from   w w w  .j  a  v a2 s. co  m*/
    String value = valueObj.toString();
    Object toReturn = null;
    Type type = property.getType();
    Class clazz = type.getReturnedClass();

    if (Number.class.isAssignableFrom(clazz)) {
        //BigInteger, Integer, Long, Short, Byte
        if (value.equals("NaN") || value.equals("null")) {
            toReturn = null;
            return toReturn;
        }
        if (Integer.class.isAssignableFrom(clazz)) {
            toReturn = Integer.parseInt(value);
        } else if (BigInteger.class.isAssignableFrom(clazz)) {
            toReturn = new BigInteger(value);
        } else if (BigDecimal.class.isAssignableFrom(clazz)) {
            toReturn = new BigDecimal(value);
        } else if (Long.class.isAssignableFrom(clazz)) {
            toReturn = new Long(value);
        } else if (Short.class.isAssignableFrom(clazz)) {
            toReturn = new Short(value);
        } else if (Byte.class.isAssignableFrom(clazz)) {
            toReturn = new Byte(value);
        } else {
            toReturn = new Float(value);
        }
    } else if (String.class.isAssignableFrom(clazz)) {
        toReturn = value;
    } else if (Timestamp.class.isAssignableFrom(clazz)) {
        // TODO manage dates
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        try {
            toReturn = sdf.parse(value);
        } catch (ParseException e) {
            logger.error("Unparsable timestamp", e);
        }

    } else if (Date.class.isAssignableFrom(clazz)) {
        // TODO manage dates
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        try {
            toReturn = sdf.parse(value);
        } catch (ParseException e) {
            logger.error("Unparsable date", e);
        }

    } else if (Boolean.class.isAssignableFrom(clazz)) {
        toReturn = Boolean.parseBoolean(value);
    } else {
        toReturn = value;
    }

    return toReturn;
}

From source file:it.eng.qbe.model.structure.builder.hibernate.HibernateModelStructureBuilder.java

License:Mozilla Public License

public List addNormalFields(IModelEntity dataMartEntity) {

    ClassMetadata classMetadata;// ww w . j  a  va 2s  .  co m
    PersistentClass classMapping;
    String[] propertyNames;
    Property property;
    Type propertyType;

    classMetadata = getDataSource().getHibernateSessionFactory().getClassMetadata(dataMartEntity.getType());
    classMapping = getDataSource().getHibernateConfiguration().getClassMapping(dataMartEntity.getType());
    propertyNames = classMetadata.getPropertyNames();

    List subEntities = new ArrayList();
    String propertyName = null;

    for (int i = 0; i < propertyNames.length; i++) {

        property = classMapping.getProperty(propertyNames[i]);

        // TEST if they are the same: if so use the first invocation
        propertyType = property.getType();

        Iterator columnIterator = property.getColumnIterator();
        Column column;

        if (propertyType instanceof ManyToOneType) { // chiave esterna

            ManyToOneType manyToOnePropertyType = (ManyToOneType) propertyType;
            String entityType = manyToOnePropertyType.getAssociatedEntityName();

            String columnName = null;
            if (columnIterator.hasNext()) {
                column = (Column) columnIterator.next();
                columnName = column.getName(); // ????
            }

            propertyName = propertyNames[i];

            //String entityName = getEntityNameFromEntityType(entityType);
            String entityName = propertyName;
            IModelEntity subentity = new ModelEntity(entityName, null, columnName, entityType,
                    dataMartEntity.getStructure());
            subEntities.add(subentity);

        } else if (propertyType instanceof CollectionType) { // chiave interna

        } else { // normal field
            propertyName = propertyNames[i];

            String type = propertyType.getName();
            int scale = 0;
            int precision = 0;

            if (columnIterator.hasNext()) {
                column = (Column) columnIterator.next();
                scale = column.getScale();
                precision = column.getPrecision();
            }

            IModelField datamartField = dataMartEntity.addNormalField(propertyName);
            datamartField.setType(type);
            datamartField.setPrecision(precision);
            datamartField.setLength(scale);
            propertiesInitializer.addProperties(datamartField);
        }
    }

    return subEntities;
}