-->
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.  [ 3 posts ] 
Author Message
 Post subject: one-to-zero/one mapping
PostPosted: Fri Feb 03, 2006 10:42 pm 
Newbie

Joined: Thu Jan 05, 2006 1:56 pm
Posts: 7
BHibernate version: 1.0.1

Mapping documents:
I have this 3 table, one for Transaction, one for Note, and one is the join table between those two. One transaction may have zero-one note.

Here's the table structure (pseudo-code):
Code:
CREATE TABLE TR_TRANSACTION
(
      transaction_id PK IDENTITY,
      .... // other columns,
)

CREATE TABLE TBL_NOTE
(
     note_id PK IDENTITY,
     note_content TEXT
)

CREATE TABLE TR_TRANSACTION_NOTE
(
     transaction_id PK,
     note_id
)


I have this class Transaction, and I want to have property Note, to access whether this Transaction has note (it can be some value, or it can be null), by accessing TBL_NOTE.
I was reading Hibernate documentation, and there were <join> which I think can used to solve this problem, however I don't think <join> is supported in NHibernate 1.0.1. Is there any work around?

NB: Note and Transaction class are both mapped separately already (not connected the way I wanted yet).

Thank You.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 9:16 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:09 pm
Posts: 23
Unless you are doing a one-to-many you shouldn't need a join table.

I would do it like this:

Code:
CREATE TABLE TR_TRANSACTION
(
      transaction_id PK IDENTITY
)

CREATE TABLE TBL_NOTE
(
     note_id PK IDENTITY,
     transaction_id,
     note_content TEXT
)

ALTER TABLE TBL_NOTE
Add Constraint FK_Note_Transaction FOREIGN KEY
(transaction_id) references TR_TRANSACTION (transaction_id)



<class name="Example.Transaction, Example" table="TR_TRANSACTION">
    <id name="ID" column="transaction_id" type="Int64" unsaved-value="0">
        <generator class="identity" />
    </id>
      
    <one-to-one name="Note" class="Example.Note, Example" property-ref="Transaction"/>
             
</class>

<class name="Example.Note, Example" table="TBL_NOTE">
      <id name="ID" column="note_id" type="Int64" unsaved-value="0">
         <generator class="identity" />
      </id>
      
      <many-to-one name="Transaction" class="Example.Transaction, Example" column="transaction_id" not-null="true" />
      
</class>


That will take care of the one-to-one and one-to-zero situations. Note, I'm assuming TBL_NOTE must have a TR_TRANSACTION.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 10:07 pm 
Newbie

Joined: Thu Jan 05, 2006 1:56 pm
Posts: 7
No Tristian, you got it the other way around. It should be Transaction has Note.
Well I'm just curious how to solve my problem without changing the database.

I've solved this problem by changing the database into:

TR_TRANSACTION
{
transaction_id PK,
note_id FK
}

TBL_NOTE
{
note_id PK,
note
}


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