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