Search in sources :

Example 66 with SessionImplementor

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();
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 67 with SessionImplementor

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();
}
Also used : Transaction(org.hibernate.Transaction) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 68 with SessionImplementor

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();
}
Also used : Transaction(org.hibernate.Transaction) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 69 with SessionImplementor

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();
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) TransactionException(org.hibernate.TransactionException) LazyInitializationException(org.hibernate.LazyInitializationException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with SessionImplementor

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();
    }
}
Also used : Field(java.lang.reflect.Field) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) BatchingBatch(org.hibernate.engine.jdbc.batch.internal.BatchingBatch) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)82 Session (org.hibernate.Session)59 Test (org.junit.Test)54 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 EntityEntry (org.hibernate.engine.spi.EntityEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)6