One DAO per entity - how to handle references?
Asked Answered
H

3

5

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.

Hardwood answered 24/9, 2012 at 19:3 Comment(5)
Whether you have a single EntityManager or not, you should certainly have a single persistence context shared by the two DAOs (that would naturally involve a single EntityManager, but it could be implemented with different instances at each injection site). How are you injecting your EntityManager? What container is this?Demogorgon
Also, how are you doing transaction demarcation? Could you show the code for UserDao?Demogorgon
I've added some more details regarding container and transactions.Hardwood
As for the userDao it just extends the SomeDAO<User> classHardwood
I don't know Spring, but if @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 the UserGroup, and then another to save the User. 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
T
3

Different instances of EntityManager are normal in Spring. It creates proxies that dynamically use the entity manager that is currently in a transaction if one exists. Otherwise, a new one will be created.

The problem is that your transactions are too short. Retrieving your user group executes in a transaction (because the findById method is implicitly @Transactional ). But then the transaction commits and the group is detached. When you save the new user, it will create a new transaction which fails because the user references a detached entity.

The way to solve this (and to do such things in general) is to create a method that does the whole operation in a single transaction. Just create that method in a service class (any Spring-managed component will work) and annotate it with @Transactional as well.

Takao answered 24/9, 2012 at 21:26 Comment(1)
I've tried to do as you said (removing @Transactional from SomeDao and wrapping whole operation in transaction). I got following exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.y.z.model.User#1]. I did not change anything regarding the model or the operation itself.Hardwood
A
3

I don't know Spring, but the JPA issue is that you are persisting a User that has a reference to a UserGroup, but JPA thinks the UserGroup is transient.

transient is one of the life-cycle states a JPA entity can be in. It means it's just created with the new operator, but has not been persisted yet (does not have a persistent identity yet).

Since you obtain your UserGroup instance via a DAO, it seems like something is wrong there. Your instance should not be transient, but detached. Can you print the Id of the UserGroup instance just after your received it from the DAO? And perhaps also show the findById implementation?

You don't have cascade persist on the group relation, so this normally should just work if the entity was indeed detached. Without a new entity, JPA simply has no way to set the FK correctly, since it would need the Id of the UserGroup instance here but that (seemingly) doesn't exist.

A merge should also not "overwrite" your detached entity. What is the exception that you're getting here?

I only partially agree with the answers being given by the others here about having to put everything in one transaction. Yes, this indeed may be more convenient as the UserGroup instance will still be 'attached', but it should not be -necessary-. JPA is perfectly capable of persisting new entities with references to either other new entities or existing (detached) entities that were obtained in another transaction. See e.g. JPA cascade persist and references to detached entities throws PersistentObjectException. Why?

Amoakuh answered 24/9, 2012 at 22:36 Comment(1)
Thanks for the response! I have added findById method implementation. Obviously, the persistentClass is UserGroup in case of UserGroupDao. I also checked the id of fetched UserGroup and it is OK.Hardwood
H
1

I am not sure how but I've managed to solve this. The user group I was trying to assign the user to had NULL version field in database (the field annotated with @Version). I figured out it was an issue when I was testing GWT RequestFactory that was using this table. When I set the field to 1 everything started to work (no changes in transaction handling were needed).

If the NULL version field really caused the problem then this would be one of the most misleading exception messages I have ever got.

Hardwood answered 25/9, 2012 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.