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

3

8

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?

Excrete answered 30/7, 2009 at 13:0 Comment(0)
D
8

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.

Delwyn answered 30/7, 2009 at 14:42 Comment(3)
Thank you for your extensive answer, Michael. I have found some good info in it. But, I'm afraid, it doesn't solve the problem I have because two different EntityManager instances hold two different caches and when one of them updates an entity instance, the other one does not see the update and it's cached entity becomes outdatedExcrete
Sounds like you need to look at update triggers that will then take that object and update the other cache. Or you could make those two caches members of the same cluster if you caching solution suports clustering.Delwyn
Unfortunately I have no control over Hibernate's session cache. Or have I?Excrete
A
4

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

Antiphonary answered 28/9, 2011 at 12:55 Comment(0)
F
1

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

Flamethrower answered 16/11, 2010 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.