2008-10-09 14 views
5

मैं निम्नलिखित विवरण के साथ एक वर्ग है:NHibernate द्विदिश कई-से-अनेक संघ

public class Customer { 
    public ISet<Client> Contacts { get; protected set;} 
} 

मैं निम्न तालिका पर संपर्क संपत्ति मैप करना चाहते हैं:

CREATE TABLE user_contacts (
    user1 uuid NOT NULL, 
    user2 uuid NOT NULL 
) 

मैं इसे दोनों ओर मैप करना चाहते हैं, यानी जब ग्राहक 1 ग्राहक 2 के संपर्क में जोड़ा गया है, तो ग्राहक 1 के संपर्क संग्रह में ग्राहक 2 होना चाहिए (शायद इकाई रीलोड के बाद)। ऐसा कैसे किया जा सकता था?

अद्यतन निश्चित रूप से मैं बाएं से दाएं और दाएं से बाएं सेट मैप कर सकता हूं और फिर रनटाइम पर गठबंधन कर सकता हूं, लेकिन यह ... हम्म ... अशिष्ट ... क्या कोई अन्य समाधान है? किसी भी तरह, बहुत बहुत धन्यवाद, FryHard!

उत्तर

2

हाइबरनेट को यूनिडायरेक्शनल many-to-many associations पर कॉल करने के लिए इस लिंक पर एक नज़र डालें। Castle ActiveRecord में मैं HasAndBelongsToMany लिंक का उपयोग करता हूं, लेकिन मुझे यकीन नहीं है कि इसे निबर्ननेट में मैप किया गया है।

हालांकि आपके प्रश्न को थोड़ा गहराई से देखते हुए, ऐसा लगता है कि आप ग्राहक से उपयोगकर्ता से संपर्क करने के लिए द्विपक्षीय रूप से लिंक करेंगे, जो कई सारे लिंक तोड़ सकता है। मैं एक उदाहरण के साथ खेलूँगा और देखें कि मैं किसके साथ आ सकता हूं।

एक ActiveRecord से HBM फ़ाइलों के निर्यात से पता चलता इस

<?xml version="1.0" encoding="utf-16"?> 
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="NHibernateMapping.Customer, NHibernateMapping" table="Customer" schema="dbo"> 
    <id name="Id" access="property" column="Id" type="Int32" unsaved-value="0"> 
     <generator class="identity"> 
     </generator> 
    </id> 
    <property name="LastName" access="property" type="String"> 
     <column name="LastName" not-null="true"/> 
    </property> 
    <bag name="ChildContacts" access="property" table="user_contacts" lazy="false"> 
     <key column="user1" /> 
     <many-to-many class="NHibernateMapping.Customer, NHibernateMapping" column="user2"/> 
    </bag> 
    <bag name="ParentContacts" access="property" table="user_contacts" lazy="false" inverse="true"> 
     <key column="user2" /> 
     <many-to-many class="NHibernateMapping.Customer, NHibernateMapping" column="user1"/> 
    </bag> 
    </class> 
</hibernate-mapping> 

ActiveRecord उदाहरण:

[ActiveRecord("Customer", Schema = "dbo")] 
public class Customer 
{ 
    [PrimaryKey(PrimaryKeyType.Identity, "Id", ColumnType = "Int32")] 
    public virtual int Id { get; set; } 

    [Property("LastName", ColumnType = "String", NotNull = true)] 
    public virtual string LastName { get; set; } 

    [HasAndBelongsToMany(typeof(Customer), Table = "user_contacts", ColumnKey = "user1", ColumnRef = "user2")] 
    public IList<Customer> ChildContacts { get; set; } 

    [HasAndBelongsToMany(typeof(Customer), Table = "user_contacts", ColumnKey = "user2", ColumnRef = "user1", Inverse = true)] 
    public IList<Customer> ParentContacts { get; set; } 
} 

आशा है कि यह मदद करता है!