Note that there are some explanatory texts on larger screens.

plurals
  1. POStale object in memory (mysql, spring 3, hibernate)
    text
    copied!<p>I'm using Spring 3, Hibernate 3.3.1 and MySQL 5.1.x (on red hat 5, also see same behavior when on windows however). I'm noticing that a row in the database is getting updated, but the one of the object references in memory is not being refreshed. I'll try to describe the class structure in a simplified form..</p> <pre><code>class A { @OneToMany(cascade = CascadeType.ALL) List&lt;Widget&gt; widgets; @OneToMany(cascade = CascadeType.ALL) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) List&lt;B&gt; bObjects; @Transactional public void turnOnWidgets() { ... } } class B { @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) @Cascade(value = { org.hibernate.annotations.CascadeType.LOCK, org.hibernate.annotations.CascadeType.EVICT}) private A aObj; @Transactional(propagation=Propagation.REQUIRES_NEW) public void turnOnWidgets() { for (Widget w : aObj.getWidgets()) { w.setOn(true); } } } class Widget { Boolean on; } </code></pre> <p>Here's the sequence of actions. When A.turnOnWidgets gets invoked it iterates through its collection of "bObjects" and invokes "turnOnWidgets" on each B object. The B object modifies the "on" property for each widget referenced by the A object (as shown in above in B.turnOnWidgets).</p> <p>When this happens I can look at the database and see that the "on" value has changed for the widgets. However, when I step into the code using a debugger I notice that the B object references a different instance of A (e.g. the same A from the database but in memory its a different Java object instance) than the A that I invoked "turnOnWidgets" on.</p> <p>Effectively what I see is that when I navigate the object graph by looking at the Widgets contained by the A instance, the Widgets "on" property is false. However, if I look at the Widgets by navigating from <code>B.aObj.getWidgets()</code> their "on" property is true.</p> <p>When my application was running against an Oracle database this all worked fine, once I switched to <code>MySQL</code> I began seeing this problem.</p> <p>Anyone know if there is something particular about <code>MySQL</code> (perhaps in conjunction with Hibernate and/or Spring)?</p> <p><strong>UPDATE</strong> So I think I've narrowed it down to a section in the code where a JPA "refresh" is performed on the "B" object. When running against oracle this causes the objects to refresh properly in memory, however, when running against MySQL the objects are not being updated properly. Still need to figure out why.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload