NHibernate一对多外键为NULL
我的 NHibernate 有问题。我想要做的很简单: 我有两个班级。用户票证和用户数据。 UsertTicket 有一些 UserData,而 UserData 属于一个 UserTicket:
public class UserData{
public virtual int Id { get; set; }
public virtual String PDF_Path { get; set; }
}
public class UserTicket
{
public virtual int Ticketnr { get; set; }
public virtual IList<UserData> UserData { get; set; }
}
这里是 mappig xml:
<class name="UserTicket" table="UserTicket">
<id name="Ticketnr">
<generator class="identity"/>
</id>
<bag name="UserData" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="FK_Ticketnr" not-null="false"/>
<one-to-many class="UserData" />
</bag>
</class>
<class name="UserData" table="UserData">
<id name="Id">
<generator class="identity"/>
</id>
<property name="PDF_Path" />
</class>
当我运行它时,它可以工作,但是 UserData 的 DB 表中的 FK_Ticketnr 列始终 = NULL。 有人告诉我,我必须建立从我的孩子(用户数据)到父类的反向链接。但我不知道该怎么做。我如何让Hibernate将UserTicket的主键(Ticketnr)的值写入FK_Ticketnr?
提前致谢
I have a problem with NHibernate. What i am trying to to is very simple:
I have two Classes. UserTicket and UserData. A UsertTicket has some UserData and a UserData belongs to one UserTicket:
public class UserData{
public virtual int Id { get; set; }
public virtual String PDF_Path { get; set; }
}
public class UserTicket
{
public virtual int Ticketnr { get; set; }
public virtual IList<UserData> UserData { get; set; }
}
And here the mappig xml:
<class name="UserTicket" table="UserTicket">
<id name="Ticketnr">
<generator class="identity"/>
</id>
<bag name="UserData" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="FK_Ticketnr" not-null="false"/>
<one-to-many class="UserData" />
</bag>
</class>
<class name="UserData" table="UserData">
<id name="Id">
<generator class="identity"/>
</id>
<property name="PDF_Path" />
</class>
When i run it, it works, but the column FK_Ticketnr in the DB-Table of UserData is always = NULL.
Someone told me i have to make a back link from my Child (UserData) to the Parent-Class. But i can not figure out how to do so. How do i get Hibernate to write the value of the Primary-Key (Ticketnr) of UserTicket into FK_Ticketnr?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除包上的 inverse="true"。这告诉 NHibernate 该关系是通过 UserData 映射进行管理的。由于您没有反向引用,因此它永远不会被持久化。另一种选择是在 UserData 上放置父引用,但如果您不需要它,我不会推荐它。
Remove inverse="true" on your bag. That is telling NHibernate that the relationship is managed from the UserData mapping. Since you don't have a back reference, it is never persisted. The other option is to put a parent reference on UserData, but I wouldn't recommend it if you don't need it.
另一种选择如下...
像这样修改您的映射文件...
将
inverse="true"
放在包上。如果您采用这种方法,则在将 UserData 对象添加到 UserData 包时,需要在 UserData 对象上设置 Ticket。换句话说,你需要维持双方的关系。您可以手动执行此操作,也可以尝试使用方法或构造函数将其自动化一些。
The alternative is the following...
Modify your mapping file like this...
Keep the
inverse="true"
on the bag.If you go with this approach, you will need to set Ticket on your UserData objects when you add them to the UserData bag. In other words, you need to maintain both sides of the relationship. You could do this manually or you could try to automate it a little with with methods or constructors.