我有一个有以下描述的课程:
public class Customer {
public ISet<Client> Contacts { get; protected set;}
}
Run Code Online (Sandbox Code Playgroud)
我想将Contacts属性映射到下表:
CREATE TABLE user_contacts (
user1 uuid NOT NULL,
user2 uuid NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
我希望它双向映射,即当Customer1添加到Customer2的Contacts时,Customer1的Contacts集合应包含Customer2(可能仅在实体重新加载后).我怎么能这样做?
更新当然我可以映射从左到右和从右到左的集合,然后在运行时进行组合,但它会......嗯......不讨厌......还有其他解决方案吗?无论如何,非常感谢你,FryHard!
看一下这个链接,了解 hibernate 所谓的单向多对多关联。在Castle ActiveRecord中,我使用 HasAndBelongsToMany 链接,但我不确定它在 nhibernate 中的映射方式到底如何。
尽管更深入地研究您的问题,但看起来您将从 customer 到 user_contacts 进行双向链接,这可能会破坏多对多链接。我将举一个例子,看看我能想出什么。
从 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>
Run Code Online (Sandbox Code Playgroud)
活动记录示例:
[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; }
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
2917 次 |
| 最近记录: |