Announcement Announcement Module
Collapse
No announcement yet.
Getting a session when using JPA and Hibernate when testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting a session when using JPA and Hibernate when testing

    What is the preferred way to get the hibernate session so it can be flushed when testing?

    According to the reference manual (scroll down), one should always flush() the hibernate session when testing the ORM mapping to avoid false positives:

    Code:
    @Autowired
    private SessionFactory sessionFactory;
    
    @Test(expected = GenericJDBCException.class)
    public void updateWithSessionFlush() {
    
        updateEntityInHibernateSession();
    
        // Manual flush is required to avoid false positive in test
        sessionFactory.getCurrentSession().flush();
    }

    My configuration
    I have an entityManagerFactory that I can use:
    Code:
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
            </props>
        </property>
    
        <!-- more properties -->
    </bean>


    Option A
    Somewhere I read that I could the getSessionFactory() method (see below). The problem is that there is no such method.
    Code:
    <bean id="sessionFactory"  
        factory-bean="entityManagerFactory" 
        factory-method="getSessionFactory" />


    Option B
    Elsewhere I found that I inject the EntityManagerFactory and use it (as opposed to a SessionFactory), which seems to work?
    Code:
    @PersistenceContext
    EntityManager entityManager;
    
    @Test
    void someTest() {
        updateJpaEntity();
    
        entityManager.unwrap(Session.class).flush();
    
        // execute validations
    }


    Option C
    Create a SessionFactory bean using the HibernateJpaSessionFactoryBean and then flush it like in the reference manual. The problem is that this fails (HibernateException: No Session found for current thread)
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
        
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        
        <!-- Are there any other properties required? -->
    </bean>

    Dependencies
    Spring 3.1
    Hibernate 4.0.1

  • #2
    One question why ?!... What is wrong with calling flush on the entityManager?!... YOU are making things way to complex this way.

    Comment


    • #3
      Thanks!

      I failed to see the flush() method of the entity manager. That's why things became complex, and that's why I wrote the question.

      Comment

      Working...
      X