Folks,
I'm looking for the prescribed way to use hibernate in a 3 tier web app
with struts and session beans facades. I have one issue that I can't
resolve. I've read the docs and FAQs, searched the forums and I own
the book.
When I update a record that is the child side of a parent child relationship,
the relationhip is broken. This is happening because the parent is not set
on the child POJO from the struts action. Basically, when the form is
saved, a new POJO is created and it's properties are set, minus any
associations (FKs). It doesn't seem right to have to load the associations
and set them if they haven't (in this case, can't) change. That POJO is
then passed to the session facade and session.update is called. See below for code.
If someone could give me some strategy options here, I would greatly
appreciate it. I'm certainly not the first person to have this issue.
Hibernate version:
2.1
Mapping documents:
Code:
<class
name="Location"
table="Location"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="guid"
column="GUID"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>
<property
name="locationName"
...
<many-to-one
name="company"
class="Company"
cascade="none"
outer-join="auto"
update="true"
insert="true"
>
<column
name="CompanyGUID"
/>
</many-to-one>
</class>
<class
name="Company"
table="Company"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="guid"
column="GUID"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>
<property
name="companyName"
type="java.lang.String"
...
<set
name="locations"
lazy="true"
inverse="true"
cascade="none"
sort="unsorted"
>
<key
column="CompanyGUID"
/>
<one-to-many
class="Location"
/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():Code:
sess.update(location);
sess.flush();
Name and version of the database you are using:sql server 2000
Debug level Hibernate log excerpt:Code:
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.persister.EntityPersister] Updating entity: [Location#6bbf9c7c-6748-4282-baee-400c03807dcc]
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.impl.BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.SQL] update Location set LocationName=?, Address1=?, Address2=?, Municipality=?, Region=?, PostalCode=?, CountryCode=?, Timezone=?, CompanyGUID=? where GUID=?
2005-02-16 09:37:27,491 INFO [STDOUT] Hibernate: update Location set LocationName=?, Address1=?, Address2=?, Municipality=?, Region=?, PostalCode=?, CountryCode=?, Timezone=?, CompanyGUID=? where GUID=?
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.impl.BatcherImpl] preparing statement
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.persister.EntityPersister] Dehydrating entity: [Location#6bbf9c7c-6748-4282-baee-400c03807dcc]
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.type.StringType] binding 'Greg' to parameter: 1
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.type.StringType] binding '801 ' to parameter: 2
2005-02-16 09:37:27,491 DEBUG [net.sf.hibernate.type.StringType] binding '' to parameter: 3
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding 'something' to parameter: 4
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding 'something' to parameter: 5
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding 'something' to parameter: 6
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding 'something' to parameter: 7
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding 'something' to parameter: 8
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding null to parameter: 9
2005-02-16 09:37:27,501 DEBUG [net.sf.hibernate.type.StringType] binding '6bbf9c7c-6748-4282-baee-400c03807dcc' to parameter: 10
2005-02-16 09:37:27,611 DEBUG [net.sf.hibernate.impl.BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
2005-02-16 09:37:27,611 DEBUG [net.sf.hibernate.impl.BatcherImpl] closing statement
2005-02-16 09:37:27,611 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2005-02-16 09:37:27,611 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session