4

NHibernateで双方向の1対1のマッピングを作成しようとしているときに、オブジェクトの参照を再帰的に取得できないことがわかりました。

例:との間に1対1の関係があるPersonとしAddressます。

次に、次のコードを実行した後、

class Person
{
    ... ...
    public Address Address { get;set; }
}

class Address
{
    ... ...
    public Person Person {get;set;}
}

Repository<Person> rep = new Repository<Person>();
Person p = rep.Get<Person>(1);

からの非null値が必要p.Address.Personです。つまり、IDが1の同じ人です。

しかし、プロパティは値を返していnullます。

問題を解決するために何を探す必要がありますか?

私のデータベーステーブルは次のようなものです。

Address {ID, Desc}
Person {ID, Name, AddressID}

Person.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
     default-access="property"
    >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO" 
         table="Person">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Name"/>

    <many-to-one
        name="Address"
        class="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO"
        column="AddressID" 
        cascade="all" 
        unique="true" />

  </class>
</hibernate-mapping>

Address.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
   default-access="property"
  >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO" 
         table="Address">
    <id name="ID" >
      <generator class="native" />
    </id>
    <property name="Desc"/>      
    <one-to-one
        name="Person"
        class="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO"
        />
  </class>
</hibernate-mapping>

私もエラーが発生しています:

could not load an entity: [NHibernate__BiDirectional__One_To_One.BO.Person#1][SQ
L: SELECT person0_.ID as ID0_1_, person0_.Name as Name0_1_, address1_.ID as ID1_
0_, address1_.Desc as Desc1_0_, address1_.AddressID as AddressID1_0_ FROM Person
 person0_ left outer join Address address1_ on person0_.ID=address1_.AddressID W
HERE person0_.ID=?]
Incorrect syntax near the keyword 'Desc'.
4

1 に答える 1

8

1 対 1 の関連付けには、次の 2 種類があります。

• 主キーの関連付け

• 一意の外部キーの関連付け

主キーの関連付けには、追加のテーブル列は必要ありません。2 つの行が関連付けによって関連付けられている場合、2 つのテーブル行は同じ主キー値を共有します。したがって、主キーの関連付けによって 2 つのオブジェクトを関連付ける場合は、同じ識別子の値が割り当てられていることを確認する必要があります。主キーの関連付けの場合、次のマッピングを Employee と Person にそれぞれ追加します。

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

ここで、PERSON テーブルと EMPLOYEE テーブルの関連する行の主キーが等しいことを確認する必要があります。

外部と呼ばれる特別な NHibernate 識別子生成戦略を使用します。

<class name="Person" table="PERSON">
<id name="Id" column="PERSON_ID">
<generator class="foreign">
<param name="property">Employee</param>
</generator>
</id>
...
<one-to-one name="Employee"
class="Employee"
constrained="true"/>
</class>

新しく保存された Person のインスタンスには、その Person の Employee プロパティで参照される Employee インスタンスと同じ主キー値が割り当てられます。あるいは、Employee から Person への一意制約を持つ外部キーは、次のように表現できます。

<many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>

この関連付けは、以下を Person マッピングに追加することで双方向にすることができます

<one-to-one name="Employee" class="Employee" property-ref="Person"/>

出典:第 5 章。基本的な O/R マッピング - 5.1.12。一対一

これを見てください

Hibernate コミュニティ • トピックを表示 - 子テーブルの外部キーと 1 対 1。

于 2010-01-29T06:59:14.783 に答える