Hibernate version: v2.1
Mapping documents:
Definition 1:
<class name="com.test.hibernate.ContactRecord" table="ContactRecord">
<id name="recId" column="contactId">
<generator class="sequence">
<param name="sequence">SEQ_ContactRecord</param>
</generator>
</id>
<property name="contactDate" column="contactDate"/>
<property name="duration" column="callDuration"/>
<property name="memo" column="contactMemo"/>
<property name="repId" column="userId"/>
<set name="files" table="ContactFileRecord" lazy="true" inverse="true" cascade="all">
<key column="crecId" />
<many-to-many class="com.test.hibernate.AttachFile" column="fileId"/>
</set>
</class>
Definition 2:
<class name="com.test.hibernate.AttachFile" table="AttachFile">
<id name="fileId" column="fileId">
<generator class="sequence">
<param name="sequence">SEQ_AttachFile</param>
</generator>
</id>
<property name="fileName" column="fileName"/>
<property name="filePath" column="filepath"/>
</class>
Code between sessionFactory.openSession() and session.close():
Code:
try{
Session s = DemoSession.currentSession();
AttachFile file = new AttachFile();
file.setFileName( "test1.jpg" );
file.setFilePath( "P:\\temp\\sample\\" );
ContactRecord cr = new ContactRecord();
cr.setContactDate( new Date());
cr.setMemo( "this is a test..." );
cr.setRepId( "TVSZZ10" );
cr.setWarning( true );
cr.setDuration( 150 );
cr.setHasFileAttached( true );
cr.getFiles().add( file );
Transaction t = s.beginTransaction();
s.save( cr );
t.commit();
DemoSession.closeSession();
}catch( Exception he ){
System.out.println( "HibernateException: " + he.getLocalizedMessage());
he.printStackTrace();
try{
DemoSession.closeSession();
}catch( HibernateException e ){}
Name and version of the database you are using:
ORACLE9i
The generated SQL (show_sql=true):
Hibernate: select SEQ_ContactRecord.nextval from dual
Hibernate: insert into contactrecord (contactDate, callDuration, contactMemo, callRepId, contactId) values (?, ?, ?, ?, ?)
Hibernate: insert into ContactFileRecord (crecId, fileId) values (?, ?)
Database Operation Result:
At table ContactRecord:
CONTACTID USERID NOTICE CONTACTMEMO CALLDURATION HASFILE
---------- --------- ---------- ------------ ---------- -------
29 TVSZZ10 1 this is a test... 150 1
At table ContactFileRecord:
FILEID CRECID CFRID
---------- ---------- ----------
0 29 1
At table AttachFile:
-NONE-
PROBLEM:
The above code works fine at first look.. but upon checking the result in the database tables, I found out the following.
- AttachFile table doesnt contain an entry!.
- Which is the reason why the field, FILEID, in the ContactFileRecord is (0)zero.
I really appreciate of your help. I will owe it to you.
Thanks In Advance!.