-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with a join table that contain more than 2 fields
PostPosted: Tue Sep 13, 2005 10:28 am 
Newbie

Joined: Thu Sep 08, 2005 12:15 pm
Posts: 17
I have a class media that contains a bag of address so in my database the table Media is join to the table
Address by a join table. I have no problem mapping this but the problem is that in my join table I have 3 fields IdMedia, IdAdresse and Default. The Default field tell me if the address is the media's default address.

So my question is, How can I have the default property add to my Adresse class. Note that because of other tables related to Address I CANT put the default field in the address table.

So when the bag of adress for a particular media is retreive I would like to have something like the result of this sql query

Code:
SELECT     Media.*, JN_Media_Adresse.Defaut AS [Default], Adresse.*
FROM         JN_Media_Adresse INNER JOIN
                      Adresse ON JN_Media_Adresse.IdAdresse = Adresse.IdAdresse INNER JOIN
                      Media ON JN_Media_Adresse.IdMedia = Media.IdMedia


I tried this mapping but it doesnt works

Code:
<bag name="Adresses" table="JN_Media_Adresse" lazy="true">
         <jcs-cache usage="read-write"/>
         <key column="IdMedia"/>
         <many-to-many class="Datablitz.Engine.objets.Adresse, Datablitz.Engine" column="IdAdresse"/>
         <property name="Default" column="Defaut" type="Boolean"/>
      </bag>


Thanks
Simon


Top
 Profile  
 
 Post subject: Bump
PostPosted: Wed Sep 14, 2005 5:22 pm 
Newbie

Joined: Wed Sep 14, 2005 5:07 pm
Posts: 6
I'm stuck on this problem as well.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 7:45 pm 
Regular
Regular

Joined: Tue May 24, 2005 12:55 pm
Posts: 56
First you need to know that NHibernate cannot join tables into a single mapped class. If it can, would someone please post an example.

Second, in NHibernate you need to look at xref tables with more than 2 fields as being both xref and a class in itself. The 2 xref classes can have bags but will not have any of the data in the xref table.

The way I handle cross-reference tables to do more than many-to-many is to make it a class of it's own. I have not found any other way to do this. This is because the table is no longer just a many-to-many relationship, but now holds data about the relationship, just like any other table. It ends up being less elegant looking but it works.

So for your senario I would do the following.

map the MediaAddress table
Code:
class
  prop mediaId
  prop addrId
  prop default
  many-to-one medias lazy
  many-to-one addrs lazy


Now instead of working from the Media class; work from the MediaAddress class.
Code:
//HQL
FROM MediaAddress AS ma WHERE ma.mediaId = :mId


Like I said it isn't elegant, but it works.


Top
 Profile  
 
 Post subject: Need more details...
PostPosted: Thu Sep 15, 2005 11:37 am 
Newbie

Joined: Thu Sep 15, 2005 10:27 am
Posts: 1
I appologize if this is being posted multiple times....

Can you please post mapping files and code files of your example?
I would really appreciate it.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 3:51 pm 
Newbie

Joined: Thu Sep 08, 2005 12:15 pm
Posts: 17
thanks for your reply.

I think i found another solution. I created a view on the address table that contain all the address fileds plus the default field of my join table. I map my Address call on that view so it contain the default field.

the downside of this is that I have a join in the select clause of my view.


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