Java Code Examples for org.hibernate.persister.entity.EntityPersister#hasIdentifierProperty()

The following examples show how to use org.hibernate.persister.entity.EntityPersister#hasIdentifierProperty() . 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: EntityPrinter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Renders an entity to a string.
 *
 * @param entityName the entity name
 * @param entity an actual entity object, not a proxy!
 *
 * @return the entity rendered to a string
 */
public String toString(String entityName, Object entity) throws HibernateException {
	EntityPersister entityPersister = factory.getEntityPersister( entityName );

	if ( entityPersister == null || !entityPersister.isInstance( entity ) ) {
		return entity.getClass().getName();
	}

	Map<String, String> result = new HashMap<String, String>();

	if ( entityPersister.hasIdentifierProperty() ) {
		result.put(
				entityPersister.getIdentifierPropertyName(),
				entityPersister.getIdentifierType().toLoggableString(
						entityPersister.getIdentifier( entity ),
						factory
				)
		);
	}

	Type[] types = entityPersister.getPropertyTypes();
	String[] names = entityPersister.getPropertyNames();
	Object[] values = entityPersister.getPropertyValues( entity );
	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 entityName + result.toString();
}
 
Example 2
Source File: DefaultMergeEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void entityIsTransient(MergeEvent event, Map copyCache) {

		LOG.trace( "Merging transient instance" );

		final Object entity = event.getEntity();
		final EventSource source = event.getSession();

		final String entityName = event.getEntityName();
		final EntityPersister persister = source.getEntityPersister( entityName, entity );

		final Serializable id = persister.hasIdentifierProperty() ?
				persister.getIdentifier( entity, source ) :
				null;
		if ( copyCache.containsKey( entity ) ) {
			persister.setIdentifier( copyCache.get( entity ), id, source );
		}
		else {
			( (MergeContext) copyCache ).put( entity, source.instantiate( persister, id ), true ); //before cascade!
		}
		final Object copy = copyCache.get( entity );

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
		super.cascadeBeforeSave( source, persister, entity, copyCache );
		copyValues( persister, entity, copy, source, copyCache, ForeignKeyDirection.FROM_PARENT );

		saveTransientEntity( copy, entityName, event.getRequestedId(), source, copyCache );

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		super.cascadeAfterSave( source, persister, entity, copyCache );
		copyValues( persister, entity, copy, source, copyCache, ForeignKeyDirection.TO_PARENT );

		event.setResult( copy );
	}
 
Example 3
Source File: EntityType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a loggable representation of an instance of the value mapped by this type.
 *
 * @param value The instance to be logged.
 * @param factory The session factory.
 *
 * @return The loggable string.
 *
 * @throws HibernateException Generally some form of resolution problem.
 */
@Override
public String toLoggableString(Object value, SessionFactoryImplementor factory) {
	if ( value == null ) {
		return "null";
	}

	final EntityPersister persister = getAssociatedEntityPersister( factory );
	if ( !persister.getEntityTuplizer().isInstance( value ) ) {
		// it should be the id type...
		if ( persister.getIdentifierType().getReturnedClass().isInstance( value ) ) {
			return associatedEntityName + "#" + value;
		}
	}

	final StringBuilder result = new StringBuilder().append( associatedEntityName );

	if ( persister.hasIdentifierProperty() ) {
		final Serializable id;
		if ( value instanceof HibernateProxy ) {
			HibernateProxy proxy = (HibernateProxy) value;
			id = proxy.getHibernateLazyInitializer().getIdentifier();
		}
		else {
			id = persister.getIdentifier( value );
		}

		result.append( '#' )
				.append( persister.getIdentifierType().toLoggableString( id, factory ) );
	}

	return result.toString();
}
 
Example 4
Source File: DefaultReactiveMergeEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext copyCache) {

		LOG.trace( "Merging transient instance" );

		final Object entity = event.getEntity();
		final EventSource session = event.getSession();

		final String entityName = event.getEntityName();
		final EntityPersister persister = session.getEntityPersister( entityName, entity );

		final Serializable id = persister.hasIdentifierProperty()
				? persister.getIdentifier( entity, session )
				: null;

		final Object copy;
		final Object existingCopy = copyCache.get( entity );
		if ( existingCopy != null ) {
			persister.setIdentifier( copyCache.get( entity ), id, session );
			copy = existingCopy;
		}
		else {
			copy = session.instantiate( persister, id );

			//before cascade!
			copyCache.put( entity, copy, true );
		}

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
		return super.cascadeBeforeSave( session, persister, entity, copyCache )
				.thenAccept( v -> copyValues( persister, entity, copy, session, copyCache, ForeignKeyDirection.FROM_PARENT ) )
				.thenCompose( v -> saveTransientEntity( copy, entityName, event.getRequestedId(), session, copyCache ) )
				.thenCompose( v -> super.cascadeAfterSave( session, persister, entity, copyCache ) )
				.thenAccept( v -> {
					copyValues(persister, entity, copy, session, copyCache, ForeignKeyDirection.TO_PARENT);

					event.setResult(copy);

					if (copy instanceof PersistentAttributeInterceptable) {
						final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) copy;
						final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
						if (interceptor == null) {
							persister.getBytecodeEnhancementMetadata().injectInterceptor(copy, id, session);
						}
					}
				});
	}
 
Example 5
Source File: DefaultMergeEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void entityIsTransient(MergeEvent event, Map copyCache) {
	
	log.trace("merging transient instance");
	
	final Object entity = event.getEntity();
	final EventSource source = event.getSession();

	final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
	final String entityName = persister.getEntityName();
	
	final Serializable id = persister.hasIdentifierProperty() ?
			persister.getIdentifier( entity, source.getEntityMode() ) :
	        null;
	
	final Object copy = persister.instantiate( id, source.getEntityMode() );  //TODO: should this be Session.instantiate(Persister, ...)?
	copyCache.put(entity, copy); //before cascade!
	
	// cascade first, so that all unsaved objects get their
	// copy created before we actually copy
	//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
	super.cascadeBeforeSave(source, persister, entity, copyCache);
	copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT);
	
	//this bit is only *really* absolutely necessary for handling 
	//requestedId, but is also good if we merge multiple object 
	//graphs, since it helps ensure uniqueness
	final Serializable requestedId = event.getRequestedId();
	if (requestedId==null) {
		saveWithGeneratedId( copy, entityName, copyCache, source, false );
	}
	else {
		saveWithRequestedId( copy, requestedId, entityName, copyCache, source );
	}
	
	// cascade first, so that all unsaved objects get their 
	// copy created before we actually copy
	super.cascadeAfterSave(source, persister, entity, copyCache);
	copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.FOREIGN_KEY_TO_PARENT);
	
	event.setResult(copy);

}