Results 1 to 3 of 3

Thread: Getting a session when using JPA and Hibernate when testing

  1. #1
    Join Date
    Jun 2011
    Posts
    8

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    One question why ?!... What is wrong with calling flush on the entityManager?!... YOU are making things way to complex this way.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2011
    Posts
    8

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •