-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: LazyInitializationException when migrating to hibernate 3.1
PostPosted: Mon Jan 23, 2006 9:39 am 
Newbie

Joined: Fri Apr 22, 2005 11:30 am
Posts: 5
Hi,

I try to migrate my application from H2.1.8 to 3.1 (newest download). I am stack with the org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed when accessing the list collection. I carefully read the migration guide, made sure my mapping poinst to 3.0dtd as well that this collection has lazy="false". No idea what is wrong - any help would be greatly appretiated!

My mapping:

<?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
>
<class
name="com.poeware.ephotoshop.prods.AttributeValueEnumeration"
table="AttribValueEnums"
>

<id
name="id"
column="Id"
type="long"
unsaved-value="-1"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-AttributeValueEnumeration.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<list
name="enumElements"
lazy="false"
cascade="all"
outer-join="true"
>

<key
column="EnumerationId"
>
</key>

<index
column="ListNdx"
/>

<one-to-many
class="com.poeware.ephotoshop.prods.AttributeValueEnumerationElement"
/>

</list>

<property
name="attributeTypeId"
type="int"
update="true"
insert="true"
column="AttributeTypeId"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-AttributeValueEnumeration.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>



and the stacktrace:

2006-01-23 14:19:28,066 ERROR [org.hibernate.LazyInitializationException] could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at com.poeware.ephotoshop.prods.AttributeValueEnumeration$$EnhancerByCGLIB$$2f0b494.getEnumElements(<generated>)
at com.poeware.ephotoshop.web.CustomerState.getEnumerationElements(CustomerState.java:923)
at org.apache.jsp.WEB_002dINF.pages.adminEnumConfigurationForWizard_jsp._jspService(adminEnumConfigurationForWizard_jsp.java:109)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

Thanks a lot for any help!
Bartek


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 12:06 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
try putting lazy="false" in the class declaration as well.

If not, maybe the outer-join="true" is screwing with it. See if it needs join="true" to accompany outer-join="true".

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:46 am 
Newbie

Joined: Fri Apr 22, 2005 11:30 am
Posts: 5
Hello Chris,

Thanks so much for your help.

I tried both: adding lazy="false" on the class level as well as adding fetch="join" which I think was what you mean as join="true" was giving me an error. Unfortunately the problem is still there. Any idea what can I yet try? I generate all that with Xdoclet 1.2.3 - do you think that could be a problem?
I attach the xdoclet code of the class header as well as this getter.

