Java Code Examples for org.hibernate.metadata.ClassMetadata#getPropertyValues()

The following examples show how to use org.hibernate.metadata.ClassMetadata#getPropertyValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultReactiveFlushEntityEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean copyState(Object entity, Type[] types, Object[] state, SessionFactory sf) {
	// copy the entity state into the state array and return true if the state has changed
	ClassMetadata metadata = sf.getClassMetadata( entity.getClass() );
	Object[] newState = metadata.getPropertyValues( entity );
	int size = newState.length;
	boolean isDirty = false;
	for ( int index = 0; index < size; index++ ) {
		if ( ( state[index] == LazyPropertyInitializer.UNFETCHED_PROPERTY &&
				newState[index] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) ||
				( state[index] != newState[index] && !types[index].isEqual( state[index], newState[index] ) ) ) {
			isDirty = true;
			state[index] = newState[index];
		}
	}
	return isDirty;
}
 
Example 2
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private boolean copyState(Object entity, Type[] types, Object[] state, SessionFactory sf) {
	// copy the entity state into the state array and return true if the state has changed
	ClassMetadata metadata = sf.getClassMetadata( entity.getClass() );
	Object[] newState = metadata.getPropertyValues( entity );
	int size = newState.length;
	boolean isDirty = false;
	for ( int index = 0; index < size; index++ ) {
		if ( ( state[index] == LazyPropertyInitializer.UNFETCHED_PROPERTY &&
				newState[index] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) ||
				( state[index] != newState[index] && !types[index].isEqual( state[index], newState[index] ) ) ) {
			isDirty = true;
			state[index] = newState[index];
		}
	}
	return isDirty;
}
 
Example 3
Source File: Printer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @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();
}
 
Example 4
Source File: HibernateUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/** 
 * Initialize Object for use with closed Session. 
 * 
 * @param sessionFactory max depth in recursion
 * @param obj Object to initialize
 * @param initializedObjects list with already initialized Objects
 * @param depth max depth in recursion
 */
private static void initialize(SessionFactory sessionFactory, Object obj, 
		List<Object> initializedObjects, int depth) {
	
	// return on nulls, depth = 0 or already initialized objects
	if (obj == null || depth == 0) { 
		return; 
	}
	
	if (!Hibernate.isInitialized(obj)) {
		// if collection, initialize objects in collection too. Hibernate don't do it.
		if (obj instanceof Collection) {
			initializeCollection(sessionFactory, obj, initializedObjects,
					depth);
			return;
		}
		
		sessionFactory.getCurrentSession().buildLockRequest(LockOptions.NONE).lock(obj);
		Hibernate.initialize(obj);
	}

	// now we can call equals safely. If object are already initializated, return
	if (initializedObjects.contains(obj))
		return;
	
	initializedObjects.add(obj);
	
	// initialize all persistent associaciations.
	ClassMetadata classMetadata = getClassMetadata(sessionFactory, obj);
	
	if (classMetadata == null) {
		return; // Not persistent object
	}
	
	Object[] pvs = classMetadata.getPropertyValues(obj, EntityMode.POJO);
	
	for (Object pv : pvs) {
		initialize(sessionFactory, pv, initializedObjects, depth - 1);
	}
}