-->
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.  [ 9 posts ] 
Author Message
 Post subject: How can I do this mapping:
PostPosted: Fri Dec 02, 2005 2:26 pm 
Newbie

Joined: Fri Dec 02, 2005 8:03 am
Posts: 10
I´m looking for a way of doing the mapping for this relationship:

A Document entity has a reference for a File entity. The relationship is an unidirectional composition:a Document has 0 or 1 File and a File always belongs to one and only one Document. In the data base I have the Document table and the File table. The File table has the column “documentID” which is a foreign key that references the table Document.
How could I map this relationship in the Document hbm?

thanks in advance,
Fabrício Lemos

Hibernate version: 3.0.5


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 11:56 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Here's something that might work for your requirement:

Mapping for Document
Code:
<hibernate-mapping>
    <class name="com.hibernate.forums.Document"
           table="Document"
           >
        <id name="documentId" type="integer">
            <column name="documentId" />
            <generator class="assigned" />
        </id>           
      <set    name="fileList"
            cascade="all-delete-orphan"
            inverse="true"
            lazy="true"
            access="field"
            outer-join="true">
           <key>
               <column name="documentId" not-null="true"/>
           </key>
           <one-to-many class="com.hibernate.forums.File"/>
      </set>           
    </class>
</hibernate-mapping>



Mapping for File
Code:
<hibernate-mapping>
   <class name="com.hibernate.forums.File"
           table="File">
        <id name="fileId" type="integer">
            <column name="fileId" />
            <generator class="assigned" />
        </id>
        <property name="description" type="string">
            <column name="description"/>
        </property>   
    </class>
</hibernate-mapping>


You can look at:

http://forum.hibernate.org/viewtopic.ph ... highlight=

You can also look:
http://caveatemptor.hibernate.org
[/u][/b]


Top
 Profile  
 
 Post subject: Document has 0 or 1 File, and not a collection of then.
PostPosted: Sat Dec 03, 2005 1:36 am 
Newbie

Joined: Fri Dec 02, 2005 8:03 am
Posts: 10
In Document class I have:
Code:
class Document{
  private File file;
//... get and set methods
}


not:
Code:
class Document{
private List fileList;
//...get and set methods
}


Document has 0 or 1 File, not a collection of then.
So I don´t thing that the <set> mapping in document .hbm might work.

thanks anyway...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 11:09 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
I don't think you have a <one-or-zero> kind of mapping in Hibernate. Typically this is considered as a special case of <one-to-many> were the many is 0 or 1.

You could however still have "setFile" and getFile but use a Set in the implementation. Something like this:

Code:
public File getFile(){
   if(this.fileList != null && ! this.fileList.isEmpty()){
     return (File)fileList.get(0);
   }else{
     return null;
   }
}


You could also change the "access" mode to "field" in the HBM. This i think would then not require any "setters/getters" like getFileList(), setFileList(), there by not exposing more method than what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 10:44 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
one-to-one is what you want to use. For the case where object A doesn't have a B, A.getB() is null. There would be no corresponding row for B in this case. Manage the relationship using cascades.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 8:54 am 
Newbie

Joined: Fri Dec 02, 2005 8:03 am
Posts: 10
anandbn wrote:
I don't think you have a <one-or-zero> kind of mapping in Hibernate. Typically this is considered as a special case of <one-to-many> were the many is 0 or 1.

I´m sorry to hear that. This is a so common kind of relationship, that I thought hibernate would offer support.
Thanks for the answer.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 8:57 am 
Newbie

Joined: Fri Dec 02, 2005 8:03 am
Posts: 10
puffybsd wrote:
one-to-one is what you want to use. For the case where object A doesn't have a B, A.getB() is null. There would be no corresponding row for B in this case. Manage the relationship using cascades.


I can´t even start writing the .hbm mapping for this relationship.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 4:26 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
Here's some help:
Code:
<class name="Document">
  <one-to-one name="folder"/>
</class>
<class name="Folder">
  <id name="id" column="document_id">
    <generator class="foreign">
      <param property="document"/>
    </generator>
  </id>
  <one-to-one name="document" class="Document" constrained="true" />
</class>


If you are not already using XDoclet's hibernatedoclet, you really need to check it out. It's much easier to keep your source and mappings synchronized.


Top
 Profile  
 
 Post subject: The relationship is unidirectional.
PostPosted: Mon Dec 12, 2005 7:59 am 
Newbie

Joined: Fri Dec 02, 2005 8:03 am
Posts: 10
The relationship is unidirectional: in File class I don´t have a reference to the Document class. Is Document that has a reference to File class. So, I don´t think I can put the
<one-to-one name="document" class="Document" constrained="true" /> tag at the File hbm.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.