java – EntityManager.merge没有做任何事情

前端之家收集整理的这篇文章主要介绍了java – EntityManager.merge没有做任何事情前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用户实体:
  1. @Entity
  2. @Table( name = "bi_user" )
  3. @SequenceGenerator( name = "USER_SEQ_GEN",sequenceName = "USER_SEQUENCE" )
  4. public class User
  5. extends DataObjectAbstract<Long>
  6. {
  7. private static final long serialVersionUID = -7870157016168718980L;
  8.  
  9. /**
  10. * key for this instance. Should be managed by JPA provider.
  11. */
  12. @Id
  13. @GeneratedValue( strategy = GenerationType.SEQUENCE,generator = "USER_SEQ_GEN" )
  14. private Long key;
  15.  
  16. /**
  17. * Username the user will use to login. This should be an email address
  18. */
  19. @Column( nullable=false,unique=true)
  20. private String username;
  21.  
  22. // etc. other columns and getters/setters
  23. }

其中DataObjectAbstract是一个简单的@MappedSuperClass,它具有jpa版本和equals / hashcode定义.

我有一个基本的dao类,看起来像这样

  1. public abstract class BaseDaoAbstract<T extends DataObject<K>,K extends Serializable>
  2. implements BaseDao<T,K>
  3. {
  4.  
  5. @PersistenceContext
  6. private EntityManager em;
  7.  
  8. /**
  9. * Save a new entity. If the entity has already been persisted,then merge
  10. * should be called instead.
  11. *
  12. * @param entity The transient entity to be saved.
  13. * @return The persisted transient entity.
  14. */
  15. @Transactional
  16. public T persist( T entity )
  17. {
  18. em.persist( entity );
  19. return entity;
  20. }
  21.  
  22. /**
  23. * merge the changes in this detached object into the current persistent
  24. * context and write through to the database. This should be called to save
  25. * entities that already exist in the database.
  26. *
  27. * @param entity The entity to be merged
  28. * @return The merged entity.
  29. */
  30. @Transactional
  31. public T merge( T entity )
  32. {
  33. return em.merge( entity );
  34. }
  35.  
  36. // other methods like persist,delete,refresh,findByKey that all delegate to em.
  37. }

我已经在web.xml中定义了OpenEntityManagerInView过滤器,如下所示

  1. <filter>
  2. <filter-name>openEntityManagerInViewFilter</filter-name>
  3. <filter-class>
  4. org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
  5. </filter-class>
  6. <init-param>
  7. <param-name>entityManagerfactorybeanName</param-name>
  8. <param-value>biEmf</param-value>
  9. </init-param>
  10. </filter>
  11.  
  12. <filter-mapping>
  13. <filter-name>openEntityManagerInViewFilter</filter-name>
  14. <url-pattern>/*</url-pattern>
  15. </filter-mapping>

我最近升级到eclipselink 2.3.2和Spring 3.1,并从CGLIB代理转换为使用AspectJ for Spring的加载时间编织,但是我没有为eclipselink配置LTW.

问题出在这个代码中,它存在于SpringListener中,见注释.

  1. User user = userService.findByKey(userDetails.getKey());
  2.  
  3. // THIS MERGE NEVER WRITES THROUGH TO THE DATABASE.
  4. // THIS DOESN'T WORK AS PERSIST EITHER
  5. user = userService.merge( user.loginSuccess() );

user.loginSuccess只是设置一些字段并返回这个我确定它正在通过代码,因为我收到日志语句,我可以设置一个断点并通过它.我的postgres日志没有显示任何流量到postgres合并.

我保存所有地方的其他东西没有问题,包括用户在另一个位置,当他们更改密码,我知道这个代码以前工作.这里有什么明显的不好吗?我使用OpenEntityManagerInViewFilter不正确吗?我应该使用@Transactional方法,以便实体被认为是管理的?任何帮助是赞赏.

更新
我尝试了由prajeesh建议的冲水.这是代码

  1. @Transactional
  2. public T merge( T entity )
  3. {
  4. entity = em.merge( entity );
  5. em.flush();
  6. return entity;
  7. }

在com.bi.data的一个类中.我有这个在我的春天app配置文件

  1. <context:component-scan base-package="com.bi.controller,com.bi.data,com.bi.web" />

在我的春天配置我有

  1. <context:load-time-weaver/>
  2. <tx:annotation-driven mode="aspectj"/>

一个aop.xml如下所示:

  1. <aspectj>
  2. <weaver>
  3. <!-- only weave classes in our application-specific packages -->
  4. <include within="com.bi..*"/>
  5. </weaver>
  6. </aspectj>

我得到了

  1. javax.persistence.TransactionrequiredException:
  2. Exception Description: No transaction is currently active

所以有些东西显然配置错误,但是什么?

更新2:
我恢复了我的更改以启用加载时间编织,现在合并通过或不使用flush,但我仍然不明白LTW的问题是什么…

解决方法

在em.merge()之后尝试em.flush().有时,EntityManager只是稍后保留更新以进行更新.

猜你在找的Java相关文章