我有很多很多的关系。
表A、表B和表AB。表AB将包括2列,A_Id和B_id。主键需要来自这两列。
许多B记录可以指一个A记录。但是对于B中的每一个记录,只有一个A记录是mutch
HBM和POCO类中正确的语法是什么?
提前感谢
发布于 2014-04-29 12:42:26
这是一个基于文档中非常清楚的示例的示例:23.2.作者/作品
<class name="A" table="A" >
<id name="Id" column="A_Id" generator="native" />
<bag name="Bs" table="AB" lazy="true">
<key column="A_Id">
<many-to-many class="B" column="B_Id" not-null="true" />
</bag>
...
</class>
<class name="B" table="B" >
<id name="Id" column="B_Id" generator="native" />
<bag name="As" table="AB" lazy="true" inverse="true">
<key column="B_Id">
<many-to-many class="A" column="A_Id" not-null="true" />
</bag>
...
</class>这将是C#中的类
public class A
{
public virtual int Id { get; set; }
public virtual IList<B> Bs { get; set; }
}
public class B
{
public virtual int Id { get; set; }
public virtual IList<A> As { get; set; }
}表AB在这里隐式映射..。没有显式配对对象AB
但我自己更喜欢的方法是使用AB_ID代理键扩展表AB,并将其映射为标准对象。如果您喜欢阅读关于显式配对对象作为映射实体的更多信息:
与评论相关的更新,B只能有一个A
在这种情况下,我们不需要AB表。B应该有列A_Id,表示rela引用:
<class name="A" table="A" >
<id name="Id" column="A_Id" generator="native" />
<bag name="Bs" table="B" lazy="true">
<key column="A_Id">
<!-- not MANY but ONE-TO-Many -->
<one-to-many class="B" />
</bag>
...
</class>
<class name="B" table="B" >
<id name="Id" column="B_Id" generator="native" />
<many-to-one name="A" column="A_Id" />
...
</class>实体
// This class is the same
public class A
{
public virtual int Id { get; set; }
public virtual IList<B> Bs { get; set; }
}
// here just a reference
public class B
{
public virtual int Id { get; set; }
public virtual A A { get; set; }
}问题是,要么是many-to-many,要么是AB表已经就位--或者没有。我之间什么都不会说
https://stackoverflow.com/questions/23364607
复制相似问题