【发布时间】:2014-04-16 00:23:17
【问题描述】:
我有一个带有 Spring Security 应用程序的 Hibernate 4/Spring MVC 3.2.4/Spring Data 1.4.1,我正在尝试将 EventListeners 集成到我的实体类中。我想我已经正确配置了所有东西,当我运行单元测试时,我可以看到我的事件监听器被触发了。但是,当我运行完整的 MVC 应用程序时,事件侦听器永远不会被触发。
我有点迷失在哪里/如何调试这个问题。我的EntityManagerFactory 在这两种情况下的设置几乎相同:
生产:
<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>
单元测试:
<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>
所以我认为单元测试 entityManagerFactory 的定义并非如此。
我的单元测试相当简单:
@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 中的断点从未被命中,我在日志中也没有看到任何表明它被命中的内容。
如果有帮助,我也可以通过事件监听器类发布,但认为它没有多大价值。
我的实体类定义为:
@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 应用程序中不起作用,而是在单元测试中起作用?我的单元测试也使用了完整的 Spring 上下文配置,所以我对这两种情况之间的区别感到非常困惑。
我意识到这是一个非常模糊的问题,但任何关于如何进一步调试的指导将不胜感激。
【问题讨论】:
标签: java spring hibernate jpa entitylisteners