@Override public boolean hasIdentifierProperty() { return metadata.hasIdentifierProperty(); }
/** * @param obj1 * @param obj2 * @param sf * @return */ private static boolean areEntitiesEqual(Object obj1, Object obj2, SessionFactory sf) { try { // compare the database identifier ClassMetadata clazz = sf.getClassMetadata(obj1.getClass()); if (clazz != null) { if (clazz.hasIdentifierProperty() == true) { if (clazz.getIdentifier(obj1/* , EntityMode.POJO */) .equals(clazz.getIdentifier(obj2/* , EntityMode.POJO */)) == true) { return true; } } } } catch (Exception ex) { log.error("Exception occured:" + ex, ex); } return obj1.equals(obj2); } }
/** * Use hibernate metadata to get the id value */ protected Serializable baseGetIdValue(Object object) { Class<?> type = findClass(object); Serializable idValue = null; ClassMetadata classmeta = getSessionFactory().getClassMetadata(type); if (classmeta != null) { if (classmeta.hasIdentifierProperty()) { idValue = classmeta.getIdentifier(object); } } else { throw new IllegalArgumentException("Could not get classmetadata for this object, it may not be persistent: " + object); } return idValue; }
/** * @param entity an actual entity object, not a proxy! */ public String toString(Object entity, EntityMode entityMode) throws HibernateException { // todo : this call will not work for anything other than pojos! ClassMetadata cm = factory.getClassMetadata( entity.getClass() ); if ( cm==null ) return entity.getClass().getName(); Map result = new HashMap(); if ( cm.hasIdentifierProperty() ) { result.put( cm.getIdentifierPropertyName(), cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory ) ); } Type[] types = cm.getPropertyTypes(); String[] names = cm.getPropertyNames(); Object[] values = cm.getPropertyValues( entity, entityMode ); for ( int i=0; i<types.length; i++ ) { if ( !names[i].startsWith("_") ) { String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ? values[i].toString() : types[i].toLoggableString( values[i], factory ); result.put( names[i], strValue ); } } return cm.getEntityName() + result.toString(); }
/** * @param entity an actual entity object, not a proxy! */ public String toString(Object entity, EntityMode entityMode) throws HibernateException { // todo : this call will not work for anything other than pojos! ClassMetadata cm = factory.getClassMetadata( entity.getClass() ); if ( cm==null ) return entity.getClass().getName(); Map result = new HashMap(); if ( cm.hasIdentifierProperty() ) { result.put( cm.getIdentifierPropertyName(), cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory ) ); } Type[] types = cm.getPropertyTypes(); String[] names = cm.getPropertyNames(); Object[] values = cm.getPropertyValues( entity, entityMode ); for ( int i=0; i<types.length; i++ ) { if ( !names[i].startsWith("_") ) { String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ? values[i].toString() : types[i].toLoggableString( values[i], factory ); result.put( names[i], strValue ); } } return cm.getEntityName() + result.toString(); }
/** * 获取模型属性类型 * * @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); }
/** * 根据实体类创建元数据 * @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; }