我正在嘗試使用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();
}
我不知道爲什麼。那麼,有人可以向我解釋嗎?
向我們展示您的實體,尤其是參考資料中的註釋。 – JimmyB
重新讀你的問題,似乎問題必須在'setAddressByStudentId'和/或'setStudentByAddressId'。這些方法有什麼作用?告訴我們da codez! – JimmyB
再讀一遍,我不確定你的意思是「第一行未註釋」。你的意思是執行行「#1」*而不是*上面的行或除此之外? – JimmyB