views:

3853

answers:

1

Hi, everybody.

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?

+4  A: 

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.

Michael Wiles
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 outdated
artemb
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.
Michael Wiles
Unfortunately I have no control over Hibernate's session cache. Or have I?
artemb