类org.hibernate.TransientObjectException源码实例Demo

下面列出了怎么用org.hibernate.TransientObjectException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: lams   文件: ForeignKeys.java
/**
 * Return the identifier of the persistent or transient object, or throw
 * an exception if the instance is "unsaved"
 * <p/>
 * Used by OneToOneType and ManyToOneType to determine what id value should
 * be used for an object that may or may not be associated with the session.
 * This does a "best guess" using any/all info available to use (not just the
 * EntityEntry).
 *
 * @param entityName The name of the entity
 * @param object The entity instance
 * @param session The session
 *
 * @return The identifier
 *
 * @throws TransientObjectException if the entity is transient (does not yet have an identifier)
 */
public static Serializable getEntityIdentifierIfNotUnsaved(
		final String entityName,
		final Object object,
		final SharedSessionContractImplementor session) throws TransientObjectException {
	if ( object == null ) {
		return null;
	}
	else {
		Serializable id = session.getContextEntityIdentifier( object );
		if ( id == null ) {
			// context-entity-identifier returns null explicitly if the entity
			// is not associated with the persistence context; so make some
			// deeper checks...
			if ( isTransient( entityName, object, Boolean.FALSE, session ) ) {
				throw new TransientObjectException(
						"object references an unsaved transient instance - save the transient instance before flushing: " +
								(entityName == null ? session.guessEntityName( object ) : entityName)
				);
			}
			id = session.getEntityPersister( entityName, object ).getIdentifier( object, session );
		}
		return id;
	}
}
 
源代码2 项目: lams   文件: SessionImpl.java
@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
	checkOpen();
	checkTransactionSynchStatus();
	if ( object == null ) {
		throw new NullPointerException( "null object passed to getCurrentLockMode()" );
	}
	if ( object instanceof HibernateProxy ) {
		object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation( this );
		if ( object == null ) {
			return LockMode.NONE;
		}
	}
	EntityEntry e = persistenceContext.getEntry( object );
	if ( e == null ) {
		throw new TransientObjectException( "Given object not associated with the session" );
	}
	if ( e.getStatus() != Status.MANAGED ) {
		throw new ObjectDeletedException(
				"The given object was deleted",
				e.getId(),
				e.getPersister().getEntityName()
		);
	}
	return e.getLockMode();
}
 
源代码3 项目: lams   文件: SessionImpl.java
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
	checkOpen();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.getSession() != this ) {
			throw new TransientObjectException( "The proxy was not associated with this session" );
		}
		return li.getIdentifier();
	}
	else {
		EntityEntry entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throw new TransientObjectException( "The instance was not associated with this session" );
		}
		return entry.getId();
	}
}
 
源代码4 项目: lams   文件: SessionImpl.java
@Override
	public String getEntityName(Object object) {
		checkOpen();
//		checkTransactionSynchStatus();
		if ( object instanceof HibernateProxy ) {
			if ( !persistenceContext.containsProxy( object ) ) {
				throw new TransientObjectException( "proxy was not associated with the session" );
			}
			object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
		}

		EntityEntry entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throwTransientObjectException( object );
		}
		return entry.getPersister().getEntityName();
	}
 
源代码5 项目: lams   文件: DefaultSaveOrUpdateEventListener.java
/**
 * Determine the id to use for updating.
 *
 * @param entity The entity.
 * @param persister The entity persister
 * @param requestedId The requested identifier
 * @param session The session
 *
 * @return The id.
 *
 * @throws TransientObjectException If the entity is considered transient.
 */
protected Serializable getUpdateId(
		Object entity,
		EntityPersister persister,
		Serializable requestedId,
		SessionImplementor session) {
	// use the id assigned to the instance
	Serializable id = persister.getIdentifier( entity, session );
	if ( id == null ) {
		// assume this is a newly instantiated transient object
		// which should be saved rather than updated
		throw new TransientObjectException(
				"The given object has a null identifier: " +
						persister.getEntityName()
		);
	}
	else {
		return id;
	}

}
 
