Hibernate 4 JPA EntityListeners not firing in Spring MVC/ Spring Data application

情到浓时终转凉″ 提交于 2019-12-12 06:38:51

问题


I've got a Hibernate 4 / Spring MVC 3.2.4/ Spring Data 1.4.1 with Spring Security application in which I am trying to integrate EventListeners on my entity classes. I think I have everything configured properly, and when I run my unit tests, I can see my event listeners being fired. However, when i run my full MVC application, the event listeners are never fired.

I'm a bit lost where/how to debug this problem. My EntityManagerFactory is setup pretty much the same in both cases:

PRODUCTION:

<beans profile="tomcat">
    <!-- application datasource -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true">
        <property name="jndiName" value="java:comp/env/jdbc/josak" />
    </bean>

    <!--  entity manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="packagesToScan" value="com.ia.domain"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.query.substitutions">true '1', false '0'</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>

    </bean>
</beans>

UNIT TESTS:

<beans profile="test">
    <jdbc:embedded-database id="dataSource" type="H2"></jdbc:embedded-database>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="packagesToScan" value="com.ia.domain"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.query.substitutions">true '1', false '0'</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
        </bean>
</beans>  

so I presume that it isn't the case of the Unit test entityManagerFactory being defined differently.

My unit test is fairly simple:

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:META-INF/spring/applicationContext*.xml"})
@ActiveProfiles("test")
public class UserServiceImplTest {
    @Test
    public void updateUser(){
        User newInfo = dod.getNewTransientUser(15);
        userService.saveUser(newInfo);
    }
}       

If I put a breakpoint in my @PrePersist method, I see that the method is called.

My MVC controller is also fairly straight-forward:

@RequestMapping( method=RequestMethod.GET, value="getUserInfo/{userId}", produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_PERMISSION_RATE_VIEW')")
@Transactional
public String getUserInfo( @PathVariable long userId ){

    userService.saveUser(createUser());
    return "done";
}

where createUser() simply returns a new User object with filled fields. However, my breakpoint in my EventListener is never hit, nor do I see anything in my logs indicating that it was.

If it is helpful, I can post by event listener class as well, but didn't think it would have much value.

My Entity class is defined as:

@Entity
@EntityListeners( AuditEventListener.class)
public class User {

    @TableGenerator( name="UUIDGenerator", pkColumnValue="user_id", table="uuid_generator", allocationSize=1)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator="UUIDGenerator")
    @Column(name = "id")
    private Long id;

...
}

Am I missing something obvious here? Is there a reason why this is not working from within the MVC application, but is from within the unit test? My Unit test uses a full Spring context configuration as well so I am very confused as to the difference between the two cases.

I realize that this is a very nebulous question, but any guidance how to further debug this would be much appreciated.


回答1:


As sad as it sounds, I think I have tracked the error down to being a jRebel problem. Not in jRebel itself, however, I had a rebel.xml config file that was pointing to my entities' classpath, even though I wanted jRebel to load the entities from my webapp class path (they are in 2 seperate modules). I have since modified my pom.xml and removed the rebel.xml file, and everything seems to work.



来源:https://stackoverflow.com/questions/22341841/hibernate-4-jpa-entitylisteners-not-firing-in-spring-mvc-spring-data-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!