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();
}
}
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();
}
}
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();
}
}
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;
}
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);
}
}
Aggregations