-->
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.  [ 5 posts ] 
Author Message
 Post subject: Strange Problem while saving a parent-child <one-to-many&
PostPosted: Wed Aug 27, 2003 2:01 am 
Newbie

Joined: Wed Aug 27, 2003 1:37 am
Posts: 10
Location: Kyrgyzstan
Hello,

I have been struggling for quite some time with this problem. I have a parent child relationship that I want to save to the database. When I call session.saveOrUpdate(myParent); The program hangs forever. This relationship works fine for reading data already saved in the database, or saving the parent when the child collection is empty. Here is my java code:


Session session = null;
try
{
session = database.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
System.out.println("BEFORE SAVE");
session.saveOrUpdate(filter);
System.out.println("SAVED");
session.delete(filter);
System.out.println("DELETED");
transaction.commit();
}
catch(HibernateException e)
{
e.printStackTrace();
}
finally
{
System.out.println("FINALLY");
try
{
session.close();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}

Here is the mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.atwoodsoft.data.Language" table="Language">
<id name="id" type="int" column="id" unsaved-value="-1" >
<generator class="assigned"/>
</id>
<property name="name" column="name" type="string" length="50" not-null="true"/>
<property name="localName" column="localName" type="string" length="50" not-null="true"/>
<property name="languageCode" column="languageCode" type="string" length="50" not-null="true"/>
</class>
<class name="com.atwoodsoft.tools.translator.LanguageFilter" table="LanguageFilter">
<id name="id" type="int" column="id" unsaved-value="-1" >
<generator class="native"/>
</id>
<property name="nullFilterMode" column="nullFilterMode" type="int" not-null="true"/>
<property name="index" column="index" type="int" not-null="true"/>
<property name="filterExpression" column="filterExpression" type="string" not-null="false"/>
<many-to-one name="language" class="com.atwoodsoft.data.Language" column="language"/>
<many-to-one name="filter" class="com.atwoodsoft.tools.translator.TranslatorFilter" column="filter" not-null="true"/>
</class>
<class name="com.atwoodsoft.tools.translator.TranslatorFilter" table="TranslatorFilter">
<id name="id" type="int" column="id" unsaved-value="-1" >
<generator class="native"/>
</id>
<property name="name" column="name" type="string" not-null="true" length="50" />
<property name="description" column="description" type="string" not-null="false" length="100"/>
<list name="selectedLanguageFilters" cascade="all" inverse="true">
<key column="filter"/>
<index column="index"/>
<one-to-many class="com.atwoodsoft.tools.translator.LanguageFilter"/>
</list>
<!--bag name="selectedLanguageFilters" cascade="all" inverse="true">
<key column="filter"/>
<one-to-many class="com.atwoodsoft.tools.translator.LanguageFilter"/>
</bag-->
</class>
</hibernate-mapping>


And here is the relavent portion of the hibernate log:

BEFORE SAVE
22:57:57,675 DEBUG Cascades:190 - unsaved-value: -1
22:57:57,677 DEBUG SessionImpl:1213 - saveOrUpdate() unsaved instance with id: -1
22:57:57,679 DEBUG SessionImpl:671 - saving [com.atwoodsoft.tools.translator.TranslatorFilter#<null>]
22:57:57,681 DEBUG Cascades:301 - processing cascades for: com.atwoodsoft.tools.translator.TranslatorFilter
22:57:57,684 DEBUG Cascades:308 - done processing cascades for: com.atwoodsoft.tools.translator.TranslatorFilter
22:57:57,738 DEBUG SessionImpl:2506 - Wrapped collection in role: com.atwoodsoft.tools.translator.TranslatorFilter.selectedLanguageFilters


Any suggestions or advice would be greatly appreciated.

John Atwood


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No idea. How about you run it in the debugger and pause the program during the hang; see what it is waiting for.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:20 am 
Newbie

Joined: Wed Aug 27, 2003 1:37 am
Posts: 10
Location: Kyrgyzstan
When running the debugger this is what I found:

/**Adds a new <code>LanguageFilter</code> to this <code>TranslatorFilter</code>. If a <code>LanguageFilter</code>
* is already registered with the same language then it is overritten in favor of the new <code>LanguageFilter</code>*/
public void addLanguageFilter(LanguageFilter filter)
{
if(getLanguageFilter(filter.getLanguage()) != null) //if we already have a LanguageFilter for the specified language
{
int index = selectedLanguageFilters.indexOf(getLanguageFilter(filter.getLanguage()));
selectedLanguageFilters.add(index,filter); //GAVIN This is where the program stops
filter.setIndex(index);
}
else
{
selectedLanguageFilters.add(filter);
reOrder();
}
filter.setFilter(this);
}

Here is the stack trace.

addLanguageFilter():171, com.atwoodsoft.tools.translator.TranslatorFilter
setSelectedLanguageFilters():103, com.atwoodsoft.tools.translator.TranslatorFilter
setPropertyValues(), com.atwoodsoft.tools.translator.TranslatorFilterMetaClass0
setPropertyValues():158, net.sf.hibernate.persister.AbstractEntityPersister
doSave():724, net.sf.hibernate.impl.SessionImpl
save():620, net.sf.hibernate.impl.SessionImpl
saveOrUpdate():1214, net.sf.hibernate.impl.SessionImpl

From this stack trace I am guesing that Hibernate saves my data succesfully to the database and then, within the same transaction, tries to reload the object being saved. The problem seems to be caused when hibernate tries to set the languageFilter's property. This is probably because the setSelectedLanguageFilters method implements logic to ensure that the order of LanguageFilters in the list is corrospondent to their index property. I am wondering why does hibernate try to repopulate an object that has just been saved?

Thanks,

John


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate is allowed to call your getters/setters at ANY time. For example, it might like to wrap a collection. Or, it might want to increment a version number. Or, you might have an Interceptor defined.


You should implement get/set pairs so that it is impossible to cause an infinite loop. By the look of the code, you have some dodgy recursion going on.


Top
 Profile  
 
 Post subject: Your a lifesaver
PostPosted: Wed Aug 27, 2003 2:30 am 
Newbie

Joined: Wed Aug 27, 2003 1:37 am
Posts: 10
Location: Kyrgyzstan
Thanks Gavin,

Adding the line:

public void setSelectedLanguageFilters(List languageFilters)
{
if(!langaugeFilters.equals(selectedLanguageFilters))
{
//do collection processing work here
}
...
}

seems to have made things work. I will continue to work on it and perhaps switch to a more simple list mapping.

Thanks,
John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.