I am writing an application that has typical two entities: User and UserGroup. The latter may contain one or more instances of the former. I have following (more/less) mapping for that:
User:
public class User {
@Id
@GeneratedValue
private long id;
@ManyToOne(cascade = {CascadeType.MERGE})
@JoinColumn(name="GROUP_ID")
private UserGroup group;
public UserGroup getGroup() {
return group;
}
public void setGroup(UserGroup group) {
this.group = group;
}
}
User group:
public class UserGroup {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy="group", cascade = {CascadeType.REMOVE}, targetEntity = User.class)
private Set<User> users;
public void setUsers(Set<User> users) {
this.users = users;
}
}
Now I have a separate DAO class for each of these entities (UserDao and UserGroupDao). All my DAOs have EntityManager injected using @PersistenceContext annotation, like this:
@Transactional
public class SomeDao<T> {
private Class<T> persistentClass;
@PersistenceContext
private EntityManager em;
public T findById(long id) {
return em.find(persistentClass, id);
}
public void save(T entity) {
em.persist(entity);
}
}
With this layout I want to create a new user and assign it to existing user group. I do it like this:
UserGroup ug = userGroupDao.findById(1);
User u = new User();
u.setName("john");
u.setGroup(ug);
userDao.save(u);
Unfortunately I get following exception:
object references an unsaved transient instance - save the transient instance before flushing: x.y.z.model.User.group -> x.y.z.model.UserGroup
I investigated it and I think it happens becasue each DAO instance has different entityManager assigned (I checked that - the references in each DAO to entity manager are different) and for user entityManager does not manager the passed UserGroup instance.
I've tried to merge the user group assigned to user into UserDAO's entity manager. There are two problems with that:
- It still doesn't work - the entity manager wants to overwrite the existing UserGroup and it gets exception (obviously)
- even if it worked I would end up writing merge code for each related entity
Described case works when both find and persist are made using the same entity manager. This points to a question(s):
- Is my design broken? I think it is pretty similar to recommended in this answer. Should there be single EntityManager for all DAOs (the web claims otherwise)?
- Or should the group assignment be done inside the DAO? in this case I would end up writing a lot of code in the DAOs
- Should I get rid of DAOs? If yes, how to handle data access nicely?
- any other solution?
I am using Spring as container and Hibernate as JPA implementation.
EntityManager
or not, you should certainly have a single persistence context shared by the two DAOs (that would naturally involve a singleEntityManager
, but it could be implemented with different instances at each injection site). How are you injecting yourEntityManager
? What container is this? – DemogorgonUserDao
? – Demogorgon@Transactional
means that invocations of a class's methods are wrapped in transactions, then that could be your problem. You use one transaction to find theUserGroup
, and then another to save theUser
. I don't know that that would cause the problem you observe, but it's definitely the wrong thing to do - a transaction should wrap an entire unit of work. I would strongly suggest doing the find and the save in the same transaction (even if it doesn't solve the problem!). – Demogorgon