-->
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: Foreign key field in mapping file
PostPosted: Wed Nov 07, 2007 11:52 am 
Newbie

Joined: Wed Nov 07, 2007 11:38 am
Posts: 5
Hi,

I am trying to find solution and approach for a common task in my projects.
I have a class and I want to use a mapping file to fill the properties of this class.
Also, I have a table which stores the data for this class. And I have a foreign key in this table that points to onother table.

How can i use hibernate-mapping file to map all the fields from my table and one field from the table where the foreign key points?

Thank you in advance,
Best, Daniel


Top
 Profile  
 
 Post subject: Re: Foreign key field in mapping file
PostPosted: Wed Nov 07, 2007 12:09 pm 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
DanielSap wrote:
Hi,

I am trying to find solution and approach for a common task in my projects.
I have a class and I want to use a mapping file to fill the properties of this class.
Also, I have a table which stores the data for this class. And I have a foreign key in this table that points to onother table.

How can i use hibernate-mapping file to map all the fields from my table and one field from the table where the foreign key points?

Thank you in advance,
Best, Daniel


Do you mean have a method of automatically populate the Java class based on an already written hibernate mapping file? If this is your question, you can use Hibernate tools, that integrates with Eclipse.

As far as I understand your second question, read the documentation:

http://www.hibernate.org/hib_docs/reference/en/html/example-mappings.html
http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html#tutorial-associations
http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html#tutorial-associations


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 4:01 am 
Newbie

Joined: Wed Nov 07, 2007 11:38 am
Posts: 5
I can give a concrete situation that illustrate the problem.
It's not about eclipse tools, it's more likely about writing a hibernate mapping file!

I have a table PAYMENT and another table PAYMENT_STATUS
In table payment I have a foreign key PAYMENT_STATUS_ID.

I have only one class Payment. And I use HQL and hibernate mapping file
to map all the fields from the PAYMENT table to the Payment class.
...("from Payment");

It maps all the table fields to the class Person
And it works fine, but!

But I can't show the user the PAYMENT_STATUS_ID field. This is an Integer field ant does not make any sanse to the user. But I have only this property in Payment. I want to show the user the name of the Status.
This status is in the second table - PAYMENT_STATUS.NAME.

I tought that the simplest solution will be to write one more property in the hibernate mapping file (Payment.hbm.xml). And in this property tag to say that this is a column from another table, and the foreign key is ...!
And it should look something like that
<property name="statusText" table="PAYMENT_STATUS" key="STATUS_ID" column="STATUS_TEXT" />

and this will cause a join between these 2 tables on the field STATUS_ID
and return of the STATUS_TEXT column

I don't want to have extra class for my second table.
Also I don't want to use formula in my property tag

Tanks for the reply, wild_oscar!

Best, Daniel


Top
 Profile  
 
 Post subject: Foreign key field in mapping file
PostPosted: Thu Nov 08, 2007 4:02 am 
Newbie

Joined: Wed Nov 07, 2007 11:38 am
Posts: 5
error


Last edited by DanielSap on Thu Nov 08, 2007 4:07 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Foreign key field in mapping file
PostPosted: Thu Nov 08, 2007 4:02 am 
Newbie

Joined: Wed Nov 07, 2007 11:38 am
Posts: 5
error


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 7:44 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
DanielSap wrote:
I can give a concrete situation that illustrate the problem.
It's not about eclipse tools, it's more likely about writing a hibernate mapping file!

I have a table PAYMENT and another table PAYMENT_STATUS
In table payment I have a foreign key PAYMENT_STATUS_ID.

I have only one class Payment. And I use HQL and hibernate mapping file
to map all the fields from the PAYMENT table to the Payment class.
...("from Payment");

It maps all the table fields to the class Person
And it works fine, but!

But I can't show the user the PAYMENT_STATUS_ID field. This is an Integer field ant does not make any sanse to the user. But I have only this property in Payment. I want to show the user the name of the Status.
This status is in the second table - PAYMENT_STATUS.NAME.

I tought that the simplest solution will be to write one more property in the hibernate mapping file (Payment.hbm.xml). And in this property tag to say that this is a column from another table, and the foreign key is ...!
And it should look something like that
<property name="statusText" table="PAYMENT_STATUS" key="STATUS_ID" column="STATUS_TEXT" />

and this will cause a join between these 2 tables on the field STATUS_ID
and return of the STATUS_TEXT column

I don't want to have extra class for my second table.
Also I don't want to use formula in my property tag

Tanks for the reply, wild_oscar!

Best, Daniel


I believe this is what you're looking for:
http://www.thearcmind.com/confluence/display/RandomThoughts/Hibernate+need+to+access+properties+on+join+table

I don't think it's recommended, though, as I suspect you'll get into problems if you update the values from your STATUS_TEXT column...

Tell us if it works.

PS: If you feel my answer was useful, please rate the answer. Thanks![/url]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 8:04 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
Basically, what you're looking for is a subselect on your Payment class, where you'll have to select all the Payment table properties and the status_text from your inner joined class:




Code:
          <subselect>
         select
            ta.id, ta.property1, ta.tableb_id, tb.property_a as propertya
         from    tablea ta inner join tableb tb on ta.tableb_id=tb.id     
   
        </subselect>


Top
 Profile  
 
 Post subject: Foreign key field in mapping file
PostPosted: Thu Nov 08, 2007 11:47 am 
Newbie

Joined: Wed Nov 07, 2007 11:38 am
Posts: 5
Dear, wild_oscar!

The solution you gave me exactly matches my problem.
Thank you very much!

I don't need to update the field from the other table so it works perfect to me.

Here is the code in the mapping file:

<hibernate-mapping schema="dbo" package="data">
<class name="Payment" table="PAYMENT" schema="dbo"
optimistic-lock="none">

<subselect>
select
p.*,
s.NAME as STATUS_TEXT
from PAYMENT p
inner join
PAYMENT_STATUS s on s.STATUS_ID=p.STATUS_ID
</subselect>

<id name="paymentId" type="integer" unsaved-value="null">
<column name="PAYMENT_ID" not-null="true" unique="true"
index="PK_PAYMENT" />
<generator class="sequence">
<param name="sequence">SEQ_PAYMENT</param>
</generator>

</id>
<property name="statusId" type="long" column="STATUS_ID" />
<property name="statusText" column="STATUS_TEXT" insert="false" update="false"/>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 5:27 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
Could you please rate my answers so I can earn credits in the forum? Thank you.


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.