简体   繁体   English

如何使用外键生成器映射与NHibernate的一对一关联?

[英]How do I map a one-to-one association with NHibernate using foreign key generator?

So far the association works fine (the User class loads the appropriate UserRoles instance when present), but when creating a new User and setting its Roles property to a new instance of UserRoles , the UserRoles object is not saved. 到目前为止,关联工作正常( User类加载了适当的UserRoles实例(如果存在)),但是在创建新User并将其Roles属性设置为UserRoles的新实例时,不会保存UserRoles对象。

Here is my abridged User.hbm.xml: 这是我删节的User.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="DistrictObservations.User, DistrictObservations" table="users">
    <cache usage="read-write" region="all" />

    <id name="ID" column="id" type="int" unsaved-value="0">
      <generator class="identity" />
    </id>

    <!-- snip -->

    <one-to-one name="Roles" class="DistrictObservations.UserRoles, DistrictObservations" lazy="false" />

  </class>
</hibernate-mapping>

And here is the UserRoles mapping: 这是UserRoles映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="DistrictObservations.UserRoles, DistrictObservations" table="user_roles">
    <cache usage="read-write" region="all" />

    <id name="UserID" column="user_id" type="int" >
      <generator class="foreign">
        <param name="property">User</param>
      </generator>
    </id>

    <!-- snip -->

    <one-to-one name="User" class="DistrictObservations.User, DistrictObservations" lazy="false" constrained="true" foreign-key="FK_user_roles_users" />

  </class>
</hibernate-mapping>

Anyone got an idea how to have the UserRoles object saved with the User.ID as its primary key? 任何人都知道如何保存以User.ID作为其主键的UserRoles对象? I've been looking at the documentation, and to be honest, it is not particularly helpful. 我一直在看文档,老实说,它并不是特别有用。

Usually you don't need one-to-one, unless you're stuck with an immutable legacy database schema. 通常,您不需要一对一,除非您坚持使用不变的旧数据库架构。 Most User-Role mappings allow for users to be in multiple roles and roles to be allowed to be used by any number of users, which means an associative table for a many-to-many association. 大多数用户角色映射都允许用户担任多个角色,并且允许任何数量的用户使用这些角色,这意味着多对多关联的关联表。

If your app only needs one role per user ever, then I suggest putting the RoleId as a FK in the Users table directly. 如果您的应用每个用户仅需要一个角色,那么我建议将RoleId作为FK直接放在Users表中。

That being said, if you stick to a one-to-one for whatever reason, make sure you use inverse="true" on the one that will not be doing the updating (the 'child' entity). 话虽如此,如果您出于某种原因坚持一对一,请确保对不会进行更新的对象(“子”实体)使用inverse =“ true”。 (See the Hibernate one-to-one docs for more info on the inverse attribute--it should explain enough about it.) (有关反向属性的更多信息,请参见Hibernate一对一文档-它应该对此做足够的解释。)

I'm probably missing something, but do you need a one-to-one between User and UserRoles? 我可能会丢失一些东西,但是您是否需要User和UserRoles之间一对一的关系? Can you not just have a one-to-many between User and Role? 您不仅可以在用户和角色之间建立一对多关系吗? UserRoles seems redundant. UserRoles似乎是多余的。

I have found the one-to-one mapping to be rarely necessary. 我发现很少需要一对一映射。 What I would often think of as a one-to-one is usually a one-to-many mapping. 我经常想到的一对一映射通常是一对多映射。 The same value as the primary key on two tables is not something is a common occurrence. 与两个表上的主键相同的值并不常见。

Judging from the looks of the class names it seems that you are trying to associate roles to a user? 从类名的外观来看,您似乎正在尝试将角色与用户相关联? That does not look like a one-to-one to me. 在我看来,这似乎不是一对一的。

That said, it looks like the generator of class foreign should do what you need. 就是说,外国类的生成器似乎应该执行您需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NHibernate / Fluent的一对一映射问题:外键未更新 - One-to-one Mapping issue with NHibernate/Fluent: Foreign Key not updateing 使用流畅的API在旧数据库中以一对一的关系设置复合外键 - Using fluent API to set up composite foreign key in one-to-one relation in legacy database 导航属性名称与属性类型不同时,NHibernate多对一/一对多外键如何处理? - How to do a NHibernate many-to-one/one-to-many foreign key in case of navigation property name different from property type? Linq中的一对一关联和过滤 - One-to-one association and filtering in Linq 是一对一还是 Component ? 休眠映射 - Is it one-to-one or Component ? Nhibernate Mapping 如何使用nHibernate映射n列主键 - How do I map a n-column primary key with nHibernate 如何在实体框架中建立真正的一对一关系? - How to do a true one-to-one relationship in Entity Framework? 如何在NHibernate中映射两个具有外键关系的表? - How to map two tables with a foreign key relationship in NHibernate? NHibernate中的双向一对多关联 - Two way one-to-many association in NHibernate 如何初始化与另一个实体具有一对一关系的新对象,然后将该新对象添加到数据库中? - How do I initialise a new object that has a one-to-one relationship to another entity and then add that new object to the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM