org.hibernate.TransientObjectException Java Examples
The following examples show how to use
org.hibernate.TransientObjectException.
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: DefaultSaveOrUpdateEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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; } }
Example #2
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #3
Source File: DefaultSaveOrUpdateEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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; } }
Example #4
Source File: SessionImpl.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example #5
Source File: SessionImpl.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #6
Source File: SessionImpl.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example #7
Source File: ForeignKeys.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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; } }
Example #8
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #9
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #10
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #11
Source File: SessionImpl.java From lams with GNU General Public License v2.0 | 6 votes |
@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(); }
Example #12
Source File: SessionImpl.java From lams with GNU General Public License v2.0 | 6 votes |
@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(); } }
Example #13
Source File: SessionImpl.java From lams with GNU General Public License v2.0 | 6 votes |
@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(); }
Example #14
Source File: ForeignKeys.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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; } }
Example #15
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #16
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); } }
Example #17
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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; }
Example #18
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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(); } }
Example #19
Source File: AnyType.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
private Serializable getIdentifier(Object value, SessionImplementor session) throws HibernateException { try { return ForeignKeys.getEntityIdentifierIfNotUnsaved( session.bestGuessEntityName(value), value, session ); } catch (TransientObjectException toe) { return null; } }
Example #20
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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(); } }
Example #21
Source File: CascadeTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testManyToOneAssignedIds() { // 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(); ParentAssigned p = new ParentAssigned( new Long( 1 ), "parent" ); ChildAssigned c = new ChildAssigned( new Long( 2 ), "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(); } }
Example #22
Source File: DefaultLockEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** 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 ); }
Example #23
Source File: ForeignKeys.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 ); } } }
Example #24
Source File: ForeignGenerator.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * @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; }
Example #25
Source File: CascadingAction.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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 ); } } }
Example #26
Source File: AbstractCollectionPersister.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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 ); } }
Example #27
Source File: AnyType.java From lams with GNU General Public License v2.0 | 5 votes |
private Serializable getIdentifier(Object value, SharedSessionContractImplementor session) throws HibernateException { try { return ForeignKeys.getEntityIdentifierIfNotUnsaved( session.bestGuessEntityName( value ), value, session ); } catch (TransientObjectException toe) { return null; } }
Example #28
Source File: DefaultLockEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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() ); }
Example #29
Source File: AbstractLazyInitializer.java From lams with GNU General Public License v2.0 | 5 votes |
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." ); } }
Example #30
Source File: StatefulPersistenceContext.java From lams with GNU General Public License v2.0 | 5 votes |
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; }