Nhibernate: join tables and get one column from another table

I have the following tables:

create table Users(
 Id uniqueidentifier primary key,
 InfoId uniqueidentifier not null unique,
 Password nvarchar(255) not null
)

Create table UserInfo(
 Id uniqueidentifier primary key,
 Company nvarchar(255) not null,
 ContactPerson nvarchar(255) not null
)

And InfoId- this is a foreign key referring to UserInfo(Id).

I want to match this with the following class:

public class UserCredentials
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string PasswordHash { get; set; }

    protected UserCredentials() { }
}

I want the following display:

Id  --> Users(Id)
UserName --> UserInfo(Company)
PasswordHash --> Users(Password)

I tried the following mapping:

<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
    <class name="UserCredentials" table="Users">
        <id name="Id" type="Guid">
            <generator class="guid.comb" />
        </id>

    <property name="PasswordHash" not-null="true" column="Password"/>
    <join table="UserInfo">
      <key column="Id" foreign-key="InfoId"/>
      <property name="UserName" column="Company" not-null="true" />
    </join>
  </class>
</hibernate-mapping>

But it seems that the element <key>is incorrectly specified (attribute foreign-key) does not do what I want. If I do not specify an attribute foreign-key, it will try to join the columns of Idboth tables, which is incorrect.

I do not want to include a property InfoIdin my class UserCredentialsif it can be avoided.

Can someone help me achieve the desired display?

+2
1

, , , , property-ref.

:                            

<property name="InfoId" not-null="true" column="InfoId"/>
<property name="PasswordHash" not-null="true" column="Password"/>

<join table="UserInfo">
  <key column="Id" property-ref="InfoId"/>
  <property name="UserName" column="Company" not-null="true" />
</join>

, InfoId UserCredentials, , , , (NH , )

, , , , nhibernate .

+4

Source: https://habr.com/ru/post/1747584/


All Articles