源代码6 项目: cacheonix-core   文件: ForeignKeys.java
/**
 * Return the identifier of the persistent or transient object, or throw
 * an exception if the instance is "unsaved"
 *
 * Used by OneToOneType and ManyToOneType to determine what id value should 
 * be used for an object that may or may not be associated with the session. 
 * This does a "best guess" using any/all info available to use (not just the 
 * EntityEntry).
 */
public static Serializable getEntityIdentifierIfNotUnsaved(
		final String entityName, 
		final Object object, 
		final SessionImplementor session) 
throws HibernateException {
	if ( object == null ) {
		return null;
	}
	else {
		Serializable id = session.getContextEntityIdentifier( object );
		if ( id == null ) {
			// context-entity-identifier returns null explicitly if the entity
			// is not associated with the persistence context; so make some
			// deeper checks...
			if ( isTransient(entityName, object, Boolean.FALSE, session) ) {
				throw new TransientObjectException(
						"object references an unsaved transient instance - save the transient instance before flushing: " +
						(entityName == null ? session.guessEntityName( object ) : entityName)
				);
			}
			id = session.getEntityPersister( entityName, object ).getIdentifier( object, session.getEntityMode() );
		}
		return id;
	}
}
 
/**
 * Determine the id to use for updating.
 *
 * @param entity The entity.
 * @param persister The entity persister
 * @param requestedId The requested identifier
 * @param entityMode The entity mode.
 *
 * @return The id.
 *
 * @throws TransientObjectException If the entity is considered transient.
 */
protected Serializable getUpdateId(
		Object entity,
		EntityPersister persister,
		Serializable requestedId,
		EntityMode entityMode) {
	// use the id assigned to the instance
	Serializable id = persister.getIdentifier( entity, entityMode );
	if ( id == null ) {
		// assume this is a newly instantiated transient object
		// which should be saved rather than updated
		throw new TransientObjectException(
				"The given object has a null identifier: " +
						persister.getEntityName()
		);
	}
	else {
		return id;
	}

}
 
源代码8 项目: cacheonix-core   文件: SessionImpl.java
public LockMode getCurrentLockMode(Object object) throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	if ( object == null ) {
		throw new NullPointerException( "null object passed to getCurrentLockMode()" );
	}
	if ( object instanceof HibernateProxy ) {
		object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation(this);
		if ( object == null ) {
			return LockMode.NONE;
		}
	}
	EntityEntry e = persistenceContext.getEntry(object);
	if ( e == null ) {
		throw new TransientObjectException( "Given object not associated with the session" );
	}
	if ( e.getStatus() != Status.MANAGED ) {
		throw new ObjectDeletedException( 
				"The given object was deleted", 
				e.getId(), 
				e.getPersister().getEntityName() 
			);
	}
	return e.getLockMode();
}
 
源代码9 项目: cacheonix-core   文件: SessionImpl.java
public Serializable getIdentifier(Object object) throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.getSession() != this ) {
			throw new TransientObjectException( "The proxy was not associated with this session" );
		}
		return li.getIdentifier();
	}
	else {
		EntityEntry entry = persistenceContext.getEntry(object);
		if ( entry == null ) {
			throw new TransientObjectException( "The instance was not associated with this session" );
		}
		return entry.getId();
	}
}
 
源代码10 项目: cacheonix-core   文件: SessionImpl.java
public String getEntityName(Object object) {
	errorIfClosed();
	checkTransactionSynchStatus();
	if (object instanceof HibernateProxy) {
		if ( !persistenceContext.containsProxy( object ) ) {
			throw new TransientObjectException("proxy was not associated with the session");
		}
		object = ( (HibernateProxy) object ).getHibernateLazyInitializer().getImplementation();
	}

	EntityEntry entry = persistenceContext.getEntry(object);
	if ( entry == null ) {
		throwTransientObjectException( object );
	}
	return entry.getPersister().getEntityName();
}
 
源代码11 项目: cacheonix-core   文件: CascadeTest.java
public void testOneToOneGeneratedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		Parent p = new Parent( "parent" );
		ParentInfo info = new ParentInfo( "xyz" );
		p.setInfo( info );
		info.setOwner( p );
		s.persist( p );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码12 项目: cacheonix-core   文件: CascadeTest.java
public void testOneToOneAssignedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
		ParentInfoAssigned info = new ParentInfoAssigned( "something secret" );
		p.setInfo( info );
		info.setOwner( p );
		s.persist( p );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码13 项目: cacheonix-core   文件: CascadeTest.java
public void testManyToOnePropertyRefGeneratedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		Parent p = new Parent( "parent" );
		Other other = new Other();
		other.setOwner( p );
		s.persist( other );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码14 项目: cacheonix-core   文件: CascadeTest.java
public void testManyToOnePropertyRefAssignedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" );
		OtherAssigned other = new OtherAssigned( new Long( 2 ) );
		other.setOwner( p );
		s.persist( other );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码15 项目: cacheonix-core   文件: CascadeTest.java
public void testOneToOnePropertyRefGeneratedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		Child c2 = new Child( "c2" );
		ChildInfo info = new ChildInfo( "blah blah blah" );
		c2.setInfo( info );
		info.setOwner( c2 );
		s.persist( c2 );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception : " + e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码16 项目: cacheonix-core   文件: CascadeTest.java
public void testOneToOnePropertyRefAssignedIds() {
	try {
		Session s = openSession();
		s.beginTransaction();
		ChildAssigned c2 = new ChildAssigned( new Long( 3 ), "c3" );
		ChildInfoAssigned info = new ChildInfoAssigned( new Long( 4 ), "blah blah blah" );
		c2.setInfo( info );
		info.setOwner( c2 );
		s.persist( c2 );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception : " + e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码17 项目: hibernate-reactive   文件: ForeignKeys.java
/**
 * Return the identifier of the persistent or transient object, or throw
 * an exception if the instance is "unsaved"
 * <p/>
 * Used by OneToOneType and ManyToOneType to determine what id value should
 * be used for an object that may or may not be associated with the session.
 * This does a "best guess" using any/all info available to use (not just the
 * EntityEntry).
 *
 * @param entityName The name of the entity
 * @param object The entity instance
 * @param session The session
 *
 * @return The identifier
 *
 * @throws TransientObjectException if the entity is transient (does not yet have an identifier)
 */
public static CompletionStage<Serializable> getEntityIdentifierIfNotUnsaved(
		final String entityName,
		final Object object,
		final SessionImplementor session) throws TransientObjectException {
	if ( object == null ) {
		return CompletionStages.nullFuture();
	}
	else {
		Serializable id = session.getContextEntityIdentifier( object );
		if ( id == null ) {
			// context-entity-identifier returns null explicitly if the entity
			// is not associated with the persistence context; so make some
			// deeper checks...
			return isTransient( entityName, object, Boolean.FALSE, session )
					.thenApply( trans -> {
						if ( trans ) {
							throw new TransientObjectException(
									"object references an unsaved transient instance - save the transient instance before flushing: " +
											(entityName == null ? session.guessEntityName( object ) : entityName)
							);
						}
						return session.getEntityPersister( entityName, object )
								.getIdentifier( object, session );
					} );
		}
		else {
			return CompletionStages.completedFuture( id );
		}
	}
}
 
源代码18 项目: lams   文件: AbstractCollectionPersister.java
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SharedSessionContractImplementor session) {
	try {
		PreparedStatement st = session
				.getJdbcCoordinator()
				.getStatementPreparer()
				.prepareStatement( sql );
		try {
			getKeyType().nullSafeSet( st, key, 1, session );
			indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
			ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( st );
			try {
				return rs.next();
			}
			finally {
				session.getJdbcCoordinator().getResourceRegistry().release( rs, st );
			}
		}
		catch ( TransientObjectException e ) {
			return false;
		}
		finally {
			session.getJdbcCoordinator().getResourceRegistry().release( st );
			session.getJdbcCoordinator().afterStatementExecution();
		}
	}
	catch ( SQLException sqle ) {
		throw getSQLExceptionHelper().convert(
				sqle,
				"could not check row existence: " +
						MessageHelper.collectionInfoString( this, key, getFactory() ),
				sqlSelectSizeString
		);
	}
}
 
源代码19 项目: lams   文件: StatefulPersistenceContext.java
private void setEntityReadOnly(Object entity, boolean readOnly) {
	final EntityEntry entry = getEntry( entity );
	if ( entry == null ) {
		throw new TransientObjectException( "Instance was not associated with this persistence context" );
	}
	entry.setReadOnly( readOnly, entity );
	hasNonReadOnlyEntities = hasNonReadOnlyEntities || ! readOnly;
}
 
源代码20 项目: lams   文件: AbstractLazyInitializer.java
private void errorIfReadOnlySettingNotAvailable() {
	if ( session == null ) {
		throw new TransientObjectException(
				"Proxy [" + entityName + "#" + id + "] is detached (i.e, session is null). The read-only/modifiable setting is only accessible when the proxy is associated with an open session."
		);
	}
	if ( session.isClosed() ) {
		throw new SessionException(
				"Session is closed. The read-only/modifiable setting is only accessible when the proxy [" + entityName + "#" + id + "] is associated with an open session."
		);
	}
}
 
源代码21 项目: lams   文件: DefaultLockEventListener.java
/**
 * Handle the given lock event.
 *
 * @param event The lock event to be handled.
 * @throws HibernateException
 */
public void onLock(LockEvent event) throws HibernateException {

	if ( event.getObject() == null ) {
		throw new NullPointerException( "attempted to lock null" );
	}

	if ( event.getLockMode() == LockMode.WRITE ) {
		throw new HibernateException( "Invalid lock mode for lock()" );
	}

	if ( event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED ) {
		LOG.explicitSkipLockedLockCombo();
	}

	SessionImplementor source = event.getSession();
	
	Object entity = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );
	//TODO: if object was an uninitialized proxy, this is inefficient,
	//      resulting in two SQL selects
	
	EntityEntry entry = source.getPersistenceContext().getEntry(entity);
	if (entry==null) {
		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
		final Serializable id = persister.getIdentifier( entity, source );
		if ( !ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source ) ) {
			throw new TransientObjectException(
					"cannot lock an unsaved transient instance: " +
					persister.getEntityName()
			);
		}

		entry = reassociate(event, entity, id, persister);
		cascadeOnLock(event, persister, entity);
	}

	upgradeLock( entity, entry, event.getLockOptions(), event.getSession() );
}
 
