Announcement Announcement Module
Collapse
No announcement yet.
Spring Data, Hibernate, and AbstractTransationalJUnit4SpringContextTest Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data, Hibernate, and AbstractTransationalJUnit4SpringContextTest

    I haven't worked with transaction managers and Hibernate a whole lot, but I'm using Spring Data JPA and Hibernate for a new application.

    I have a test case where I'm deleting an entity with my service layer. When I use the repository.findOne method to retrieve it after the delete, it comes back with null. Good. But I was thinking that I'm using my service to findOne which I haven't tested so I wanted to use the simpleJdbcTemplate that comes with the AbstractTransationalJUnit4SpringContextTest class. But when I queryForMap for that same entity, it comes back with data.

    The service method is annotated with @Transactional, so I assume it would have been committed, but I'm not positive now. Do I need to do a flush with Hibernate or something in my test case before trying to get the data with the simpleJdbcTemplate? Is it because the simpleJdbcTemplate isn't connected to the transaction manager? But if the method is annotated with @Transactional I would think it would have been saved.

    Also, I have show_sql set to true, and at no point do I see a delete statement in the output. I see a lot of select statements, but never a delete.

    Thanks.

  • #2
    Ok - I think what I need to do to fix this is 1 of 2 things. I can either (1) flush the hibernate session, or (2) instead of using simpleJdbcTemplate to make select statements directly use a Hibernate Session from the EntityManager and do it that way. I don't know how to go about doing either from my current setup.

    If I am using a JPA EntityManagerFacotry, how do I get the Hibernate session?

    Below is my snippet I'm using to test with:

    Code:
            
    <bean id="entityManagerFactory"
                    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                    <property name="dataSource" ref="dataSource" />
                    <property name="jpaVendorAdapter">
                            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                                    <property name="generateDdl" value="true" />
                                    <property name="showSql" value="true" />
                                    <property name="database" value="HSQL" />
                            </bean>
                    </property>
                    <property name="persistenceUnitName" value="persist.name" />
            </bean>
    
            <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                    <property name="entityManagerFactory" ref="entityManagerFactory" />
            </bean>
    
            <jdbc:embedded-database id="dataSource" type="HSQL" />
    
            <jdbc:initialize-database>
                    <jdbc:script location="classpath:testData.sql" />
            </jdbc:initialize-database>
    
            <jpa:repositories base-package="com.company.app" />
    
            <tx:annotation-driven />

    Comment

    Working...
    X