-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: heres the XML mapping, the code, hibernate msg log....
PostPosted: Fri Nov 19, 2004 12:19 pm 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
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!.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
look for inverse explanation.... on wiki

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 8:41 pm 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
anthony wrote:
look for inverse explanation.... on wiki


hello!. sorry but where can i find the wiki?.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 12:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
All the documentation is within the wiki.
Look for the link on the menu on the left side of the hibernate web site.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 11:21 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
anthony wrote:
look for inverse explanation.... on wiki


oh, i got it right and now it looks fine!.
what i did is, i removed the inverse="true" and cascade="all" attributes
from the set element in the ContactRecord class!.

what im worried now is if there's a side effect to what i did.

am i doing right here?
i must admit, i still got to understand what inverse="true" really is for.
still have some few readings about this.

kindly advise. thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 1:39 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
inverse= true means that working on the collection doesn't affect the the db, if you add or remove an element, you won't see any insert or delete...

this is usefull for bidirectionnal association to avoid 2 SQL updates instead of one.

So if you have A 1--* B
in A.hbm.xml, set inverse = true in the collection declaration
in B.hbm.xml, just map A as a many-to-one association

and the SQL will be executed when doing b.setA(a), or b.setA(null)
don"t forget you have to be ok with your domain model, that means you must also operate on the collection to have good instances.

You should read hibernate in action, you'll learn how to do it very easily

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.