We have just converted a POJO used in a Many-to-one relationship from xDoclet-based mappings to Annotations (this is the first item of many we will convert) and get a LazyInitializationException accessing the POJO.
This is not a collection of pojos, so we are surprised it is lazily loaded at all. Also, the parent class (test.Item) has the property default-lazy="false" set on the whole class. This lazy=false setting seems to work for other one-to-many relationships that have not been converted to annotations.
Is there some lazy setting on the sub-item (test.RelatedItem) itself that is overriding the settings of the parent class?
The sub-item itself can be explicitly saved and loaded without any problem, but when saved as a related item in a one-to-many we get this error.
Hibernate version:
3.2.5
annotations 3.2.0
Mapping documents:
We have one mapping document that is a hbm.xml file and another that is specified via annotations. We are starting the process of incrementally moving to annotations.
Item.hbm.xml (edited for brevity):
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false"
>
<class
name="test.Item"
table="items"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="guid"
column="item_guid"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>
<many-to-one
name="relatedItem"
class="test.RelatedItem"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
>
<column
name="relateditem_guid"
/>
</many-to-one>
</class>
</hibernate-mapping>
New annotated class (edited for brevity):
Code:
package test;
import javax.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(name="relateditem")
public class RelatedItem extends Persistent
implements Serializable {
private String guid;
private String description;
public RelatedItem() { }
@Id
@Column(name="RelatedItem_GUID")
public String getGuid() {
return this.guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
@Basic @Column(length=50, name="description")
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}
hibernate.cfg.xml section:
Code:
<session-factory>
<mapping resource="test/item.hbm.xml"/>
<mapping class="test.Place"/>
</session-factory>
Full stack trace of any exception that occurs:Code:
ERROR 2008-01-17 16:31:18,425 [exception.ExInternalException] line:32 - Internal Exception:: Error opening ItemReport(0a0a-2283-11788172aa1-b6220b9f-0-fd51)
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at test.RelatedItem$$EnhancerByCGLIB$$28d6ed11.getDescription(<generated>)
at test.RelatedItemButton.updateDisplay(RIButton.java:138)
...