从<map> </map>中删除

时间:2009-08-07 12:40:04

标签: nhibernate map

我有映射:

<class name="User" table="Users" lazy="false">
  <id name="id" type="Int32" column="id">
    <generator class="identity" />
  </id>
  <property name="name" column="name" type="String"/>
  <map name="Urls" table="UserUrl" lazy="true" inverse="true" cascade="all">
     <key column="user_id"></key>
     <index column="url_type_id" type="Int32"/>
     <one-to-many class="UserUrl"/>
   </map>
</class>

<class name="UserUrl" table="UserUrl" lazy="false">
  <id name="id" type="Int32" column="id">
    <generator class="identity"/>
  </id>
  <property name="user_id" column="user_id" type="Int32" not-null="true"/>
  <property name="UrlType" column="url_type_id" type="Int32" not-null="true"/>
  <property name="Url" column="url" type="String" not-null="true"/>
</class>

我也得到

class User
{
  IDictionary<int,UserUrl> Urls;
 ....
}

User currentUser = FindById(2);
currentUser.Urls.Remove(5);

所以我从Url的assosiation集合中删除了一个项目。然后我拨打SaveOrUpdateCopy(...),但表UserUrl中的网址没有删除,也没有错误。

有人知道如何从集合和DB中删除子项吗?

4 个答案:

答案 0 :(得分:1)

为&lt; map&gt;设置inverse为false元件。

答案 1 :(得分:0)

试试这个:

<map name="Urls" lazy="true" cascade="all-delete-orphans">
  • 您不需要table,因为该表是在类UserUrl映射中定义的
  • 如果不是
  • ,你不应该反过来
  • 您应该将其全部级联 - 删除 - 孤立,以告知NH删除从集合中删除的项目。

与您的问题无关,为什么在网址中有这个?

<property name="user_id" column="user_id" type="Int32" not-null="true"/>

你正在那里映射外键!我永远不敢这样做。


实际上,我不确定你是否应该像这样映射它:

<map name="Urls" table="UserUrl" lazy="true" cascade="all-delete-orphans">
   <key column="user_id"/>
   <index column="url_type_id" type="Int32"/>
   <composite-element>
     <property name="UrlType" column="url_type_id" type="Int32" not-null="true"/>
     <property name="Url" column="url" type="String" not-null="true"/>
   </composite-element>
 </map>

如果Url不是具有自己的Id的独立实体,那么您可以将其映射为composite-element。该URL被视为值类型。没有类UserUrl映射了。

修改:

  • 请参阅NH Reference Chapter 7,它解释了组件。
  • 如果在引用上遇到非null问题,只需删除not-null 对外键的约束。 NH需要插入一些列来获取 主键如果使用generator class="identity",则存储 零临时。

答案 2 :(得分:0)

我尝试在谷歌搜索,但没有关于<map><one-to-many ><map><composite-element>之间差异的信息,因此我使用<map><one-to-many >
  <map name="Urls" table="UserUrl" lazy="true" cascade="all-delete-orphans"> <key column="user_id"/> <index column="url_type_id" type="Int32"/> <composite-element> <property name="UrlType" column="url_type_id" type="Int32" not-null="true"/> <property name="Url" column="url" type="String" not-null="true"/> </composite-element> </map>

<小时/> 如果Url不是一个独立的实体,拥有自己的Id,那么您可以将其映射为复合元素。 url被视为值类型。
不再有类UserUrl映射。 我在其他代码

中使用UserUrl类

答案 3 :(得分:0)

  

- 顺便说一句,如果您实际上正在评论,请不要回答您的问题   回答。你可能会被低估了

- 问题是我没有stackoverflow帐户,因此只有一种方法可以在我的帖子下输入名称和电子邮件。我尝试使用相同的名称和电子邮件在该网站上注册帐户,但是当我登录时,它将我视为新用户(而不是我登录时间)。
所以我别无选择。

我也发现工作代码是

<map name="Urls" lazy="true" cascade="all-delete-orphans" inverse="true">

没有inverse =“true”它不起作用。

相关问题