[英]Migrate Spring application to Spring Boot with Spring Data JPA and Hibernate
[英]Hibernate 4 JPA EntityListeners not firing in Spring MVC/ Spring Data application
我有一個帶有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()
只是返回一個具有填充字段的新User對象。 但是,我的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上下文配置,因此我對這兩種情況之間的區別感到非常困惑。
我意識到這是一個非常模糊的問題,但是任何有關進一步調試該問題的指南將不勝感激。
聽起來很難過,我認為我已將錯誤歸結為一個jRebel問題。 但是,盡管我希望jRebel從我的webapp類路徑中加載實體(它們位於2個獨立的模塊中),但我不在jRebel本身中,而是擁有一個指向我實體的類路徑的rebel.xml配置文件。 從那以后,我修改了pom.xml並刪除了rebel.xml文件,一切似乎都正常了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.