Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
2.1.8
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.myapp.model.hibernate">
<class name="Event"
table="EVENT"
lazy="true">
<meta attribute="implements">java.lang.Cloneable</meta>
<!-- Common id property. -->
<id name="id"
type="long"
column="EVENT_ID"
unsaved-value="null">
<generator class="native"/>
</id>
<property name="date"
update="true"
type="date">
<column name="DATE" not-null="true" length="12"/>
</property>
<set name="documents" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="EVENT_ID"/>
<one-to-many class="Document"/>
</set>
<many-to-one name="type" column="EVENT_TYPE_ID" class="EventType"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.myapp.model.hibernate">
<class name="Document"
table="DOCUMENT"
lazy="true">
<!-- Common id property. -->
<id name="id"
type="long"
column="DOCUMENT_ID"
unsaved-value="null">
<generator class="native"/>
</id>
<property name="name"
update="true"
type="string">
<column name="NAME" not-null="false" length="75"/>
</property>
<list name="information" lazy="true">
<key column="DOCUMENT_ID"/>
<index column="SEQUENCE"/>
<one-to-many class="Information"/>
</list>
<many-to-one name="event" column="EVENT_ID" class="Event"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public boolean updateEvent(Event evt) {
boolean returnv = false;
Session session = null;
Transaction tx = null;
try {
session = HibUtil.getSession();
tx = session.beginTransaction();
session.update(evt);
session.flush();
tx.commit();
returnv = true;
} catch (HibernateException he) {
he.printStackTrace();
} finally {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return returnv;
}
public List getDocumentsForEvent(Event event) {
List docs = null;
Session session = null;
Transaction tx = null;
try {
session = HibUtil.getSession();
tx = session.beginTransaction();
if (!session.contains(event)) {
System.out.println("Session locking event: " + event.getId());
session.lock(event, LockMode.UPGRADE);
} else
System.out.println("Session already has event: " + event.getId());
Hibernate.initialize(event.getDocuments());
docs = new java.util.ArrayList(event.getDocuments());
tx.commit();
session.close();
} catch (HibernateException he) {
he.printStackTrace();
} finally {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return docs;
}
Name and version of the database you are using:PostgreSQL 7.4
The generated SQL (show_sql=true):Code:
Session locking event: 122
Hibernate: select EVENT_ID from EVENT where EVENT_ID =? for update
Hibernate: select documents0_.EVENT_ID as EVENT_ID__, documents0_.DOCUMENT_ID as DOCUMENT1___, documents0_.DOCUMENT_ID as DOCUMENT1_3_, documents0_.NAME as NAME3_, documents0_.EVENT_ID as EVENT_ID3_, from DOCUMENT documents0_ where documents0_.EVENT_ID=?
Hibernate: update EVENT set DATE=?, EVENT_TYPE_ID=? where EVENT_ID=?
Session locking event: 122
Hibernate: select EVENT_ID from EVENT where EVENT_ID =? for update
My code calls getDocumentsForEvent(eventObj) first and retrieves a Set of Documents. On the first pass, this works just fine.
The code then changes the date property of the event and calls updateEvent(eventOjb). This works fine as well...and you can see the update call in the generated SQL.
Finally, the code calls getDocumentsForEvent(eventObj) again to retrieve the list of objects again. However, this time the Documents are *not* retrieved. The call to event.getDocuments() in getDocumentsForEvent simply returns an empty Set.
I've toggled various things to try to change this, but nothing seems to work. The association is still there in the database, and if I restart the application, the Documents are retrieved properly again. However, another call to updateEvent and the same behavior results.
I'm quite flabbergasted, honestly.
Can someone please point me at the grave mistake I've made?
Thanks!
Jbwiv