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: map column from another table
PostPosted: Mon Dec 18, 2006 7:06 am 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
Hello.

Can I map only one colunm from anoter table?
For example, I have a table Order, every Order has Customer, I want include to Order only CustomerName, not whole Customer!
I know that I may write:
Code:
<many-to-one name="Customer" class="Customer" column="CustomerID"/>

But I needn't Customer, I want map only Customer.Name. How do it?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 7:04 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You can't map fields from multiple tables into a single class, however you can access the information using an HQL query.

You can do this two ways:

1. You can provide a join in your HQL and include the additional field you want in your SELECT statement

Code:
SELECT o, c.Name FROM Order o LEFT JOIN o.Customer c


In this case the you map the Customer's primary key as a foreign key in the Orders table. The result will be a list of 2 dimensioned arrays where the first dimension is the Order and the second dimension is a string containing the Customer's name.

2. You can use an new class to wrap the information together using the HQL NEW keyword. You can find information about it in the documentation, but in essence you do the same thing as in step one but you indicate to NHibernate that each result should be passed to the constructor of a new object which exposes all of the data you want to expose.

Hope that helps,

Symon.


Top
 Profile  
 
 Post subject: thanks
PostPosted: Wed Dec 20, 2006 4:24 am 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
Thanks a lot! I'll try to do this.


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.