use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class OneToOneWithDerivedIdentityTest method testInsertFooAndBarWithDerivedIdPC.
@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testInsertFooAndBarWithDerivedIdPC() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails("Some details");
Foo foo = new Foo();
foo.setBar(bar);
bar.setFoo(foo);
s.persist(foo);
s.flush();
assertNotNull(foo.getId());
assertEquals(foo.getId(), bar.getFoo().getId());
s.clear();
Bar barWithFoo = new Bar();
barWithFoo.setFoo(foo);
barWithFoo.setDetails("wrong details");
bar = (Bar) s.get(Bar.class, barWithFoo);
assertSame(bar, barWithFoo);
assertEquals("Some details", bar.getDetails());
SessionImplementor si = (SessionImplementor) s;
assertTrue(si.getPersistenceContext().isEntryFor(bar));
assertFalse(si.getPersistenceContext().isEntryFor(bar.getFoo()));
s.getTransaction().rollback();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class CompositeIdTest method testManyToOneInCompositePkInPC.
@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testManyToOneInCompositePkInPC() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
ParentPk ppk = new ParentPk();
ppk.setFirstName("Emmanuel");
ppk.setLastName("Bernard");
Parent p = new Parent();
p.id = ppk;
s.persist(p);
ChildPk cpk = new ChildPk();
cpk.parent = p;
cpk.nthChild = 1;
Child c = new Child();
c.id = cpk;
s.persist(c);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Parent) s.get(Parent.class, ppk);
// p.id should be ppk.
assertSame(ppk, p.id);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
c = (Child) s.get(Child.class, cpk);
// c.id should be cpk
assertSame(cpk, c.id);
// only Child should be in PC (c.id.parent should not be in PC)
SessionImplementor sessionImplementor = (SessionImplementor) s;
assertTrue(sessionImplementor.getPersistenceContext().isEntryFor(c));
assertFalse(sessionImplementor.getPersistenceContext().isEntryFor(c.id.parent));
tx.commit();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class CompositeIdTest method testManyToOneInCompositeIdClassInPC.
@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testManyToOneInCompositeIdClassInPC() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Order order = new Order();
s.persist(order);
Product product = new Product();
product.name = "small car";
s.persist(product);
OrderLine orderLine = new OrderLine();
orderLine.order = order;
orderLine.product = product;
s.persist(orderLine);
s.flush();
s.clear();
s.clear();
OrderLinePk orderLinePK = new OrderLinePk();
orderLinePK.order = orderLine.order;
orderLinePK.product = orderLine.product;
orderLine = (OrderLine) s.get(OrderLine.class, orderLinePK);
assertTrue(orderLine.order != orderLinePK.order);
assertTrue(orderLine.product != orderLinePK.product);
SessionImplementor sessionImplementor = (SessionImplementor) s;
assertTrue(sessionImplementor.getPersistenceContext().isEntryFor(orderLine));
assertTrue(sessionImplementor.getPersistenceContext().isEntryFor(orderLine.order));
assertTrue(sessionImplementor.getPersistenceContext().isEntryFor(orderLine.product));
assertFalse(sessionImplementor.getPersistenceContext().isEntryFor(orderLinePK.order));
assertFalse(sessionImplementor.getPersistenceContext().isEntryFor(orderLinePK.product));
tx.rollback();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class FooBarTest method testLazyCollectionsTouchedDuringPreCommit.
@Test
@TestForIssue(jiraKey = "HHH-7603")
public void testLazyCollectionsTouchedDuringPreCommit() throws Exception {
Session s = openSession();
s.beginTransaction();
Qux q = new Qux();
s.save(q);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.getTransaction().commit();
//clear the session
s.clear();
//now reload the proxy and delete it
s.beginTransaction();
final Qux qToDelete = (Qux) s.load(Qux.class, q.getKey());
//register a pre commit process that will touch the collection and delete the entity
((EventSource) s).getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
qToDelete.getFums().size();
}
});
s.delete(qToDelete);
boolean ok = false;
try {
s.getTransaction().commit();
} catch (LazyInitializationException e) {
ok = true;
s.getTransaction().rollback();
} catch (TransactionException te) {
if (te.getCause() instanceof LazyInitializationException) {
ok = true;
}
s.getTransaction().rollback();
} finally {
s.close();
}
assertTrue("lazy collection should have blown in the beforeQuery trans completion", ok);
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.delete(q);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class BatchingBatchFailureTest method testBasicInsertion.
@Test
public void testBasicInsertion() {
Session session = openSession();
session.getTransaction().begin();
try {
session.persist(new User(1, "ok"));
session.persist(new User(2, null));
session.persist(new User(3, "ok"));
session.persist(new User(4, "ok"));
session.persist(new User(5, "ok"));
session.persist(new User(6, "ok"));
// the flush should fail
session.flush();
fail("Expecting failed flush");
} catch (Exception expected) {
System.out.println("Caught expected exception : " + expected);
expected.printStackTrace(System.out);
try {
//at this point the transaction is still active but the batch should have been aborted (have to use reflection to get at the field)
SessionImplementor sessionImplementor = (SessionImplementor) session;
Field field = sessionImplementor.getJdbcCoordinator().getClass().getDeclaredField("currentBatch");
field.setAccessible(true);
Batch batch = (Batch) field.get(sessionImplementor.getJdbcCoordinator());
if (batch == null) {
throw new Exception("Current batch was null");
} else {
//make sure it's actually a batching impl
assertEquals(BatchingBatch.class, batch.getClass());
field = AbstractBatchImpl.class.getDeclaredField("statements");
field.setAccessible(true);
//check to see that there aren't any statements queued up (this can be an issue if using SavePoints)
assertEquals(0, ((Map) field.get(batch)).size());
}
} catch (Exception fieldException) {
fail("Couldn't inspect field " + fieldException.getMessage());
}
} finally {
session.getTransaction().rollback();
session.close();
}
}
Aggregations