开发者

NHibernate joining on incorrect field

开发者 https://www.devze.com 2023-03-03 22:08 出处:网络
I am very new to Nhibernate, I know what my issue is however unsure how to fix it. Issue: The user is joined to a employee via the EmployeeID rather than the UserID. Which is causing issues as it is

I am very new to Nhibernate, I know what my issue is however unsure how to fix it.

Issue: The user is joined to a employee via the EmployeeID rather than the UserID. Which is causing issues as it is bringing back incorrect details.

Employee.hbm.xml
<many-to-one name="User" unique="true" column="UserID" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="EmployeeID" class="Employee" lazy="false" />

Employee.cs
public virtual int EmployeeID { get; set; }
public virtual User User { get; set; } - Th开发者_JAVA百科is is UserID within the actual database
public virtual string EmailAddress { get; set; }

User.cs
public virtual int UserID { get; set; }
public virtual string Username { get; set; }
public virtual string Title { get; set; }
public virtual string Forename { get; set; }
public virtual string Surname { get; set; }
public virtual Employee Employee { get; set; }

Above is what someone else has coded and I'm trying to fix it. I tried changing

<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />

however this still causes issues.

Am I missing something? Thanks in advance for any help :-)

Clare

UPDATE

I was hoping by making it one-to-one that it would join on the UserID, however it is still joining on the EmployeeID. Anyone have any other ideas? Thanks again :-)

Employee.hbm.xml
<one-to-one name="User" foreign-key="UserID" class="User" lazy="false" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />


You shouldn't specify any column name in a one-to-one mapping, because that piece of information is part of the many-to-one mapping on the other side of the relationship.

In order to solve your problem you should remove the foreign-key attribute from the one-to-one mapping on the User entity:

<one-to-one name="Employee" class="Employee" lazy="false" />

NHibernate will use the information in the many-to-one mapping defined on the Employee entity to build the SQL statements.

Note that if an Employee always has a User associated to it (i.e. the Employee.User property can never be null) then you should state this fact in the one-to-onemapping by adding the constrained attribute:

<one-to-one name="Employee" class="Employee" constrained="true" lazy="false" />

Related resources:

  • NHibernate Mapping - <one-to-one />


According to your entities there is not a one to one relationship between user and employee. Please see this explanation:

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

I think you need to specifiy your one to one as a many-to-one instead.

Edit:
I think I may have misunderstood your scenario.
You may want to use property-ref here. So you're mapping would look like this:

<one-to-one name="Employee" class="Employee" property-ref="User" lazy="false" />

Please check out the following link for more detail: Nhibernate Documentation

Also there is a similar post on SO here: one-to-one and specifying column


I would use a many-to-one relationship here because I am aware you may want to merge User accounts and one-to-one may cause problems with moving an employee record to the merged user.

If you treat it like your User can have multiple Employee records then this avoids this problem.

Have you tried something like the following:

Employee.hbm.xml

User.hbm.xml

http://ayende.com/blog/3938/nhibernate-mapping-many-to-one

0

精彩评论

暂无评论...
验证码 换一张
取 消