@Override public List<String> getPropertyNames(Class<?> entityClass, HibernateEntityManager entityManager) { entityClass = getNonProxyImplementationClassIfNecessary(entityClass); ClassMetadata metadata = getSessionFactory(entityManager).getClassMetadata(entityClass); List<String> propertyNames = new ArrayList<>(); Collections.addAll(propertyNames, metadata.getPropertyNames()); return propertyNames; }
ClassMetadata classMetadata = sessionFactory.getClassMetadata(AppTaskConfig.class); String[] propertyNames = classMetadata.getPropertyNames();
String[] names = classMetadata.getPropertyNames(); for (String name : names) { if (classMetadata.getPropertyType(name).getReturnedClass().equals(OrderFrequency.class)) {
ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass); if (metadata != null) { String[] propNames = metadata.getPropertyNames(); Object identifierType = metadata.getIdentifierType(); String identifierName = metadata.getIdentifierPropertyName();
private void scanClassMetadata(ClassMetadata meta) { Class<?> entityClass = meta.getMappedClass(); String[] names = meta.getPropertyNames(); Type[] types = meta.getPropertyTypes(); for (int n = 0; n < names.length; n++) { String fieldName = names[n]; Type type = types[n]; registerEntityField(entityClass, fieldName, type); } if (meta.getIdentifierPropertyName() != null) registerEntityField(entityClass, meta.getIdentifierPropertyName(), meta.getIdentifierType()); //System.out.println(names + " " + types); }
@Override public String[] getPropertyNames() { return metadata.getPropertyNames(); }
ClassMetadata classMetadata = sessionFactory.getClassMetadata(Person.class); String[] propertyNames = classMetadata.getPropertyNames();
private static boolean hasProperty(String property, ClassMetadata metadata) { List<String> properties = Arrays.asList(metadata.getPropertyNames()); return properties.contains(property); }
Collection clsMetaData = session.getSessionFactory() .getAllClassMetadata().values(); for (Iterator i = clsMetaData.iterator(); i.hasNext();) { ClassMetadata cmd = (ClassMetadata) i.next(); System.out.println("cmd" + cmd.getEntityName()); for (String s : cmd.getPropertyNames()) { System.out.println("prop:" + s); } }
ClassMetadata cmd = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); String[] propertyNames = cmd.getPropertyNames(); for (String name : propertyNames) { if (cmd.getPropertyType(name).isAssociationType()) { // association type } }
ClassMetadata meta = sessionFactory.getClassMetadata(entity.getClass()); String[] propertyNames = meta.getPropertyNames(); boolean[] propertyLaziness = meta.getPropertyLaziness(); for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; // Check if the property fetch type is Lazy if (propertyLaziness[i]) { System.out.println("Property : " + propertyName + " fetch type is Lazy"); } }
/** * 获取模型属性 * * @param sessionFactory 会话工厂 * @param model 数据模型 * @return 属性名称数组 */ public static String[] getProperties(SessionFactory sessionFactory, Class<?> model) { return getClassMetadata(sessionFactory, model).getPropertyNames(); }
public String[] getProperties() { String[] pn = metadata.getPropertyNames(); String[] result = new String[pn.length + 1]; result[0] = metadata.getIdentifierPropertyName(); for (int i = 0; i < pn.length; i++) { result[i+1] = pn[i]; } return result; }
class IntrospectClassMetadata extends BasicTransformerAdapter { PassThroughResultTransformer rt = PassThroughResultTransformer.INSTANCE; public Object transformTuple(Object[] tuple, String[] aliases) { final Object o = rt.transformTuple(tuple, aliases); ClassMetadata cm = sf.getClassMetadata(o.getClass()); List<String> pns = new ArrayList<String>(Arrays.asList(cm.getPropertyNames())); Map<String, Object> m = new HashMap<String, Object>(); for(String pn : pns) { m.put(pn, cm.getPropertyValue(o, pn)); } m.put(cm.getIdentifierPropertyName(), cm.getIdentifier(o)); return m; } }
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 ); }
@RolesAllowed("user") @Transactional(propagation = Propagation.SUPPORTS) public boolean checkProperty(String type, String property) { ClassMetadata meta = getHibernateTemplate().getSessionFactory() .getClassMetadata(type); String[] names = meta.getPropertyNames(); for (int i = 0; i < names.length; i++) { // TODO: possibly with caching and Arrays.sort/search if (names[i].equals(property)) { return true; } } return false; }
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()); } }
/** * Construct a entity collection. * * @param parentMetadata parent meta data * @param childMetadata child meta data * @param parent parent object * @param objects child objects */ public HibernateEntityCollection(ClassMetadata parentMetadata, ClassMetadata childMetadata, Object parent, Collection<?> objects) { this.objects = objects; int i = 0; for (Type type : childMetadata.getPropertyTypes()) { if (type instanceof ManyToOneType) { ManyToOneType mto = (ManyToOneType) type; if (mto.getAssociatedEntityName().equals(parentMetadata.getEntityName())) { parentName = childMetadata.getPropertyNames()[i]; } } i++; } this.metadata = childMetadata; this.parent = parent; }
/** * 根据实体类创建元数据 * @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; }
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; }