if (classMetadata.getPropertyType(name).getReturnedClass().equals(OrderFrequency.class)) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass); criteria.add(Restrictions.eq(name, orderFrequency));
Type propType = metadata.getPropertyType(propName); if (propType instanceof StringType || propType instanceof TextType) { String propertyValue = (String) metadata.getPropertyValue(object, propName);
ClassMetadata cmd = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); String[] propertyNames = cmd.getPropertyNames(); for (String name : propertyNames) { if (cmd.getPropertyType(name).isAssociationType()) { // association type } }
@Override public Type getPropertyType(final String propertyName) throws HibernateException { return TypeV2Adapter.adapt(metadata.getPropertyType(propertyName)); }
Class clazz = ...; SessionFactory sessionFactory = ...; ClassMetadata classMetadata = sessionFactory.getClassMetadata( clazz ); // bit of a hack, relies on knowing that SessionFactoryImpl implements Mapping Mapping mapping = (Mapping)sessionFactory; for ( String propertyName : classMetadata.getPropertyNames() ) { Type type = classMetadata.getPropertyType( propertyName ); if ( type.isCollectionType() ) continue; // TODO: do something with the result type.sqlTypes( mapping ); }
public String find(ClassMetadata metadata) throws FinderException { for (String prop : metadata.getPropertyNames()) { Type type = metadata.getPropertyType(prop); if (Geometry.class.isAssignableFrom(type.getReturnedClass())) { return prop; } } throw new FinderException( "Could not find a Geometry-valued property in " + metadata.getEntityName()); } }
/** * 获取对象属性值 * * @param sessionFactory 会话工厂 * @param object 对象实例 * @param property 属性名称 * @return 属性值 */ public static Object getValue(SessionFactory sessionFactory, Object object, String property) { if (object == null) { return null; } ClassMetadata metadata = getClassMetadata(sessionFactory, object.getClass()); Object value = metadata.getPropertyValue(object, property); if (value != null && !Hibernate.isInitialized(value)) { Type type = metadata.getPropertyType(property); return type.isCollectionType() ? new ArrayList<Object>(0) : null; } return value; }
/** * 获取模型属性类型 * * @param sessionFactory 会话工厂 * @param model 数据模型 * @param property 属性名称 * @return 类型对象 */ public static Type getPropertyType(SessionFactory sessionFactory, Class<?> model, String property) { int index = property.indexOf('.'); if (index > 0) { Type type = getPropertyType(sessionFactory, model, property.substring(0, index)); return getPropertyType(sessionFactory, getPropertyTypeClass(sessionFactory, type), property.substring(index + 1)); } ClassMetadata metadata = getClassMetadata(sessionFactory, model); if (metadata.hasIdentifierProperty() && metadata.getIdentifierPropertyName().equals(property)) { return metadata.getIdentifierType(); } return metadata.getPropertyType(property); }
public static boolean isPersistentProperty(SessionFactory sessionFactory, Class<?> clazz, String propertyPath) { int pos = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(propertyPath); // Handle nested properties recursively. if (pos > -1) { String nestedProperty = propertyPath.substring(0, pos); String nestedPath = propertyPath.substring(pos + 1); Class<?> nestedClazz = PropertyUtils.getPropertyType(clazz, nestedProperty); return isPersistentProperty(sessionFactory, clazz, nestedProperty) && isPersistentProperty(sessionFactory, nestedClazz, nestedPath); } else { try { ClassMetadata metadata = sessionFactory.getClassMetadata(clazz); if (metadata == null) { return false; } return metadata.getPropertyType(propertyPath) != null; } catch (HibernateException e) { return false; } } }
/** * 根据实体类创建元数据 * @param entityClazz */ @Transactional public boolean createMObjectFromEntityClass(Class entityClazz){ Session factorySession = (org.hibernate.Session) entityManager.getDelegate(); SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) factorySession.getSessionFactory(); ClassMetadata entityMetaInfo = sessionFactory.getClassMetadata(entityClazz); String[] propertyNames = entityMetaInfo.getPropertyNames(); for (int i = 0, n = propertyNames.length; i < n; i++) { String propertyName = propertyNames[i]; Type propType = entityMetaInfo.getPropertyType(propertyName);//propType.sqlTypes(idPropType);; System.out.println(propertyName + "字段类型为" + propType.getReturnedClass().getName()); } if (entityMetaInfo.hasIdentifierProperty()){ String idPropName = entityMetaInfo.getIdentifierPropertyName(); Type idPropType = entityMetaInfo.getIdentifierType(); System.out.println("主键字段为:" + idPropName + "类型为" + idPropType.getReturnedClass().getName()); } else { System.out.println("此实体无主键"); } return true; }
protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object object, PostInsertIdentityPersister persister) throws SQLException { Type uniqueKeyPropertyType = session.getSessionFactory() .getClassMetadata(entityName) .getPropertyType(uniqueKeyPropertyName); Object uniqueKeyValue = persister.getPropertyValue( object, uniqueKeyPropertyName, session.getEntityMode() ); uniqueKeyPropertyType.nullSafeSet( ps, uniqueKeyValue, 1, session ); }
public static References getReferencingClasses(SessionFactory sessionFactory, Class<?> clazz) { References references = new References(); for (Map.Entry<String, ClassMetadata> entry: sessionFactory.getAllClassMetadata().entrySet()) { Class<?> mappedClass = entry.getValue().getMappedClass(); for (String propertyName: entry.getValue().getPropertyNames()) { Type t = entry.getValue().getPropertyType(propertyName); if (t instanceof EntityType) { EntityType entityType=(EntityType) t; if (entityType.getAssociatedEntityName().equals(clazz.getName())) { references.addEntityReference(propertyName, mappedClass); } } else if (t.isCollectionType()) { CollectionType cType = (CollectionType) t; QueryableCollection collectionPersister = (QueryableCollection) ((SessionFactoryImplementor) sessionFactory).getCollectionPersister( cType.getRole()); if (collectionPersister.getElementType().isEntityType() && collectionPersister.getElementPersister().getEntityName().equals(clazz.getName())) { references.addCollectionReference(propertyName, mappedClass); } } } } return references; }
import org.hibernate.SessionFactory; import org.hibernate.metadata.ClassMetadata; import org.hibernate.type.CollectionType; import org.hibernate.type.Type; // you should already have these somewhere: SessionFactory sessionFactory = ... Session session = ... MyEntity myEntity = ... // this fetches all collections by inspecting the Hibernate Metadata. ClassMetadata classMetadata = sessionFactory.getClassMetadata(MyEntity.class); String[] propertyNames = classMetadata.getPropertyNames(); for (String name : propertyNames) { Object propertyValue = classMetadata.getPropertyValue(myEntity, name, EntityMode.POJO); Type propertyType = classMetadata.getPropertyType(name); if (propertyValue != null && propertyType instanceof CollectionType) { CollectionType s = (CollectionType) propertyType; s.getElementsIterator(propertyValue, session); // this triggers the loading of the data } }
private ComponentType buildComponentType(SessionFactory sessionFactory, String entityName, String propertyName) { EntityType entityType = (EntityType) entityTypes.get(entityName); if (null != entityType) { Type propertyType = (Type) entityType.getPropertyTypes().get(propertyName); if (null != propertyType) { return (ComponentType) propertyType; } } ClassMetadata cm = sessionFactory.getClassMetadata(entityName); org.hibernate.type.ComponentType hcType = (org.hibernate.type.ComponentType) cm .getPropertyType(propertyName); String[] propertyNames = hcType.getPropertyNames(); ComponentType cType = new ComponentType(hcType.getReturnedClass()); Map<String, Type> propertyTypes = cType.getPropertyTypes(); for (int j = 0; j < propertyNames.length; j++) { org.hibernate.type.Type type = cm.getPropertyType(propertyName + "." + propertyNames[j]); if (type.isEntityType()) { propertyTypes.put(propertyNames[j], buildEntityType(sessionFactory, type.getName())); } else if (type.isComponentType()) { propertyTypes.put(propertyNames[j], buildComponentType(sessionFactory, entityName, propertyName + "." + propertyNames[j])); } else if (type.isCollectionType()) { propertyTypes.put( propertyNames[j], buildCollectionType(sessionFactory, defaultCollectionClass(type), entityName + "." + propertyName + "." + propertyNames[j])); } } return cType; }
for (ClassMetadata meta : allMeta) { for (String name : meta.getPropertyNames()) { Type type = meta.getPropertyType(name); if (RiotFile.class.isAssignableFrom(type.getReturnedClass())) { fileQueries.add(String.format(
protected CollectionCacheEntry(Object owner, String field) { ClassMetadata ownerClassMetadata = factoryImplementor.getClassMetadata(owner.getClass()); Type t = ownerClassMetadata.getPropertyType(field); Assert.assertTrue(t instanceof CollectionType); CollectionType type = (CollectionType)t; role = type.getRole(); collectionPersister = factoryImplementor.getCollectionPersister(role); if(collectionPersister.hasCache()) { cacheRegion = collectionPersister.getCacheAccessStrategy().getRegion(); cacheAccess = cacheRegion.buildAccessStrategy(AccessType.READ_ONLY); } else { cacheRegion = null; cacheAccess = null; } persistentCollection = (PersistentCollection) factoryImplementor.getEntityPersister(ownerClassMetadata.getEntityName()).getPropertyValue(owner, field, entityMode); keyOfOwner = persistentCollection.getKey(); }
String[] ps = cm.getPropertyNames(); for (int i = 0; i < ps.length; i++) { org.hibernate.type.Type type = cm.getPropertyType(ps[i]); if (type.isEntityType()) { propertyTypes.put(ps[i], buildEntityType(sessionFactory, type.getName()));
public Metadata getPropertyType(String property) { Type pType = metadata.getPropertyType(property); Class<?> pCollectionType = null; if (pType.isCollectionType()) { pType = ((CollectionType)pType).getElementType((SessionFactoryImplementor) sessionFactory); pCollectionType = pType.getReturnedClass(); } if (pType.isEntityType()) { return new HibernateEntityMetadata(sessionFactory, sessionFactory.getClassMetadata(((EntityType)pType).getName()), pCollectionType); } else { return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType); } }
@Override public EntityCollection getChildCollection(String name) throws LayerException { Type type = metadata.getPropertyType(name); if (type instanceof CollectionType) { CollectionType ct = (CollectionType) type; Collection coll = (Collection) metadata.getPropertyValue(object, name, EntityMode.POJO); if (coll == null) { // normally should not happen, hibernate instantiates it automatically coll = (Collection) ct.instantiate(0); metadata.setPropertyValue(object, name, coll, EntityMode.POJO); } String entityName = ct.getAssociatedEntityName((SessionFactoryImplementor) sessionFactory); ClassMetadata childMetadata = sessionFactory.getClassMetadata(entityName); return new HibernateEntityCollection(metadata, childMetadata, object, coll); } else { throw new LayerException(ExceptionCode.FEATURE_MODEL_PROBLEM); } }
.getPropertyType( propertyName );