源代码22 项目: lams   文件: AnyType.java
private Serializable getIdentifier(Object value, SharedSessionContractImplementor session) throws HibernateException {
	try {
		return ForeignKeys.getEntityIdentifierIfNotUnsaved(
				session.bestGuessEntityName( value ),
				value,
				session
		);
	}
	catch (TransientObjectException toe) {
		return null;
	}
}
 
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SessionImplementor session) {
	try {
		PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
		try {
			getKeyType().nullSafeSet(st, key, 1, session);
			indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
			ResultSet rs = st.executeQuery();
			try {
				return rs.next();
			}
			finally {
				rs.close();
			}
		}
		catch( TransientObjectException e ) {
			return false;
		}
		finally {
			session.getBatcher().closeStatement( st );
		}
	}
	catch (SQLException sqle) {
		throw JDBCExceptionHelper.convert(
				getFactory().getSQLExceptionConverter(),
				sqle,
				"could not check row existence: " + 
				MessageHelper.collectionInfoString( this, key, getFactory() ),
				sqlSelectSizeString
			);
	}
}
 
public void setReadOnly(Object entity, boolean readOnly) {
	EntityEntry entry = getEntry(entity);
	if (entry==null) {
		throw new TransientObjectException("Instance was not associated with the session");
	}
	entry.setReadOnly(readOnly, entity);
	hasNonReadOnlyEntities = hasNonReadOnlyEntities || !readOnly;
}
 
源代码25 项目: cacheonix-core   文件: CascadingAction.java
public void noCascade(
		EventSource session,
		Object child,
		Object parent,
		EntityPersister persister,
		int propertyIndex) {
	if ( child == null ) {
		return;
	}
	Type type = persister.getPropertyTypes()[propertyIndex];
	if ( type.isEntityType() ) {
		String childEntityName = ( ( EntityType ) type ).getAssociatedEntityName( session.getFactory() );

		if ( ! isInManagedState( child, session )
				&& ! ( child instanceof HibernateProxy ) //a proxy cannot be transient and it breaks ForeignKeys.isTransient
				&& ForeignKeys.isTransient( childEntityName, child, null, session ) ) {
			String parentEntiytName = persister.getEntityName();
			String propertyName = persister.getPropertyNames()[propertyIndex];
			throw new TransientObjectException(
					"object references an unsaved transient instance - " +
					"save the transient instance before flushing: " +
					parentEntiytName + "." + propertyName + " -> " + childEntityName
			);

		}
	}
}
 
