-->
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.  [ 2 posts ] 
Author Message
 Post subject: Numberous many-to-many relationships in one mapping table
PostPosted: Fri Sep 08, 2006 10:13 am 
Newbie

Joined: Thu Aug 24, 2006 2:49 pm
Posts: 3
I can't seem to find support for this.

I'm working with a system that would have over 20 many-to-many relationships, so I'm trying to consolidate a few...

For instance, I have a Person object. It has 5 many-to-many relationship sets to other entities and data types. So I would like to have one mapping table for all of the many-to-many relationships associated with the Person.

So my PersonDetail mapping table has columns like PersonId, ReferenceId, Category.

So for example, I have a PublishTo set that I want to map. And it's Category will be 'Publish'. How do I specify that? I can specify my key and many-to-many relationship no problem like:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="..." namespace="...">
  <class name="Person" table="People">
    <id name="Identifier" type="Int32" column="PersonId" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property name="FirstName" type="String" length="20"/>
    <property name="LastName" type="String" length="20"/>
    <set name="PublishTo" table="PeopleDetail" lazy="true">
      <key column="PersonId"/>
      <many-to-many class="Publish" column="ReferenceId"/>
    </set>
  </class>
</hibernate-mapping>

but I can't specify another column to populate. I tried using where="Category='Publish'" in my set definition, and that works for retreiving the data, but doesn't work when I try to insert or update. It just leaves the Category field null.

Anyone know a solution for this?


Top
 Profile  
 
 Post subject: A working solution
PostPosted: Fri Sep 08, 2006 4:19 pm 
Newbie

Joined: Thu Aug 24, 2006 2:49 pm
Posts: 3
Okay, so I have found a workaround solution.

I have created and intermediary class called PersonPublish. This uses Publish as a delegate pretty much. The PersonPublish class has a property for Category and a property for a Publish object. It also implements the Publish class. And overrides all of its methods by getting the value of the Publish property's properties. The class looks like this:
Code:
    public class PersonPublish : Publish
    {
        private String category = "Publish";
        private Publish publish = null;

        public PersonPublish()
        { }

        public PersonPublish(Publish pubDelegate)
        {
            publish = pubDelegate;
        }

        public String Category
        {
            get { return category; }
            set { category = "Publish"; }
        }

        public Publish Publish
        {
            get { return publish; }
            set { publish = value; }
        }

        public override int Identifier
        {
            get { return publish.Identifier; }
            set { publish.Identifier = value; }
        }

        public override String Name
        {
            get { return publish.Name; }
            set { publish.Name = value; }
        }
    }
}


And I updated my hbm.xml set to use a composite-element!

Code:
    <set name="PublishTo" table="PeopleDetail" lazy="true">
      <key column="PersonId"/>
      <composite-element class="PersonPublish">
        <property name="Category" type="String" length="20"/>
        <many-to-one name="Publish" class="Publish" column="ReferenceId"/>
      </composite-element>
    </set>


And it seems to work well enough because the PersonPublish class is hidden from the user and it can still cast to Publish which it does have access to. Just sucks to have the intermediary class... If anyone knows a better solution, I'm all ears!


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