java – EntityManager. Merge didn’t do anything
I have a user entity:
@Entity @Table( name = "bi_user" ) @SequenceGenerator( name = "USER_SEQ_GEN",sequenceName = "USER_SEQUENCE" ) public class User extends DataObjectAbstract<Long> { private static final long serialVersionUID = -7870157016168718980L; /** * key for this instance. Should be managed by JPA provider. */ @Id @GeneratedValue( strategy = GenerationType.SEQUENCE,generator = "USER_SEQ_GEN" ) private Long key; /** * Username the user will use to login. This should be an email address */ @Column( nullable=false,unique=true) private String username; // etc. other columns and getters/setters }
Dataobjectabstract is a simple @ mappedsuperclass with JPA version and equals / hashcode definition
I have a basic Dao class that looks like this
public abstract class BaseDaoAbstract<T extends DataObject<K>,K extends Serializable> implements BaseDao<T,K> { @PersistenceContext private EntityManager em; /** * Save a new entity. If the entity has already been persisted,then merge * should be called instead. * * @param entity The transient entity to be saved. * @return The persisted transient entity. */ @Transactional public T persist( T entity ) { em.persist( entity ); return entity; } /** * merge the changes in this detached object into the current persistent * context and write through to the database. This should be called to save * entities that already exist in the database. * * @param entity The entity to be merged * @return The merged entity. */ @Transactional public T merge( T entity ) { return em.merge( entity ); } // other methods like persist,delete,refresh,findByKey that all delegate to em. }
I'm already on the web The openentitymanagerinview filter is defined in XML, as shown below
<filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter </filter-class> <init-param> <param-name>entityManagerfactorybeanName</param-name> <param-value>biEmf</param-value> </init-param> </filter> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
I recently upgraded to eclipse link 2.3 2 and spring 3.1, and converted from cglib proxy to load time weaving using AspectJ for spring, but I didn't configure LTW for eclipse link
The problem lies in this code, which exists in the spring listener. See the comments
User user = userService.findByKey(userDetails.getKey()); // THIS MERGE NEVER WRITES THROUGH TO THE DATABASE. // THIS DOESN'T WORK AS PERSIST EITHER user = userService.merge( user.loginSuccess() );
user. Loginsuccess just sets some fields and returns this. I'm sure it's passing the code. Because I receive the log statement, I can set a breakpoint and pass it My Postgres log does not show any traffic merging to Postgres
I have no problem saving everything else, including users in another location. When they change their password, I know this code worked before Is there anything obviously bad here? Am I using openentitymanagerinviewfilter incorrectly? Should I use the @ transactional method so that entities are considered managed? Any help is appreciated
Update I tried the flush suggested by prajeesh This is the code
@Transactional public T merge( T entity ) { entity = em.merge( entity ); em.flush(); return entity; }
On COM bi. In a class of data I have this app profile in my spring
<context:component-scan base-package="com.bi.controller,com.bi.data,com.bi.web" />
In my spring configuration I have
<context:load-time-weaver/> <tx:annotation-driven mode="aspectj"/>
An AOP The XML is as follows:
<aspectj> <weaver> <!-- only weave classes in our application-specific packages --> <include within="com.bi..*"/> </weaver> </aspectj>
I got it
javax.persistence.TransactionrequiredException: Exception Description: No transaction is currently active
So some things are obviously misconfigured, but what?
Update 2: I restored my changes to enable load time weaving. Now merge through or without flush, but I still don't understand what the problem with LTW is
Solution
Try em.flush() after em.merge() Sometimes, the entitymanager just keeps the update for update later