2011-10-23 13 views
0

以下は、人のValueオブジェクトHibernateの1対1の関連付けの振る舞い?

マッピングファイル

<hibernate-mapping> 

<class name="com.daasl.Person" table="person"> 
    <id name="id" type="int"> 
    <generator class="increment"/> 
    </id> 

    <property name="name" column="cname" type="string"/> 
    <one-to-one name="address" class="com.daasl.Address" property-ref="personId" cascade="all"/> 

    </class> 


<class name="com.daasl.Address" table="Address"> 
    <id name="id" type="int"> 
    <generator class="increment"/> 
    </id> 

    <!--for one to one --> 
     <property name="personId" type="int"/> 
    <property name="addressLine1" type="string"/> 
以下

</hibernate-mapping> 

cfgファイルの下

<hibernate-configuration> 
<session-factory> 
    <!-- Database connection settings --> 
    <property  name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> 
    <property name="hibernate.connection.username">MyProject1</property> 
    <property name="hibernate.connection.password">tingtong</property> 
    <property name="hibernate.default_schema">MyProject1</property> 


    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> 

     <property name="hibernate.hbm2ddl.auto" >update</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

    <!-- Mapping files --> 
    <mapping resource="Person.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

私の完全なコードここ

されているあります01以下

public class Person implements Serializable { 

private int id; 
private String name; 
//private int addressId; 
private Address address; 
// getter seeter for each field 
} 

IはPersonオブジェクトを保存するとき、私は、アドレスオブジェクトはcorreponding伴って含むも保存されることを期待コード前述のようにアドレス値オブジェクトの下

public class Address { 

private int id; 
private int personId; 
private String addressLine1; 
// getter seeter for each field 

} 

メインメソッドからのコードスニペット

tx = session.beginTransaction(); 
      person = (Person)session.get(Person.class, 4); 

     // Create a Person object and save it 
     Person p1 = new Person(); 
     p1.setName("Scott"); 
     Address add1= new Address(); 
     add1.setAddressLine1("NY1"); 
     p1.setAddress(add1); 
     session.save(p1); 


    // p1.setName("mohit1"); 
     // Retrieve the person objects 
     person = (Person)session. get(Person.class,1); 

     tx.commit(); 
     tx = null; 

ありますpersonid上記のプログラムを実行すると、はアドレステーブルのpersonidカラムに0となります。私の理解によるとpersonidは、人のテーブルのID値で挿入する必要があります。右?

私が知りたいと思っていた第二の事は、one to oneマッピングが外国子キー関係を作成するかどうかです。上記の例のように、personidカラムの内部アドレステーブルがpersonテーブルの主キー、つまりidカラムを指しているはずです。 しかし、それはアドレステーブルのpersonid列に外部キー制約を作成しませんでした。

ここに何か不足しています

答えて

3

はい、あなたは何か不足しています。休止状態のようなORMのポイントは、オブジェクトの間の関連付けを実装できることです。

関連は単方向にすると、アドレスからpersonIdフィールドを削除するか、それが双方向作り、タイプPersonの、personフィールドでpersonIdフィールドを置き換えるのどちらか。この場合、アソシエーションの一方の側(person.address側)は他方のインバース側(アドレス側)と逆でなければなりません。

これは、Hibernateのリファレンスドキュメントで説明されています。それを読んで。単方向の1対1の場合はhttp://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-unidirectional-121、双方向の1対1の場合はhttp://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-bidirectional-121を参照してください。

関連する問題