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: Composite Key one-to-many
PostPosted: Tue Dec 15, 2009 4:43 am 
Newbie

Joined: Mon Nov 02, 2009 9:52 am
Posts: 19
How do I have to map the following situation?

Address 1:N Contact

Address.Address(PK)
Contact.Address(PK)
Contact.Name(PK)

It should be a bidirectional mapping.

Code:
   class Address
   {
      public string Name { get; set; }
      public virtual ISet Contacts { get; set; }
      
      public Address()
      {}
   }


Code:
   class Contact
   {
      public Address Address { get; set; }
      public string Name { get; set; }

      public override bool Equals(object obj)
      {...}
      public override int GetHashCode()
      {...}
   }


Top
 Profile  
 
 Post subject: Re: Composite Key one-to-many
PostPosted: Thu Dec 17, 2009 9:12 am 
Newbie

Joined: Mon Nov 02, 2009 9:52 am
Posts: 19
I've got a working mapping on two tables with single PK and FK. But I cannot find a solution for a Composite Key of Address and Id in table contact. I read something about an extra class, e.g. "AddressContactKey". How would this work?

Code:
namespace NewNHibernate
{
   class Contact
   {
      public String Id { get; set; }
      public Address Address { get; set; }
      public string Name1 { get; set; }
      public string Name2 { get; set; }
      public string Name3 { get; set; }

      public Contact() { }
      public Contact(Address address)
      {
         Id = "NameOfContact";
         Address = address;
      }
   }
}


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" default-lazy="false">
  <class name="NewNHibernate.Contact, NewNHibernate">

    <id name="Id" type="string" >
      <generator class="assigned"/>
    </id>

    <property name="Name1" />
    <property name="Name2" />
    <property name="Name3" />

    <many-to-one name="Address" class="NewNHibernate.Address" />

  </class>
</hibernate-mapping>


Code:
namespace NewNHibernate
{
   class Address
   {
      public string Name { get; set; }
      public string Name2 { get; set; }
      private ISet contacts = new HashedSet();

      public ISet Contacts
      {
         get { return contacts; }
         set { contacts = value; }
      }
   }
}


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" default-lazy="false">
  <class name="NewNHibernate.Address, NewNHibernate">

    <id name="Name" type="string">
      <generator class="assigned"/>
    </id>
    <property name="Name2" />

    <set name="Contacts" inverse="true" cascade="all-delete-orphan">
      <key column="Address" />
      <one-to-many class="NewNHibernate.Contact, NewNHibernate" />
    </set>

  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Composite Key one-to-many
PostPosted: Thu Dec 17, 2009 10:52 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
what you want is a so called association class AddressContactKey, which would require a composite primary key.

Take a look at
http://stackoverflow.com/questions/4975 ... -causes-in
and
http://docs.jboss.org/hibernate/core/3. ... tions.html
and
http://mihail.stoynov.com/blog/CommentV ... ab290.aspx


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.