Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One DAO per entity - how to handle references?

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.

like image 778
gregorej Avatar asked Sep 24 '12 19:09

gregorej


People also ask

Should I create DAO for each table?

You should design your DAO according to your application needs, not the layout of your database. Start of with one DAO, and if it becomes too large, then refactor it into multiple DAOs in a way that makes sense to your code. The whole point of a DAO is to hide any database concepts (like tables) from your application.

Is DAO same as entity?

DAO is an abbreviation for Data Access Object, so it should encapsulate the logic for retrieving, saving and updating data in your data storage (a database, a file-system, whatever). An entity is a lightweight persistence domain object.

What is the use of DAO class in spring boot?

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way.

What is DAO in hibernate?

Data Access Objects (or DAOs for short) are used as a direct line of connection and communication with our database. DAOs are used when the actual CRUD (CRUD = Create, Read, Update, Delete) operations are needed and invoked in our Java code. These data access objects also represent the “data layer” of our application.


1 Answers

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.

like image 72
rolve Avatar answered Oct 03 '22 02:10

rolve