These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Change DTYPE to switch Entity types
PostPosted: Mon Apr 13, 2009 4:07 pm 
Newbie

Joined: Thu Dec 11, 2008 7:10 pm
Posts: 4
I am using JPA w/Hibernate. Let's say I have a TrialUser entity and a PayingUser entity. I am using the discriminator column strategy to handle inheritance. When a TrialUser buys a membership, I want to convert them to a PayingUser entity.

I've discovered that the best way to do this is to simply change the DTYPE column manually from TrialUser to PayingUser. Since the entities have similar basic properties (though vastly different methods) this works perfectly well in my manual tests.

Here is the problem:

If the DTYPE column changes during runtime, with a live Persistence Context and an instance of the TrialUser attached, how can I detach, then reload this entity as a PayingUser. When trying a refresh(), I get an error correctly saying that it expects a TrialUser type. I believe I could call EntityManager.clear(), but I'm running this in a Spring repository bean shared by the entire application. I believe that would result in all pending changes application-wide being lost.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 10:26 pm 
Newbie

Joined: Mon Apr 06, 2009 10:48 pm
Posts: 4
Location: Wellington, New Zealand
If you can stomach it, you could fall back on the native Hibernate Session interface and use Session.evict(object) to remove just the affected object from the persistence context. Alas, there's no equivalent in JPA.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 2:51 pm 
Newbie

Joined: Thu Dec 11, 2008 7:10 pm
Posts: 4
Thanks for the tip, that looks like what I needed. I'm having a problem though. Here is my code doing the type conversion:

Code:
   
      // Make sure we save any pending changes
      user = saveUser(user);
      entityManager.flush();

      // Remove the User instance from the persistence context
      final Session session = (Session) entityManager.getDelegate();
      session.evict(user);

      // Update the DTYPE
      final String sqlString = "update user set user.DTYPE = '" + targetClass.getSimpleName() + "' where user.id = :id";
      final Query query = entityManager.createNativeQuery(sqlString);
      query.setParameter("id", user.getId());
      query.executeUpdate();

      entityManager.flush();

      // Load the User with its new type
      return getUserById(userId);


The problem is that when I run this code, I get the following error thrown during the second flush():

Code:
org.hibernate.PersistentObjectException: detached entity passed to persist: com.myapp.domain.Membership
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
   at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:671)
   at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:663)
   at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:346)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:319)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:265)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:242)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:153)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
   at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
   at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:996)
   at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1185)
   at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:357)
   at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:51)
   at com.myapp.repository.user.JpaUserRepository.convertUserType(JpaUserRepository.java:107)


Where Membership is an entity that User has a Set of, mapped @OneToMany

Does anyone understand what's happening?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.