it seems I am doomed to a life of working with legacy databases :(
hibernate version
3.0.5
I have a composite key with a foreign key that points to its parent's auto-generated primary key. The problem is when saving the parent, hibernate runs an update statement instead of an insert statement for the child object.
I can see in the log files it is assuming that the JobQueueReq (child object) is already associated with that session, and that's why it runs an update
the column jobq_seq_no in the JobQueueReq table is a foreign key relation to the column int_seq_no in the JobQueueRun table
parent mapping file snippet
Code:
...
<id
name="intSeqNo"
type="java.lang.Integer"
column="int_seq_no"
>
<generator class="native">
<param name="sequence">jobqr_seq_no</param>
</generator>
</id>
...
<set
name="jobQueueReqs"
table="job_queue_req"
cascade="all-delete-orphan"
lazy="false"
>
<key column="jobq_seq_no"/>
<one-to-many
class="dars.apis.audit.JobQueueReq"/>
</set>
...
child mapping file snippet
Code:
...
<composite-id name="comp_id" class="dars.apis.audit.JobQueueReqPK">
<key-many-to-one
name="jobQueueRun"
class="dars.apis.audit.JobQueueRun"
column="jobq_seq_no"
>
</key-many-to-one>
<key-property
name="rtabx"
column="rtabx"
type="java.lang.String"
length="3"
/>
</composite-id>
...
log file snippets
Code:
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.SQL - insert into job_queue_run (instidq, instid, instcd, jobid, userid, soprid, stuno, dprog, dpmask, catlyt, binstid, binstcd, rundate, jobq_sysout_used, runtime, ncol, complete, ip) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.jdbc.AbstractBatcher - preparing statement
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.persister.entity.BasicEntityPersister - Dehydrating entity: [dars.apis.audit.JobQueueRun#<null>]
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.type.StringType - binding
.... <more binding statements>...
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.type.StringType - binding null to parameter: 18
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.id.IdentifierGeneratorFactory - Natively generated identity: 12502313
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
... <here is where hibernate assumes that the child object is already associated with the session, hence it runs an update statement>...
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Cascades - cascading to saveOrUpdate: dars.apis.audit.JobQueueReq
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractSaveEventListener - persistent instance of: dars.apis.audit.JobQueueReq
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.DefaultSaveOrUpdateEventListener - ignoring persistent instance
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.DefaultSaveOrUpdateEventListener - object already associated with session: [dars.apis.audit.JobQueueReq#component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}]
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: dars.apis.audit.JobQueueRun.jobQueueReqs
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Cascades - deleting orphans for collection: dars.apis.audit.JobQueueRun.jobQueueReqs
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Cascades - done deleting orphans for collection: dars.apis.audit.JobQueueRun.jobQueueReqs
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: dars.apis.audit.JobQueueRun
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Collections - Collection found: [dars.apis.audit.JobQueueRun.jobQueueReqs#12502313], was: [<unreferenced>] (initialized)
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.engine.Collections - Collection found: [dars.apis.audit.JobQueueRun.jobQueueOuts#12502313], was: [<unreferenced>] (initialized)
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.DefaultFlushEntityEventListener - Updating entity: [dars.apis.audit.JobQueueReq#component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}]
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - Processing unreferenced collections
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 insertions, 1 updates, 0 deletions to 2 objects
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 2 (re)creations, 0 updates, 0 removals to 2 collections
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.pretty.Printer - listing entities:
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.pretty.Printer - dars.apis.audit.JobQueueReq{psname=null, rname=null, needct=0, gothrs=null, gpaname=null, maxhrs=null, gotgpahrs=null, gotct=0, satisfied=null, totalHour=null, ipct=0, totalGpa=null, hidden=null, reqgpa=null, gpalowyt=null, reqhrs=null, reqsrq=0, sortflg=null, estReqhrs=null, hidecategorygpa=null, summary=null, optreq=null, needsrq=0, gotgpapts=null, needhrs=null, comp_id=component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}, category=null, reqct=0, rno=null, reqgpaf=null, transcript=null, whatifct=0, gpahighyt=null, summaryGrp=null, orreq=null, hidefromchart=null, gotsrq=0, needgpa=null, whatifhrs=null, gotgpa=null, iphrs=null}
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.pretty.Printer - dars.apis.audit.JobQueueRun{runtime=null, jobQueueOuts=[], jobqSysoutUsed=null, intSeqNo=12502313, instid=null, dpmask=null, binstcd=null, dprog=null, jobQueueReqs=[dars.apis.audit.JobQueueReq#component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}], complete=null, soprid=null, ncol=null, catlyt=null, stuno=null, userid=null, rundate=null, instcd=null, instidq=null, jobid=null, ip=null, binstid=null}
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.event.def.AbstractFlushingEventListener - executing flush
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.persister.entity.BasicEntityPersister - Updating entity: [dars.apis.audit.JobQueueReq#component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}]
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
<the update statement>
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.SQL - update job_queue_req set rno=?, rname=?, psname=?, category=?, summary_grp=?, transcript=?, total_hour=?, total_gpa=?, hidefromchart=?, satisfied=?, summary=?, sortflg=?, orreq=?, optreq=?, reqsrq=?, gotsrq=?, needsrq=?, reqct=?, gotct=?, ipct=?, whatifct=?, needct=?, maxhrs=?, reqhrs=?, est_reqhrs=?, gothrs=?, iphrs=?, whatifhrs=?, needhrs=?, gotgpahrs=?, gotgpapts=?, reqgpa=?, gotgpa=?, needgpa=?, reqgpaf=?, gpaname=?, gpalowyt=?, gpahighyt=?, hidden=?, hidecategorygpa=? where jobq_seq_no=? and rtabx=?
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.jdbc.AbstractBatcher - preparing statement
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.persister.entity.BasicEntityPersister - Dehydrating entity: [dars.apis.audit.JobQueueReq#component[jobQueueRun,rtabx]{rtabx=123, jobQueueRun=dars.apis.audit.JobQueueRun#12502313}]
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.type.StringType - binding null to parameter: 1
... <more binding statements>
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.type.IntegerType - binding '12502313' to parameter: 41
07 Mar 2006 10:13:43 : [DEBUG] org.hibernate.type.StringType - binding '123' to parameter: 42
07 Mar 2006 10:13:43 : [ERROR] org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
saving code
Code:
public void testStuff() {
JobQueueRun jobQueueRun = new JobQueueRun();
JobQueueReq jobQueueReq = new JobQueueReq();
JobQueueReqPK jqpk = new JobQueueReqPK();
jqpk.setJobQueueRun(jobQueueRun);
jqpk.setRtabx("123");
jobQueueReq.setComp_id(jqpk);
Set <JobQueueReq> st = new LinkedHashSet<JobQueueReq>();
st.add(jobQueueReq);
jobQueueRun.setJobQueueReqs(st);
dao.save(jobQueueRun);
}