I'm a Glassfish user who has been using Hibernate 3.2 as the underlying persistence engine in Glassfish successfully for months now. It would appear that there's some unexpected behavior in Hibernate 3.2.1 that wasn't present in 3.2.0, however.
I can't left-join-fetch a lazily-loaded entity w/o a LIE (LazyInitializationException) in 3.2.1! Can one of you Glassfish-Hibernaters confirm this for me?
This exception didn't occur w/ the same code in 3.2.0 -or- when using Toplink.
Here's the entity relationship:
Code:
@Entity
@Table(name="pub.\"ord-traf\"")
public class OrderTraffic implements Serializable
{
/**
* EmbeddedId primary key field
*/
@EmbeddedId
protected OrderTrafficPK orderTrafficPK;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="\"carrier-no\"", insertable=false, updatable=false)
private Vendor vendor;
.....................
}
...the PK class for above entity...
@Embeddable
public class OrderTrafficPK implements Serializable
{
@Column(name="\"ar-entity\"", nullable=false)
private String arEntity;
@Column(name="\"load-no\"", nullable=false)
private String loadNo;
@Column(name="\"plant\"", nullable=false)
private String plant;
.....................
}
@Entity
@Table(name="pub.\"vendor\"")
public class Vendor implements Serializable
{
@Id
@Column(name="\"Vendor-code\"", nullable=false)
private String vendorcode;
@Column(name="\"Name\"", nullable=false)
private String name;
@Column(name="\"Zip-code\"", nullable=false)
private String zipcode;
.....................
}
As you can see, I have an entity called "OrderTraffic" that has a uni-directional one-to-one relationship w/ "Vendor". OrderTraffic has a composite PK, Vendor does not. I tried @OneToOne as well...the exception is the same.
Here's the JPA-QL to call them:
Code:
String query = "select ot from OrderTraffic ot left join fetch ot.vendor";
At runtime, I see this query (exactly as expected):
Code:
Hibernate:
/* select
ot
from
OrderTraffic ot
left join
fetch ot.vendor
where
ot.shipDate = :shipDate
and ot.shipDate > :maxShipDate
order by
ot.carrierNo */ select
ordertraff0_."ar-entity" as ar1_128_0_,
ordertraff0_."load-no" as load2_128_0_,
ordertraff0_."plant" as plant3_128_0_,
vendor1_."Vendor-code" as Vendor1_131_1_,
ordertraff0_."truck-no" as truck4_128_0_,
ordertraff0_."trailer-no" as trailer5_128_0_,
ordertraff0_."can-type" as can6_128_0_,
ordertraff0_."carrier-no" as carrier7_128_0_,
ordertraff0_."dept-time" as dept8_128_0_,
ordertraff0_."ship-date" as ship9_128_0_,
ordertraff0_."seal-no" as seal10_128_0_,
ordertraff0_."spec-instr" as spec11_128_0_,
ordertraff0_."trailer-ad" as trailer12_128_0_,
ordertraff0_."trailer-at" as trailer13_128_0_,
ordertraff0_."max-net" as max14_128_0_,
ordertraff0_."est-freight" as est15_128_0_,
ordertraff0_."act-freight" as act16_128_0_,
ordertraff0_."lock-load" as lock17_128_0_,
vendor1_."Name" as Name2_131_1_,
vendor1_."Zip-code" as Zip3_131_1_,
vendor1_."Telephone" as Telephone4_131_1_,
vendor1_."Contact" as Contact5_131_1_
from
pub."ord-traf" ordertraff0_
left outer join
pub."vendor" vendor1_
on ordertraff0_."carrier-no"=vendor1_."Vendor-code"
where
ordertraff0_."ship-date"=?
and ordertraff0_."ship-date">?
order by
ordertraff0_."carrier-no"
I'm calling it in a very straightforward way in a JSF managed bean and simply listing the entity data in a dataTable...there's nothing special going on there.
Here's the exception:
Code:
executePhase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@2a1d96) threw exception
javax.faces.FacesException: javax.el.ELException: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at javax.faces.component.UIOutput.getValue(UIOutput.java:176)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:100)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:279)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:207)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:848)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:240)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:309)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:828)
...........................
What's the problem? I'm fetching it before it is rendered in the view...yet it fails when using Hibernate 3.2.1...and it did not in 3.2.0, or Toplink...like I said.
Help is much appreciated, thanks!!