Search in sources :

Example 21 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class CascadeTest method testManyToOnePropertyRefGeneratedIds.

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();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) Session(org.hibernate.Session)

Example 22 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class CascadeTest method testManyToOneGeneratedIds.

public void testManyToOneGeneratedIds() {
    // 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();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) Session(org.hibernate.Session)

Example 23 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class CascadeTest method testManyToOneAssignedIds.

public void testManyToOneAssignedIds() {
    // 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();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) Session(org.hibernate.Session)

Example 24 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class ForeignGenerator method generate.

@Override
public Serializable generate(SharedSessionContractImplementor sessionImplementor, Object object) {
    // needs to be a Session for the #save and #contains calls below...
    final Session session = (Session) sessionImplementor;
    final EntityPersister persister = sessionImplementor.getFactory().getMetamodel().entityPersister(entityName);
    Object associatedObject = persister.getPropertyValue(object, propertyName);
    if (associatedObject == null) {
        throw new IdentifierGenerationException("attempted to assign id from null one-to-one property [" + getRole() + "]");
    }
    final EntityType foreignValueSourceType;
    final Type propertyType = persister.getPropertyType(propertyName);
    if (propertyType.isEntityType()) {
        // the normal case
        foreignValueSourceType = (EntityType) propertyType;
    } else {
        // try identifier mapper
        foreignValueSourceType = (EntityType) persister.getPropertyType(PropertyPath.IDENTIFIER_MAPPER_PROPERTY + "." + propertyName);
    }
    Serializable id;
    try {
        id = ForeignKeys.getEntityIdentifierIfNotUnsaved(foreignValueSourceType.getAssociatedEntityName(), associatedObject, sessionImplementor);
    } catch (TransientObjectException toe) {
        id = session.save(foreignValueSourceType.getAssociatedEntityName(), associatedObject);
    }
    if (session.contains(entityName, object)) {
        //abort the save (the object is already saved by a circular cascade)
        return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
    //throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
    }
    return id;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityType(org.hibernate.type.EntityType) TransientObjectException(org.hibernate.TransientObjectException) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) Serializable(java.io.Serializable) Session(org.hibernate.Session)

Example 25 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method exists.

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);
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

TransientObjectException (org.hibernate.TransientObjectException)27 Session (org.hibernate.Session)16 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 Test (org.junit.Test)8 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Serializable (java.io.Serializable)4 EntityPersister (org.hibernate.persister.entity.EntityPersister)4 SQLException (java.sql.SQLException)2 HibernateException (org.hibernate.HibernateException)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 EventSource (org.hibernate.event.spi.EventSource)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 NamingException (javax.naming.NamingException)1 EntityExistsException (javax.persistence.EntityExistsException)1 EntityManager (javax.persistence.EntityManager)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 LockTimeoutException (javax.persistence.LockTimeoutException)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1