* @hibernate.class
* table = "AttribValueEnums"
* lazy = "false"
*/
public class AttributeValueEnumeration extends PersistentEntity{

...



/**
* Get associated AttributeValueEnumerationElements.
*
* @return the contained com.poeware.ephotoshop.prods.AttributeValueEnumerationElement.
*
* @hibernate.list
* role="attributeValueElements"
* lazy="false"
* cascade="all"
* join="true"
* @hibernate.collection-key
* column="EnumerationId"
* @hibernate.collection-index
* column = "ListNdx"
* @hibernate.collection-one-to-many
* class="com.poeware.ephotoshop.prods.AttributeValueEnumerationElement"
*
*/
public List getEnumElements() {
return enumElements;
}


Thanks again,
Bartek


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 10:49 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yeah, fetch="join" was what I meant. did your xdoclet stuff add the lazy="false" to the mapping file ?

Code:
<hibernate-mapping
>
<class
name="com.poeware.ephotoshop.prods.AttributeValueEnumeration"
table="AttribValueEnums"
lazy="false"
>


If this isn't it, post the code where your calling it from.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 11:12 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
you should post your source code. From the excpetion I assume that you are just reading your collection which is not initialized but your session is allready closed. Are you reading or writing something to AttributeValueEnumeration (your cascade = true) so also writing could be an issue.

You could explicitly fetch the childs using
select a from AttributeValueEnumeration left join fetch ...

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 6:55 am 
Newbie

Joined: Fri Apr 22, 2005 11:30 am
Posts: 5
Hi,
Thanks so much for the replies! Here is attached mapping file


<?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
>
<class
name="com.poeware.ephotoshop.prods.AttributeValueEnumeration"
table="AttribValueEnums"
lazy="false"
>

<id
name="id"
column="Id"
type="long"
unsaved-value="-1"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-AttributeValueEnumeration.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<list
name="enumElements"
lazy="false"
cascade="all"
fetch="join"
>

<key
column="EnumerationId"
>
</key>

<index
column="ListNdx"
/>

<one-to-many
class="com.poeware.ephotoshop.prods.AttributeValueEnumerationElement"
/>

</list>

<property
name="attributeTypeId"
type="int"
update="true"
insert="true"
column="AttributeTypeId"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-AttributeValueEnumeration.xml
containing the additional properties and place it in your merge dir.
-->

</class>

And the class code:
/**
*
* Class representing the enumeration of the attribute values. This class is referenced
* by EnumAttribute, and holds the list of possible AttributeValues, referenced via AttributeValueEnumerationElements.
* Because of such a many-to-one relationship, when enumeration is updated, the change will be visible to all
* referencing EnumAttributes.
*
* The ID of the enumeration will correspond to the AttributeTypeId of the attribute, as defined
* in AttributeFactory class.
*
* @hibernate.class
* table = "AttribValueEnums"
* lazy = "false"
*/
public class AttributeValueEnumeration extends PersistentEntity{
protected AttributeValueEnumeration(){
super();
}

public AttributeValueEnumeration(EnumAttribute attribute){
super();
this.setAttributeTypeId(attribute.getAttributeTypeId());
attribute.setEnumeration(this);
}

/**
*
* @return List of all AttributeValues from this AttributeValueEnumeration
*/
public List getAllValues(){
List list = new ArrayList();
for(int i=0; i<this.enumElements.size(); i++){
AttributeValueEnumerationElement valEl = (AttributeValueEnumerationElement) this.enumElements.get(i);
AttributeValue attrVal = valEl.getAttributeValue();
if( attrVal != null ){
list.add(attrVal);
}
}
return list;
}

/**
*
* @return List of all Enabled AttributeValues from this AttributeValueEnumeration
*/
public List getEnabledValues(){
List list = new ArrayList();
for(int i=0; i<this.enumElements.size(); i++){
AttributeValueEnumerationElement valEl = (AttributeValueEnumerationElement) this.enumElements.get(i);
AttributeValue attrVal = valEl.getAttributeValue();
if( attrVal != null && attrVal.isEnabled() ){
list.add(attrVal);
}
}
return list;
}

/**
*
* @return List of all Disables AttributeValues from this AttributeValueEnumeration
*/
public List getDisabledValues(){
List list = new ArrayList();
for(int i=0; i<this.enumElements.size(); i++){
AttributeValueEnumerationElement valEl = (AttributeValueEnumerationElement) this.enumElements.get(i);
AttributeValue attrVal = valEl.getAttributeValue();
if( attrVal != null && attrVal.isEnabled() == false){
list.add(attrVal);
}
}
return list;
}

/**
* Adds an AttributeValueEnumerationElement to the AttributeValueEnumeration,
* making sure the values in the AttributeValueEnumeration are UNIQUE
* @param enumElement AttributeValueEnumerationElement to be added
*/
public void addEnumerationElement(AttributeValueEnumerationElement enumElement){
boolean unique = true;
//make sure the values within the enumeration (referenced by the AttributeValueEnumerationElements
//are unique
for(int i=0; i<this.enumElements.size(); i++){
AttributeValueEnumerationElement enumE = (AttributeValueEnumerationElement) this.enumElements.get(i);
AttributeValue val = (AttributeValue)enumE.getAttributeValue();
if( val.isTheSame(enumElement.getAttributeValue()) ){
unique = false;
break;
}
}
if(unique == true){
this.enumElements.add(enumElement);
}
}

/**
* Adds an AttributeValueEnumerationElement to the AttributeValueEnumeration,
* making sure the values in the AttributeValueEnumeration are UNIQUE
* @param pos position in the List on which to add the AttributeValueEnumerationElement
* @param enumElement AttributeValueEnumerationElement to be added
*/
public void addEnumerationElement(int pos, AttributeValueEnumerationElement enumElement){
boolean unique = true;
//make sure the values within the enumeration (referenced by the AttributeValueEnumerationElements
//are unique
for(int i=0; i<this.enumElements.size(); i++){
AttributeValueEnumerationElement enumE = (AttributeValueEnumerationElement) this.enumElements.get(i);
AttributeValue val = (AttributeValue)enumE.getAttributeValue();
if( val.isTheSame(enumElement.getAttributeValue()) ){
unique = false;
break;
}
}
if(unique == true){
this.enumElements.add(pos, enumElement);
}
}

/**
* Removes the AttributeValueEnumerationElement from the List of AttributeValueEnumerationElements
* associated with the AttributeValueEnumeration.
* @param enumElement AttributeValueEnumerationElement to be removed
*/
public void removeEnumerationElement(AttributeValueEnumerationElement enumElement){
this.enumElements.remove(enumElement);
}

/**
* Get associated AttributeValueEnumerationElements.
*
* @return the contained com.poeware.ephotoshop.prods.AttributeValueEnumerationElement.
*
* @hibernate.list
* role="attributeValueElements"
* lazy="false"
* cascade="all"
* join="true"
* @hibernate.collection-key
* column="EnumerationId"
* @hibernate.collection-index
* column = "ListNdx"
* @hibernate.collection-one-to-many
* class="com.poeware.ephotoshop.prods.AttributeValueEnumerationElement"
*
*/
public List getEnumElements() {
return enumElements;
}

private void setEnumElements(List enumElements) {
this.enumElements = enumElements;
}

/**
* This method "recompiles" the Enumeration - ensuring its internal integrity.
* Should be called after each modification made to the NumericRangeAttributeValues
* of the enumeration. Especially designed to support NumericRangeAttributeValues
* making sure that adding, removing or modifying those values (from within this
* enumeration) will remain consistent: there will be no gaps within the total range.<br>
* This method does nothing, if the associated AtributeValues are not
* NumericRangeAttributeValues.
* @throws BusinessLogicException if there were problems or unrecovable inconsistencies found
* (like AttributeValues other than instances of NumericRangeAttributeValues found)
*/
public void recompileNumericRange() throws BusinessLogicException{
if(this.enumElements.size() > 0 ){
//check whether this method applies to this enumeration
AttributeValueEnumerationElement el = (AttributeValueEnumerationElement)this.enumElements.get(0);
if((el.getAttributeValue() instanceof NumericRangeAttributeValue)==false) return;
}else{
//initialize this enumeration with the full range NumericRangeAttributeValue
new AttributeValueEnumerationElement(this,
new NumericRangeAttributeValue(NumericRangeAttributeValue.MIN_RANGE_VALUE,
NumericRangeAttributeValue.MAX_RANGE_VALUE),
I18n.getString("entirePossibleRange"),"entirePossibleRangeDesc");
}
/* ok, we need to recompile the range: we need to make sure that the entire range
* defined in the NumericRangeAttributeValue class will be covered without the gaps.
* Algorythm:
* 1) we will sort ascending all contained NumericRangeAttributeValues by their
* MIN values
* 2) we make sure that the first NumericRangeAttributeValue start from
* MIN_RANGE_VALUE defined in NumericRangeAttributeValue class
* 3) we make sure that the last NumericRangeAttributeValue ends at
* MAX_RANGE_VALUE defined in NumericRangeAttributeValue class
* 4) each range will start at its MIN value and will last till MIN
* value of the next range value (the last one will end at MAX_RANGE_VALUE)
*/
try {
//soriting
TreeMap tree = new TreeMap();
for(int cnt=0; cnt<this.enumElements.size(); cnt++){
AttributeValueEnumerationElement el = (AttributeValueEnumerationElement)this.enumElements.get(cnt);
NumericRangeAttributeValue val = (NumericRangeAttributeValue) el.getAttributeValue();
tree.put( new Double(val.getMinValue()), el);
}
//now, clean the collection of the enumeration elements
this.enumElements.clear();
//we will add them again in the proper order
Collection values = tree.values();
int cnt = 0;
NumericRangeAttributeValue prevValue = null;
for (Iterator i = values.iterator(); i.hasNext();) {
AttributeValueEnumerationElement el = (AttributeValueEnumerationElement) i.next();
NumericRangeAttributeValue v = (NumericRangeAttributeValue) el.getAttributeValue();
if( cnt == 0){
//make sure the first range starts from the MIN_RANGE_VALUE
v.setMinValue(NumericRangeAttributeValue.MIN_RANGE_VALUE);
this.addEnumerationElement(el);
}else if( v.getMinValue() > NumericRangeAttributeValue.MIN_RANGE_VALUE){
prevValue.setMaxValue(v.getMinValue());
this.addEnumerationElement(el);
}
//make sure the last range ends at MAX_RANGE_VALUE
v.setMaxValue(NumericRangeAttributeValue.MAX_RANGE_VALUE);
prevValue = v;
cnt++;
}
} catch (Exception e) {
throw new BusinessLogicException("Exception caught when recompiling numeric range", e);
}
}

/**
*
* @return new AttributeValueEnumeration instance as a copy of this AttributeValueEnumeration instance. The returned instance
* will have id = PersistentEntity.NO_ID_YET, which will become valid after saving instance
*/
public AttributeValueEnumeration createCopy(EnumAttribute attr){
try {
AttributeValueEnumeration copy = new AttributeValueEnumeration(attr);

// copy the contents of priceComponents collection
copy.enumElements.clear();
List l = this.getEnumElements();
for (int i = 0; i < l.size(); i++) {
AttributeValueEnumerationElement originalEnumEl = (AttributeValueEnumerationElement) l.get(i);
AttributeValueEnumerationElement copyEnumEl = originalEnumEl.createCopy();
copy.enumElements.add(copyEnumEl);
}
return copy;
} catch (Exception e) {
Terminator.fatal("unnable to create a copy of AttributeValueEnumeration!", e);
return null;
}
}

/**
* @hibernate.property
* column = "AttributeTypeId"
* @return Returns the attributeTypeId.
*/
public int getAttributeTypeId() {
return attributeTypeId;
}

public void setAttributeTypeId(int id) {
this.attributeTypeId = id;
}

public boolean equals(Object obj) {
if ( obj == null) return false;
AttributeValueEnumeration other = (AttributeValueEnumeration)obj;
return (this.attributeTypeId == other.attributeTypeId) ? true : false;
}

public int compareTo(Object obj) {
AttributeValueEnumeration other = (AttributeValueEnumeration)obj;
return new Long(this.attributeTypeId).compareTo(new Long(other.attributeTypeId));
}

public int hashCode() {
return (int)(this.attributeTypeId % Integer.MAX_VALUE);
}

private int attributeTypeId = -1;
private List enumElements = new ArrayList();
private static final long serialVersionUID = 1L;
}

All that works within JBoss. I get the the AttributeValueEnumereation class via EJB call and then (replying on its lazy=false) use the internal lists of AttributeValueEnumerationElements that couse the problem. That call is been performed from servlet.

One think I simply cannot understand: everything worked pretty good with Hibernate 2.1.8, but I cannot simply get it running on the 3.1.1. I read the migration guide several times... changed all I could to lazy=false, but it still doesn't help.
Many thanks for any further ideas!
Cheers,
Bartek


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.