2011-10-23 33 views
0

下面是我完整的代码Hibernate中一对一关联的行为?

下面是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> 

下面是映射文件

<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> 
以下

是人的价值对象

public class Person implements Serializable { 

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

是地址值对象

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; 

代码段如上述代码时,我保存Person对象我期望也保存地连同correponding地址对象personid当我运行以上程序personid正在以0 in personid列在地址表内。根据我的理解,personid应该与人员表id值一起插入。对?

我想知道的第二件事是:one to one映射创建外键子键关系。如上例中我期待personid列内的地址表应该指向人表的主键即id列。 但是它没有在地址表的personid列上创建任何外键约束。

我在这里丢失了什么

回答

3

是的,你错过了一些东西。像休眠这样的ORM的要点是能够实现对象之间的关联。

要么使所述关联是单向的,并从地址删除personId字段,或使它双向的,并通过一个person字段替换Person类型的personId字段。在这种情况下,协会的一方(person.address方)必须与另一方(adress.person方)相反。

这在Hibernate参考文档中有描述。阅读。单向一对一请参阅http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-unidirectional-121,双向一对一参见http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-bidirectional-121

相关问题