-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-many with mapping table having additional columns
PostPosted: Wed Mar 05, 2008 12:04 am 
Newbie

Joined: Mon Nov 19, 2007 1:06 pm
Posts: 1
I found a similar problem on the Hibernate Users forum, but since I am using NHibernate I wanted to make sure I posted this question in the correct forum. The other thread is located here: http://forum.hibernate.org/viewtopic.php?t=984325&highlight=&sid=6269ef6851b40580197351e5ae1f6696

I have a similar problem, I was hoping somebody could help me with.

I have a table schema like so:

Code:
CREATE TABLE [dbo].[Entity](
   [ID] [uniqueidentifier] NOT NULL,
   [FKEY_ID] [uniqueidentifier] NOT NULL,
   [Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Entity] PRIMARY KEY CLUSTERED
(
   [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[JoinTable](
   [ID] [uniqueidentifier] NOT NULL,
   [FKEY_ID1] [uniqueidentifier] NOT NULL,
   [FKEY_ID2] [uniqueidentifier] NOT NULL,
   [Value] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Join] PRIMARY KEY CLUSTERED
(
   [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


The schema is supposed to represent a many-to-many relationship between Entities. The JoinTable contains the relationship between Entities. Where FKEY_ID1 is the FKEY_ID (not the primary key) of the Parent Entity while FKEY_ID2 is the FKEY_ID (not the primary key) of the Child Entity.

I have the following domain objects:
Code:
    public class Parent
    {
        public Guid Id;
        public Guid FKeyId;
        public string Name;
        private IList<Child> _children;

        public IList<Child> Children
        {
            get { return _children; }
            set { _children = value; }
        }
    }

    public class Child
    {
        public Guid Id;
        public Guid FKeyId;
        public string Name;
    }

    public class ParentChild
    {
        public Guid Id;
        public Guid FKEY_ID1;
        public Guid FKEY_ID2;
        public string Value;

        public Parent Parent;
        public Child Child;
    }


With the following mapping files:
Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   default-lazy="false"
                   assembly="NHibernateManyToMany"
                   namespace="NHibernateManyToMany"
                   default-access="field">
  <class name="Parent" table="Entity">
    <id name="Id" type="Guid" column="ID">
      <generator class="guid"/>
    </id>

    <property name="FKeyId" column="FKEY_ID"/>
    <property name="Name" column="Name"/>

    <bag name="Children" access="property">
      <key column="FKEY_ID1"/>
      <one-to-many class="ParentChild"/>
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   default-lazy="false"
                   assembly="NHibernateManyToMany"
                   namespace="NHibernateManyToMany"
                   default-access="field">
  <class name="Child" table="Entity">
    <id name="Id" type="Guid" column="ID">
      <generator class="guid"/>
    </id>

    <property name="FKeyId" column="FKEY_ID"/>
    <property name="Name" column="Name"/>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   default-lazy="false"
                   assembly="NHibernateManyToMany"
                   namespace="NHibernateManyToMany"
                   default-access="field">
  <class name="ParentChild" table="JoinTable">
    <id name="Id" type="Guid" column="ID">
      <generator class="guid"/>
    </id>

    <property name="FKEY_ID1" column="FKEY_ID1"/>
    <property name="FKEY_ID2" column="FKEY_ID2"/>
    <property name="Value" column="Value"/>

    <many-to-one name="Parent" class="Parent" foreign-key="FKEY_ID">
      <column name="FKEY_ID1" />
    </many-to-one>
    <many-to-one name="Child" class="Child" foreign-key="FKEY_ID">
      <column name="FKEY_ID2" />
    </many-to-one>
  </class>
</hibernate-mapping>


A call to the following code:

Code:
Parent parent = session.Get<Parent>(new Guid("72EA439F-65DD-41EE-BBFA-82C211656EFF"));


Yields:
Code:
SELECT   children0_.FKEY_ID1 as FKEY2___2_, children0_.ID as ID2_, hildren0_.ID as ID1_1_, children0_.FKEY_ID1 as FKEY2_1_1_, children0_.FKEY_ID2 as FKEY3_1_1_, children0_.Value as Value1_1_, child1_.ID as ID0_0_, child1_.FKEY_ID as FKEY2_0_0_, child1_.Name as Name0_0_
FROM JoinTable children0_ left outer join Entity child1_ on children0_.FKEY_ID2=child1_.ID/* Problem 1 */
WHERE children0_.FKEY_ID1='72ea439f-65dd-41ee-bbfa-82c211656eff'/* Problem 2 */


There are two problems with this SQL. Thus I am not creating the mapping properly.

1. The ID of the child1_ table should be FKEY_ID not ID. (i.e. =child1_.FKEY_ID) I would have thought that the foreign-key attribute within the ParentChild mapping file would have corrected this.
2. This ID in the where clause is the Primary key of the Parent. I want it to be the FKEY_ID of the Parent.

I would appreciate any help in this matter. The database we are currently trying to map to is very similar to the schema I am demonstrating here, however this example has been simplified.

Thanks in Advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.