I'm having an issue with entities in a map being persisted. Here is the code:
Code:
@Entity
@Name("user")
@Table(name = "customer_user")
@Scope(ScopeType.SESSION)
@SequenceGenerator(name = "seq", sequenceName = "customer_user_seq")
public class User {
...
@OneToMany(cascade = javax.persistence.CascadeType.PERSIST)
@JoinColumn(name = "user_id", updatable = false)
@MapKey(name = "name")
@Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
@Valid
private Map<String, UserPreference> preferences =
new HashMap<String, UserPreference>();
public void setPreference(String name, String value)
{
UserPreference pref = this.preferences.get(name);
if (pref == null)
{
pref = new UserPreference();
pref.setName(name);
pref.setValue(value);
this.preferences.put(name, pref);
}
else
{
pref.setValue(value);
}
}
Code:
@Name("userPreference")
@Entity
@Table(name = "user_preference")
@Scope(ScopeType.CONVERSATION)
@SequenceGenerator(name = "seq", sequenceName = "user_preference_seq")
public class UserPreference { ... (no reverse mapping)
Now if I call:
user.setPreference("test", "testval");
The preference will not be inserted unless I call:
entityManager.merge(user);
Since the map is marked as dirty, and the user object is not detached, shouldn't be picked up automatically and the cascade automatically work? What do I need to do to ensure that the cascade is picked up. I don't have a reference to the entity manager in the user object, so cannot call persist in the setPreference method
FYI, both objects have a parent class with a version property and an ID:
Code:
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Long id;
@Basic
@Version
@Column(name="modified_dt")
private Timestamp modified;
I can call the merge, I'd just like to know why I have to.
Hibernate version: 3.2.4 sp1
Hibernate annotations version: 3.3.0.GA
Hibernate entityManager version: 3.3.1.GA