Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @OneToMany with mappedBy (parent-child) relationship and cache problem

I have this problem for a long time now, I have searched the web and SO in and out and didn't find a solution yet. I hope you can help me on that.

I have a parent-child relationship between two entities like the following:

@Entity
public class Parent {
    // ...

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Set<Child> children = new HashSet<Child>();

    // ...
}

@Entity
public class Child {
    // ...

    @ManyToOne(fetch = FetchType.LAZY)
    private Parent parent;

    // ...
}

The thing is that when I create a new child and assign it to a parent, the parent doesn't get updated when it is in the cache already.

 Parent parent = new Parent();
 em.persist(parent);

 // ...

 Child child = new Child();
 child.setParent(parent);
 em.persist(child);

 parent.getChildren().size(); // returns 0

I have tried to use @PreUpdate to automatically add the child to the parent when the child is persisted, but in the case when we have 2 entity managers in 2 different threads (like in JBoss), the issue still exists, until we call em.refresh(parent)

So the question is - is there a way to smoothly eliminate the problem and ensure that parent.getChildren() always return the up-to-date list of children?

like image 876
artemb Avatar asked Jul 30 '09 13:07

artemb


3 Answers

Most ORM's will behave this way.

The object in the cache is not updated from the database (an extra read that is not necessary). Also think of the object model and the persistence as separate. i.e. keep your object model consistent with itself and don't rely on the persistence mechanism to do this for you.

So if you want the object to be added to the collection then do that in the "setParent" code.

The best practice in this case is in fact to make one side of the relationship do all the work and let the other side defer onto it. Also I would suggest using field access rather than method access, that way you can customise methods with greater flexibility.

Add a method to parent called addChild

 public void addChild(Child child) {
    child.setParent0(this);
    getChildren().add(individualNeed);
 }

and then make setParent in Child:

public void setParent(Parent parent) {
   parent.addChild(child);
}

setParent0 in Child is the property stter for parent on child.

public void setParent0(Parent parent) {
   this.parent = parent;
}

I would also suggest that the "getChildren" method return an immutable collection so that developers don't inadvertantly not use this method (I learnt the hard way in all of this).

One more thing, you should have null checking code and other defensive pieces in the above code, I left it out for clarity.

like image 108
Michael Wiles Avatar answered Nov 02 '22 07:11

Michael Wiles


Pretty sure your problem here is your Cascade settings.

@Entity
public class Parent {
   // ...

   @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, 
      cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
   @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
   private Set<Child> children = new HashSet<Child>();

   // ...
}

@Entity
public class Child {
    // ...

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    private Parent parent;

    // ...
}

Using these cascade settings will cascade persist and updates to child objects.

eg.

Parent parent = new Parent();
em.persist(parent);

// ...

Child child = new Child();
child.setParent(parent);
em.persist(child); //will cascade update to parent

parent.getChildren().size(); // returns 1

or

Parent parent = new Parent();
Child child = new Child();
parent.setChild(parent);
em.persist(parent); //will cascade update to child

child.getParent(); // returns the parent

more info on this can be found at Hibernate Annotations

like image 39
Michael Allen Avatar answered Nov 02 '22 06:11

Michael Allen


Regarding your issue with caching, this is a very common problem when you have multiple VMs running against the same database with separate caches. It's called "cache drift".

Most hibernate-friendly cache implementations (ehcache, OSCache and SwarmCache) have a distributed cache built-in that can be used to synchronize the caches. The distributed cache, generally, sends multicast messages updating the state of the cache. Doing a second level cache eviction by SessionFactory.evict(Class,id), for example, will cause an invalidation message to be sent to the other caches in the cluster which will invalidate any other copies of that object in other caches.

Depending on your deployment, the multicast may or may not be acceptable to you. If it is not you may need to use a single-cache solution like memcached.

I personally found the configuration of eh cache's distributed cache very straightforward.

EH cache discusses the problem in a bit more detail here: http://ehcache.org/documentation/distributed_caching.html

like image 1
StevenWilkins Avatar answered Nov 02 '22 06:11

StevenWilkins