-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to make Map fields cascade? TransientObjectException...
PostPosted: Thu Mar 22, 2012 2:05 pm 
Newbie

Joined: Thu Mar 22, 2012 1:32 pm
Posts: 3
Hello,

I am trying to update an entity field of type Map, adding a new pair key/value. Keys are String and values are Reviews (an entity). I put @Cascade(ALL) so changes in the container entity are propagated, but anyway I get an exception when I try to commit:

Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Reviews


This is why I try to do:

Code:
// Get App
App app = (App) session.get(App.class, appId);

// Create Reviews (not persisted yet)
Reviews reviews = new Reviews();
fillReviewsForCountry(reviews, countryName);

// Don't save Reviews yet (TransientObjectException is NOT thrown if I uncomment this)
// session.save(reviews);

// Put new Reviews inside App
app.getReviews().put(countryName, reviews);

// Commit. TransientObjectException is thrown here (unless I save reviews before)
session.commit();


This is how App reviews is declared:

Code:
@Entity
public class App
{
   private Map<String, Reviews> reviews = new HashMap<String, Reviews>();

   @ElementCollection
   @JoinTable(name = "APP_REVIEWS", joinColumns = @JoinColumn(name = "appId"))
   @MapKeyColumn(name = "country")
   @Column(name = "reviews")
   @Cascade(ALL)
   public Map<String, Reviews> getReview()
   {
      return reviews;
   }

   public void setReviews(Map<String, Reviews> reviews)
   {
      this.reviews = reviews;
   }
}


Just in case it is important, Reviews class contains a Set<Review>, but it causes no problems. For example, if I get a Reviews object and add some Review objects to it, then everything goes fine: the Review objects get persisted even if I don't save them explicitly.

So, java.util.Set cascades well but java.util.Map doesn't. Or I made some mistake when putting the annotations.

Can you help me?

Thanks a lot


Top
 Profile  
 
 Post subject: Re: How to make Map fields cascade? TransientObjectException...
PostPosted: Mon Apr 02, 2012 1:33 pm 
Newbie

Joined: Thu Mar 22, 2012 1:32 pm
Posts: 3
Any idea, suggestion, comment?
Anything could be helpful.
Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.