Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.1
Mapping documents:
Code:
@Entity(access=AccessType.FIELD)
public class User implements Serializable {
@Id(generate = GeneratorType.AUTO)
private Long id;
@ManyToOne(cascade=CascadeType.ALL) //TODO Is this really necessary
@NotNull
private Collective collective;
public Collective getCollective() {
return collective;
}
public void setCollective(Collective collective) {
this.collective = collective;
}
}
Code:
@Entity(access=AccessType.FIELD)
public class Collective {
@Id(generate = GeneratorType.AUTO)
private Long id;
private String name;
@OneToMany(cascade=CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy="collective")
@OrderBy("name")
private Set<User> users;
@OneToMany(cascade=CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy="collective")
@OrderBy("name")
private Set<Category> categories;
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Category> getCategories() {
return categories;
}
public void addCategory(Category category) {
category.setCollective(this);
categories.add(category);
}
}
Code between sessionFactory.openSession() and session.close():Code:
User user = getUserFromHTTPSession();
session.lock(user, LockMode.NONE); //reconnecting user.
Collective collective = user.getCollective();
Category category = new Category();
category.setIsDeleted(false);
category.setName(getNewCategoryName());
user.getCollective().addCategory(category);
session.flush();
for(Category currCategory : user.getCollective().getCategories()) {
System.out.println("category: " + currCategory.getName());
}
The first time try to reconnect user to the session (with lock()) I get
org.hibernate.HibernateException: reassociated object has dirty collection
The thing is that I have followed the Parent/Child example in the Hibernate manual (Chapter 21) and I still don't get it to work :(