Type propType = metadata.getPropertyType(propName); if (propType instanceof StringType || propType instanceof TextType) { String propertyValue = (String) metadata.getPropertyValue(object, propName); if (propertyValue != null) { int maxLength = getMaximumPropertyLength(entityClass, propName);
/** * uses the {@link ClassMetadata} for this {@link Locks} tp retrieve the * component value. */ public Object getSubtypeValue(int i, int j, Object o) { return cm.getPropertyValue(o, subnames[i][j], EntityMode.POJO); }
@Override public Entity getChild(String name) throws LayerException { Object child = (object == null ? null : metadata.getPropertyValue(object, name, EntityMode.POJO)); return child == null ? null : new HibernateEntity(child); }
@Override public Object getPropertyValue(final Object object, final String propertyName) throws HibernateException { return PropertyValueAdapter.adaptToV2(metadata.getPropertyValue(object, propertyName)); }
@Override public Object getAttribute(String name) throws LayerException { if (metadata.getIdentifierPropertyName().equals(name)) { return getId(name); } return object == null ? null : metadata.getPropertyValue(object, name, EntityMode.POJO); }
public Object getPropertyValue(Object object, String property) { if (getIdProperty().equals(property)) return getIdValue(object); else return metadata.getPropertyValue(object, property); }
/** * <p>Parse the {@link DataAccessException} to see if special problems were * encountered while performing the query. See issue NMS-5029 for examples of * stack traces that can be thrown from these calls.</p> * {@see http://issues.opennms.org/browse/NMS-5029} */ private void logExtraSaveOrUpdateExceptionInformation(final T entity, final DataAccessException e) { Throwable cause = e; while (cause.getCause() != null) { if (cause.getMessage() != null) { if (cause.getMessage().contains("duplicate key value violates unique constraint")) { final ClassMetadata meta = getSessionFactory().getClassMetadata(m_entityClass); LOG.warn("Duplicate key constraint violation, class: {}, key value: {}", m_entityClass.getName(), meta.getPropertyValue(entity, meta.getIdentifierPropertyName(), EntityMode.POJO)); break; } else if (cause.getMessage().contains("given object has a null identifier")) { LOG.warn("Null identifier on object, class: {}: {}", m_entityClass.getName(), entity.toString()); break; } } cause = cause.getCause(); } } }
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; } }
/** * 获取对象属性值 * * @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; }
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 } }
metadata.getPropertyValue(value, property)));
@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); } }
/** * @see org.hibernate.id.IdentifierGenerator#generate(org.hibernate.engine.SessionImplementor, java.lang.Object) */ public Serializable generate(SessionImplementor session, Object object) throws HibernateException { Object associatedObject = session.getFactory() .getClassMetadata( entityName ) .getPropertyValue( object, propertyName, session.getEntityMode() ); if (associatedObject==null) throw new IdentifierGenerationException( "attempted to assign id from null one-to-one property: " + propertyName ); Serializable id; try { id = ForeignKeys.getEntityIdentifierIfNotUnsaved(null, associatedObject, session); //TODO: use associated entity name } catch (TransientObjectException toe) { id = session.save(associatedObject); } if ( session.contains(object) ) { //abort the save (the object is already saved by a circular cascade) return IdentifierGeneratorFactory.SHORT_CIRCUIT_INDICATOR; //throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association"); } return id; }
.getPropertyValue( object, propertyName, session.getEntityMode() );