关于 The Collection 中 Fk 参考的问题
我有 2 个实体:(人)& (地址)
具有以下映射:
<class name="Adress" table="Adress" lazy="false">
<id name="Id" column="Id">
<generator class="native" />
</id>
<many-to-one name="Person" class="Person">
<column name="PersonId" />
</many-to-one>
</class>
<class name="Person" table="Person" lazy="false">
<id name="PersonId" column="PersonId">
<generator class="native" />
</id>
<property name="Name" column="Name" type="String" not-null="true" />
<set name="Adresses" lazy="true" inverse="true" cascade="save-update">
<key>
<column name="PersonId" />
</key>
<one-to-many class="Adress" />
</set>
</class>
我的问题是,当我使用新对象设置 Adrees.Person 时 person,集合 person.Adresses 不会自行更新。
我应该更新协会的每个最终角色吗? 两个都有?
另一件事:如果我像这样手动更新 Fk : Adress.PersonId 它不会破坏或更改关联。这是吗 Nhibernte行为?
预先感谢,我正在等待您的经验
i have 2 entities : ( person ) & (Address)
with follwing mapping :
<class name="Adress" table="Adress" lazy="false">
<id name="Id" column="Id">
<generator class="native" />
</id>
<many-to-one name="Person" class="Person">
<column name="PersonId" />
</many-to-one>
</class>
<class name="Person" table="Person" lazy="false">
<id name="PersonId" column="PersonId">
<generator class="native" />
</id>
<property name="Name" column="Name" type="String" not-null="true" />
<set name="Adresses" lazy="true" inverse="true" cascade="save-update">
<key>
<column name="PersonId" />
</key>
<one-to-many class="Adress" />
</set>
</class>
my propblem is that when i set Adrees.Person with new object of
person ,The collection person.Adresses doesn't update itself .
should i update every end role of the association to be updated in the
two both?
another thing : if i updated the Fk manually like this :
Adress.PersonId it doesn't break or change association. does this is
Nhibernte behavior ?
thanks in advance , i am waiting for your experiencies
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
person.Addresses 不会自行更新。您应该这样做:
或者,因为在您的示例中 Person.Addresses 被标记为 inverse="true",所以您只能执行
然后刷新会话并从数据库中获取同一个人。其地址集合应使用新地址进行更新。
本文(http://simoes.org/docs/hibernate-2.1/155.html )很好地描述了“逆”属性。这篇文章是针对 Hibernate,而不是 NHibernate,但我相信这里并不重要。
The person.Addresses will not be updated by itself. You should do it like this:
Alternatively, because Person.Addresses is marked with inverse="true" in your example, you could execute only
then flush the session and obtain the same person from the database. Its Addresses collection should be updated with new address.
This article (http://simoes.org/docs/hibernate-2.1/155.html) describes 'inverse' attribute very nicely. The article is for Hibernate, not NHibernate, but I believe it doesn't matter here.