Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate returning lazy loaded collections with null session
    primarykey
    data
    text
    <p>(SORT OF SOLVED, SEE BELOW)</p> <p>Hibernate 4.1/Spring/JPA project. I'm using Spring &amp; JPA annotations for transactions support, entity manager injection etc.</p> <p>What I'm seeing that within the same transaction my @OneToMany lazy-loaded collections dont have session property set, and of course cannot be loaded. If I do 'left join fetch' to force loading I get multiple records that point to the same PersistentBag - obviously this throws 'shared collection' exception.</p> <p>Here is my setup:</p> <p>Transaction entity ('transaction' is meant as in financial transaction)</p> <p>Code:</p> <pre><code>@Entity @Table(name = "transactionData") @Access(AccessType.PROPERTY) public class TransactionData extends AbstractTransaction implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @OneToMany(cascade=CascadeType.ALL,targetEntity=Location.class,fetch=FetchType.LAZY) @JoinColumns( { @JoinColumn(name="routecode",referencedColumnName="v_OPERSTAT"), @JoinColumn(name="cogrp", referencedColumnName="v_COUNTRY") }) @Transactional public Collection&lt;Location&gt; getLocations() { return super.getLocations(); } public void setLocations(Collection&lt;Location&gt; l) { super.setLocations(l); } } </code></pre> <p>Base class for Transaction:</p> <p>Code:</p> <pre><code>public abstract class AbstractTransaction { private List&lt;Location&gt; _locations; @Override @Transactional public List&lt;Location&gt; getLocations() { return _locations; } @Override public void setLocations(List&lt;Location&gt; value) { _locations = value; } } </code></pre> <p>Transaction entity is linked to Location entity using 2 integer type columns, these cols are not PK on either Transaction or Location entity.</p> <p>Location entity:</p> <p>Code:</p> <pre><code>@Entity @Table(name = "locations", uniqueConstraints = @UniqueConstraint(columnNames = { "cogrp", "cugrp", "bogrp", "status", "id" })) public class Location implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "cogrp", nullable = false) public int getCogrp() { return this.cogrp; } public void setCogrp(int cogrp) { this.cogrp = cogrp; } @Column(name = "routecode") public Integer getRoutecode() { return this.routecode; } public void setRoutecode(Integer routecode) { this.routecode = routecode; } } </code></pre> <p>Transaction entity has 1-to-many relationship to locations, and most Transaction entities will point to the same list of locations.</p> <p>Now, if I do the following query:</p> <p>Code:</p> <pre><code>select distinct t from " + Transaction.class.getName() + " t left join fetch t.locations where " + filterSQL </code></pre> <p>I get results back, but almost every Transaction entity points to the same PersistentBag of locations - needless to say this causes shared references error.</p> <p>If I omit left join fetch all entities come back with lazily loaded collections, but none have session property set. </p> <p>If I do eager loading I only get 2 entities eagerly loaded, the rest are still lazy (I know this is a built-in restriction, any way to override that?)</p> <p><strong>EDIT:</strong> I have come to believe this is a bug in Hibernate. If your query returns multiple records pointing to the same collection (perfectly valid scenario I might add!) then Hibernate will reuse the same collection when eagerly fetching, and set session to null when lazy loading. It will always result in either shared reference to same collection, or 'no session' errors. </p> <p>I have switched to EclipseLink 2.4, and after fixing my JPSQL it seems to work fine in the above described case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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