Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails. Hibernate lazy loading multiple objects
    text
    copied!<p>I'm having difficulties with proxied objects in Grails. Assuming I've got the following</p> <pre><code>class Order { @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name="xxx", joinColumns = {@JoinColumn(name = "xxx")}, inverseJoinColumns = {@JoinColumn(name = "yyy")}) @OrderBy("id") @Fetch(FetchMode.SUBSELECT) private List&lt;OrderItem&gt; items; } class Customer { @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true) @JoinColumn(name = "xxx",insertable = false, nullable = false) private OrderItem lastItem; private Long lastOrderId; } </code></pre> <p>And inside some controller class</p> <pre><code>//this all happens during one hibernate session. def currentCustomer = Customer.findById(id) //at this point currentCustomer.lastItem is a javassist proxy def lastOrder = Order.findById(current.lastOrderId) //lastOrder.items is a proxy //Some sample actions to initialise collections lastOrder.items.each { println "${it.id}"} </code></pre> <p>After the iteration <code>lastOrder.items</code> still contains a proxy of <code>currentCustomer.lastItem</code>. For example if there are 4 items in the lastOrder.items collection, it looks like this:</p> <ul> <li>object</li> <li>object</li> <li>javassist proxy (all fields are null including id field). This is the same object as in currentCustomer.lastItem.</li> <li>object</li> </ul> <p>Furthermore, this proxy object has all properties set to null and it's not initialized when getters are invoked. I have to manually call <code>GrailsHibernateUtils.unwrapIdProxy()</code> on every single element inside <code>lastOrder.items</code> to ensure that there are no proxies inside (which basically leads to EAGER fetching).</p> <p>This one proxy object leads to some really weird Exceptions, which are difficult to track on testing phase.</p> <p>Interesting fact: if I change the ordering of the operations (load the order first and the customer second) every element inside <code>lastOrder.items</code> is initialized.</p> <p>The question is: Is there a way to tell Hibernate that it should initialize the collections when they are touched, no matter if any elements from the collection is already proxied in the session?</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