I read this chapter, but there is not mentioned that is correct behaviour that a call to session.isDirty performs save.
The problem is that when i call session.isDirty a save operation is fired even if it's not necessary.
Here is my short example:
Posted: Thu Oct 27, 2005 2:13 pm Post subject: Session.isDirty() perfoms save ?
Hi,
When calling Session.isDirty() (Hibernate 3.0.5, non-classic) Hibernate performs implicitly a save and generates the SQL for retrieving the new id (from the sequence).
Here is my example:
Session ses=SessionManager.currentSession();
TblDocumenti doc = (TblDocumenti)ses.load(TblDocumenti.class, new Long(506));
TblSequenze seq = (TblSequenze)TblSequenze.class.newInstance();
seq.setDoc(doc);
seq.setSezione(new Short("2"));
doc.getTblSequenze().add(seq);
// if you omit the following line the id is retrieved when calling flush
if (ses.isDirty()) {System.out.println("isDirty");} // The previous call performs a save and a call to nextvalue of the sequence
Transaction t = ses.beginTransaction();
ses.flush();
t.commit();
Here are the xml files:
TblDocumenti:
<class dynamic-update="true" lazy="false" name="TblDocumenti"
schema="GAM_ADM" table="TBL_DOCUMENTI">
<id name="id" type="long">
<column index="PK_TBL_DOCUMENTI" name="ID" not-null="true"
precision="11" scale="0"/>
<generator class="sequence">
<param name="sequence">S_470_1_TBL_DOCUMENTI</param>
</generator>
</id>
<version column="VERSION" name="version" type="integer"/>
<property column="xxx" lazy="false" name="xxx" type="string"/>
</property>
<bag access="field" cascade="save-update" inverse="true" lazy="true" name="tblSequenze">
<key foreign-key="TBL_SEQ_DOC">
<column index="DOC_ID" name="DOC_ID" not-null="true" precision="11" scale="0"/>
</key>
<one-to-many class="" entity-name="microfilm.model.TblSequenze" />
</bag>
</class>
TblSequenze
<class name="TblSequenze" lazy="false" table="TBL_SEQUENZE" schema="GAM_ADM" optimistic-lock="none" dynamic-update="true">
<id name="id" type="long" unsaved-value="null">
<column name="ID" precision="11" scale="0" not-null="true" index="ID"/>
<generator class="sequence" >
<param name="sequence">S_473_1_TBL_SEQUENZE</param>
</generator>
</id>
<property name="sezione" type="short">
<column name="SEZIONE" precision="5" scale="0"/>
</property>
<many-to-one lazy="false" access="field" name="doc" entity-name="microfilm.model.TblDocumenti" cascade="none" foreign-key="TBL_SEQ_DOC">
<column name="DOC_ID" precision="11" scale="0" not-null="true" index="DOC_ID"/>
</many-to-one>
</class>
Thanks
|