源代码26 项目: cacheonix-core   文件: ForeignGenerator.java
/**
 * @see org.hibernate.id.IdentifierGenerator#generate(org.hibernate.engine.SessionImplementor, java.lang.Object)
 */
public Serializable generate(SessionImplementor sessionImplementor, Object object)
throws HibernateException {
	
	Session session = (Session) sessionImplementor;

	Object associatedObject = sessionImplementor.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
			);
	}
	
	EntityType type = (EntityType) sessionImplementor.getFactory()
       	.getClassMetadata( entityName )
       	.getPropertyType( propertyName );

	Serializable id;
	try {
		id = ForeignKeys.getEntityIdentifierIfNotUnsaved(
				type.getAssociatedEntityName(), 
				associatedObject, 
				sessionImplementor
			); 
	}
	catch (TransientObjectException toe) {
		id = session.save( type.getAssociatedEntityName(), 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;
}
 
源代码27 项目: cacheonix-core   文件: DefaultLockEventListener.java
/** Handle the given lock event.
 *
 * @param event The lock event to be handled.
 * @throws HibernateException
 */
public void onLock(LockEvent event) throws HibernateException {

	if ( event.getObject() == null ) {
		throw new NullPointerException( "attempted to lock null" );
	}

	if ( event.getLockMode() == LockMode.WRITE ) {
		throw new HibernateException( "Invalid lock mode for lock()" );
	}

	SessionImplementor source = event.getSession();
	
	Object entity = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );
	//TODO: if object was an uninitialized proxy, this is inefficient,
	//      resulting in two SQL selects
	
	EntityEntry entry = source.getPersistenceContext().getEntry(entity);
	if (entry==null) {
		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
		final Serializable id = persister.getIdentifier( entity, source.getEntityMode() );
		if ( !ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source ) ) {
			throw new TransientObjectException(
					"cannot lock an unsaved transient instance: " +
					persister.getEntityName()
			);
		}

		entry = reassociate(event, entity, id, persister);
		
		cascadeOnLock(event, persister, entity);
	}

	upgradeLock( entity, entry, event.getLockMode(), source );
}
 
源代码28 项目: cacheonix-core   文件: AnyType.java
private Serializable getIdentifier(Object value, SessionImplementor session) throws HibernateException {
	try {
		return ForeignKeys.getEntityIdentifierIfNotUnsaved( session.bestGuessEntityName(value), value, session );
	}
	catch (TransientObjectException toe) {
		return null;
	}
}
 
源代码29 项目: cacheonix-core   文件: CascadeTest.java
public void testManyToOneGeneratedIdsOnSave() {
	// NOTES: Child defines a many-to-one back to its Parent.  This
	// association does not define persist cascading (which is natural;
	// a child should not be able to create its parent).
	try {
		Session s = openSession();
		s.beginTransaction();
		Parent p = new Parent( "parent" );
		Child c = new Child( "child" );
		c.setParent( p );
		s.save( c );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
源代码30 项目: cacheonix-core   文件: CascadeTest.java
public void testManyToOneGeneratedIds() {
	// NOTES: Child defines a many-to-one back to its Parent.  This
	// association does not define persist cascading (which is natural;
	// a child should not be able to create its parent).
	try {
		Session s = openSession();
		s.beginTransaction();
		Parent p = new Parent( "parent" );
		Child c = new Child( "child" );
		c.setParent( p );
		s.persist( c );
		try {
			s.getTransaction().commit();
			fail( "expecting TransientObjectException on flush" );
		}
		catch( TransientObjectException e ) {
			// expected result
			log.trace( "handled expected exception", e );
			s.getTransaction().rollback();
		}
		finally {
			s.close();
		}
	}
	finally {
		cleanupData();
	}
}
 
 类所在包
 类方法
 同包方法