[英]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.