I have Hibernate 4 / Spring MVC 3.2.4 / Spring Data 1.4.1 with Spring Security Application in which I am trying to integrate EventListeners into object classes. I think that everything is set up correctly for me, and when I run my unit tests, I see that my event listeners quit. However, when I launch my full MVC application, event listeners do not start.
I lost a little where / how to debug this problem. My EntityManagerFactoryset up is almost the same in both cases:
PRODUCTS:
<beans profile="tomcat">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" lazy-init="true">
<property name="jndiName" value="java:comp/env/jdbc/josak" />
</bean>
<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>
UNITS:
<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>
therefore, I assume that this is not how the Unit test entityManagerFactory is defined differently.
My Unit test is pretty 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);
}
}
@PrePersist, , .
MVC- :
@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";
}
createUser() . , EventListener , , .
, , , .
My Entity :
@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;
...
}
- ? , MVC, unit test? Unit test Spring, .
, , .