2016-12-01 81 views
0

我正在嘗試使用Hibernate中的一對一映射工作,並且我感到困惑。我有兩個表學生和地址映射通過一對一的關係,併爲地址表設置外鍵。我創建了一個功能,將數據保存到數據庫中,如下所示:無法在Hibernate中保存兩種實體之間的一對一關係?

public static void oneToOneMappingWithXML(Session session) throws Exception { 
    StudentEntity tom = new StudentEntity(); 
    tom.setName("Tom"); 
    tom.setAge(10); 
    tom.setClazz("5B"); 
    tom.setStudentId(1); 

    AddressEntity tom_address = new AddressEntity(); 
    tom_address.setCity("New York"); 
    tom_address.setAddressId(1); 

    tom.setAddressByStudentId(tom_address); 
    //tom_address.setStudentByAddressId(tom); //#1 
    session.save(tom); 
    session.getTransaction().commit(); 
} 

這是我的映射:

<class name="com.model.xmlType.AddressEntity" table="ADDRESS" schema="CHINOOK"> 
    <id name="addressId"> 
     <column name="ADDRESS_ID" sql-type="number(10)" precision="10"/> 
    </id> 
    <property name="city"> 
     <column name="CITY" sql-type="nvarchar2(50)" length="50"/> 
    </property> 
    <one-to-one name="studentByAddressId" class="com.model.xmlType.StudentEntity"/> 
</class> 


<class name="com.model.xmlType.StudentEntity" table="STUDENT" schema="CHINOOK"> 
    <id name="studentId"> 
     <column name="STUDENT_ID" sql-type="number(10)" precision="10"/> 
    </id> 
    <property name="name"> 
     <column name="NAME" sql-type="nvarchar2(50)" length="50"/> 
    </property> 
    <property name="age"> 
     <column name="AGE" sql-type="number(10)" precision="10"/> 
    </property> 
    <property name="clazz"> 
     <column name="CLASS" sql-type="nvarchar2(10)" length="10"/> 
    </property> 
    <one-to-one name="addressByStudentId" class="com.model.xmlType.AddressEntity" cascade="all"/> 

實體類

學生:

public class StudentEntity { 
    private Integer studentId; 
    private String name; 
    private Integer age; 
    private String clazz; 
    private AddressEntity addressByStudentId; 

    public Integer getStudentId() { 
     return studentId; 
    } 

    public void setStudentId(Integer studentId) { 
     this.studentId = studentId; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Integer getAge() { 
     return age; 
    } 

    public void setAge(Integer age) { 
     this.age = age; 
    } 

    public String getClazz() { 
     return clazz; 
    } 

    public void setClazz(String clazz) { 
     this.clazz = clazz; 
    } 


    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     StudentEntity that = (StudentEntity) o; 

     if (studentId != null ? !studentId.equals(that.studentId) : that.studentId != null) return false; 
     if (name != null ? !name.equals(that.name) : that.name != null) return false; 
     if (age != null ? !age.equals(that.age) : that.age != null) return false; 
     if (clazz != null ? !clazz.equals(that.clazz) : that.clazz != null) return false; 

     return true; 
    } 

    public int hashCode() { 
     int result = studentId != null ? studentId.hashCode() : 0; 
     result = 31 * result + (name != null ? name.hashCode() : 0); 
     result = 31 * result + (age != null ? age.hashCode() : 0); 
     result = 31 * result + (clazz != null ? clazz.hashCode() : 0); 
     return result; 
    } 

    public AddressEntity getAddressByStudentId() { 
     return addressByStudentId; 
    } 

    public void setAddressByStudentId(AddressEntity addressByStudentId) { 
     this.addressByStudentId = addressByStudentId; 
    } 
} 

地址:

public class AddressEntity { 
    private Integer addressId; 
    private String city; 
    private StudentEntity studentByAddressId; 

    public Integer getAddressId() { 
     return addressId; 
    } 

    public void setAddressId(Integer addressId) { 
     this.addressId = addressId; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     AddressEntity that = (AddressEntity) o; 

     if (addressId != null ? !addressId.equals(that.addressId) : that.addressId != null) return false; 
     if (city != null ? !city.equals(that.city) : that.city != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = addressId != null ? addressId.hashCode() : 0; 
     result = 31 * result + (city != null ? city.hashCode() : 0); 
     return result; 
    } 

    public StudentEntity getStudentByAddressId() { 
     return studentByAddressId; 
    } 

    public void setStudentByAddressId(StudentEntity studentByAddressId) { 
     this.studentByAddressId = studentByAddressId; 
    } 
} 

它工作得很好,兩個實體都保存了。但是,當#1行未註釋時,只有實體學生會保存。代碼如下:

public static void oneToOneMappingWithXML(Session session) throws Exception { 
    StudentEntity tom = new StudentEntity(); 
    tom.setName("Tom"); 
    tom.setAge(10); 
    tom.setClazz("5B"); 
    tom.setStudentId(1); 

    AddressEntity tom_address = new AddressEntity(); 
    tom_address.setCity("New York"); 
    tom_address.setAddressId(1); 

    tom.setAddressByStudentId(tom_address); 
    tom_address.setStudentByAddressId(tom); //#1 
    session.save(tom); 
    session.getTransaction().commit(); 
} 

我不知道爲什麼。那麼,有人可以向我解釋嗎?

+1

向我們展示您的實體,尤其是參考資料中的註釋。 – JimmyB

+0

重新讀你的問題,似乎問題必須在'setAddressByStudentId'和/或'setStudentByAddressId'。這些方法有什麼作用?告訴我們da codez! – JimmyB

+0

再讀一遍,我不確定你的意思是「第一行未註釋」。你的意思是執行行「#1」*而不是*上面的行或除此之外? – JimmyB

回答

0

請注意,當您僅在一側設置引用時,Hibernate不會自動使對象引用成爲雙向對象。因此,必須確保,在雙向參考,雙方得到正確設置之前堅持。

嘗試將實體看作數據庫中的記錄而不是對象。如果對象「湯姆」不知道,他甚至有一個地址,「湯姆」被要求堅持自己,「湯姆」如何堅持他的地址? (實際上,你可以用保存沒有地址的「tom」,然後保存一個引用「tom」的地址;然後,如果你重新加載「tom」,他會突然有一個地址,因爲數據庫沒關係的關係有哪個方向,但你不應該這樣做。)

總是正確初始化兩側的雙向參考,你應該是好的。

+0

我已經更新了我的映射。我認爲我們不需要看實體類,因爲一切運行良好,我已經在互聯網上的教程之後完成了。當我添加第1行時,我想看看會發生什麼,並且問題是無法在第1行添加時保存地址:(。鏈接教程:http://www.mkyong.com/hibernate/hibernate-一對一的關係 - 例如/ –

0
session.save(tom_address); 
session.save(tom); 
session.getTransaction().commit(); 

在保存學生之前保存地址。

+0

我不這麼認爲。因爲它是一對一的關係,我認爲我們不需要保存這兩個實體。 –

相關問題