one-to-one and specifying column

有些话、适合烂在心里 提交于 2019-12-08 09:16:29

问题


I got the following mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
  <class name="Gate.Users.User, Gate.Models" table="users">
    <id name="Id" column="id">
      <generator class="sequence">
        <param name="sequence">users_id_seq</param>
      </generator>
    </id>
    <one-to-one class="Gate.Extensions.Extension, Gate.Models"  foreign-key="extension_id" name="Extension" />
  </class>
</hibernate-mapping>

Tables (pseudo):

table users
(
     id primary key
     extension_id int
)

table extensions
(
     id primary key
     address varchar(40) 
)

My problem is that nhibernate uses users.id instead of users.extension_id when fetching the extension. Is my mapping incorrect in any way?

Update

I changed to a many-to-one binding, and now it works. Guess I've must misunderstood how one-to-one is used?


回答1:


These tables are layed out like a many-to-one. (more than one user could have the same extension_id)

For a one-to-one, you would only need the id column in the user's table (of the two you posted) and it should have the same value as the id column in the extensions table, that way it is really enforced that you can only have one extension for each user and vice-versa.

If you really want to maintain separate keys, you could use the name attribute to specify the extensionId property of your User object and the property-ref attribute to specify the id of the Extension object. Here's a blog post on it.



来源:https://stackoverflow.com/questions/4776194/one-to-one-and-specifying-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!