Search in sources :

Example 6 with SessionImpl

use of org.hibernate.internal.SessionImpl in project hibernate-orm by hibernate.

the class TestAutoFlushBeforeQueryExecution method testAutoflushIsRequired.

@Test
public void testAutoflushIsRequired() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Publisher publisher = new Publisher();
    publisher.setName("name");
    s.save(publisher);
    assertTrue("autoflush entity create", s.createQuery("from Publisher p").list().size() == 1);
    publisher.setName("name");
    assertTrue("autoflush entity update", s.createQuery("from Publisher p where p.name='name'").list().size() == 1);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    publisher = (Publisher) s.get(Publisher.class, publisher.getId());
    assertTrue(publisher.getAuthors().isEmpty());
    final PersistenceContext persistenceContext = ((SessionImplementor) s).getPersistenceContext();
    final ActionQueue actionQueue = ((SessionImpl) s).getActionQueue();
    assertEquals(1, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    Author author1 = new Author();
    author1.setPublisher(publisher);
    publisher.getAuthors().add(author1);
    assertTrue("autoflush collection update", s.createQuery("select a from Publisher p join p.authors a").list().size() == 1);
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(2, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(author1.getBooks()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    author1.setPublisher(null);
    s.delete(author1);
    publisher.getAuthors().clear();
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    assertTrue("autoflush collection update", s.createQuery("select a from Publisher p join p.authors a").list().size() == 0);
    assertEquals(1, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    Set<Author> authorsOld = publisher.getAuthors();
    publisher.setAuthors(new HashSet<Author>());
    Author author2 = new Author();
    author2.setName("author2");
    author2.setPublisher(publisher);
    publisher.getAuthors().add(author2);
    List results = s.createQuery("select a from Publisher p join p.authors a").list();
    assertEquals(1, results.size());
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(2, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author2.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(author2.getBooks()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    s.delete(publisher);
    assertTrue("autoflush delete", s.createQuery("from Publisher p").list().size() == 0);
    txn.commit();
    s.close();
}
Also used : ActionQueue(org.hibernate.engine.spi.ActionQueue) Transaction(org.hibernate.Transaction) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) SessionImpl(org.hibernate.internal.SessionImpl) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with SessionImpl

use of org.hibernate.internal.SessionImpl in project midpoint by Evolveum.

the class HibernateToSqlTranslator method toSql.

/**
     * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade.
     * Method provides translation from hibernate {@link Criteria} to plain SQL string query.
     *
     * @param criteria
     * @return SQL string, null if criteria parameter was null.
     */
public static String toSql(Criteria criteria) {
    if (criteria == null) {
        return null;
    }
    try {
        CriteriaImpl c;
        if (criteria instanceof CriteriaImpl) {
            c = (CriteriaImpl) criteria;
        } else {
            CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) criteria;
            c = (CriteriaImpl) subcriteria.getParent();
        }
        SessionImpl s = (SessionImpl) c.getSession();
        SessionFactoryImplementor factory = s.getSessionFactory();
        String[] implementors = factory.getImplementors(c.getEntityOrClassName());
        CriteriaLoader loader = new CriteriaLoader((OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0], s.getLoadQueryInfluencers());
        Field f = OuterJoinLoader.class.getDeclaredField("sql");
        f.setAccessible(true);
        return (String) f.get(loader);
    } catch (Exception ex) {
        throw new SystemException(ex.getMessage(), ex);
    }
}
Also used : Field(java.lang.reflect.Field) CriteriaImpl(org.hibernate.internal.CriteriaImpl) SystemException(com.evolveum.midpoint.util.exception.SystemException) CriteriaLoader(org.hibernate.loader.criteria.CriteriaLoader) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionImpl(org.hibernate.internal.SessionImpl) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

SessionImpl (org.hibernate.internal.SessionImpl)7 Test (org.junit.Test)6 Session (org.hibernate.Session)5 List (java.util.List)4 Transaction (org.hibernate.Transaction)3 EntityManager (javax.persistence.EntityManager)2 ActionQueue (org.hibernate.engine.spi.ActionQueue)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 Field (java.lang.reflect.Field)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 PersistenceException (javax.persistence.PersistenceException)1 TransactionRequiredException (javax.persistence.TransactionRequiredException)1 TransactionManager (javax.transaction.TransactionManager)1 HibernateException (org.hibernate.HibernateException)1 JdbcCoordinatorImpl (org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl)1