休眠一对一映射问题 [英] Hibernate one to one mapping problem
问题描述
我正在尝试在休眠模式下进行一对一映射,hbm文件和类如下,
I'm trying one-to-one mapping in hibernate, The hbm files and the classes are as follows,
Student.hbm.xml:
Student.hbm.xml :
<hibernate-mapping>
<class name="com.psl.student.Student" table="STUDENT">
<meta attribute="class-description">This class contains student details.</meta>
<id name="studentId" type="long" column="STUDENT_ID">
<generator class="native" />
</id>
<property name="studentName" type="string" length="100" column="STUDENT_NAME" />
<one-to-one name="studentAddress" class="com.psl.student.Address" not-null="true" column ="STUDENT_ADDRESS" />
</class>
</hibernate-mapping>
Address.hbm.xml:
Address.hbm.xml :
<hibernate-mapping>
<class name="com.psl.student.Address" table="ADDRESS">
<meta attribute="class-description">This class contains the student's address details.</meta>
<id name="addressId" type="long" column="ADDRESS_ID">
<generator class="native" />
</id>
<property name="street" column="ADDRESS_STREET" type="string" length="250" />
<property name="city" column="ADDRESS_CITY" type="string" length="50" />
<property name="state" column="ADDRESS_STATE" type="string" length="50" />
<property name="zipcode" column="ADDRESS_ZIPCODE" type="string" length="10" />
</class>
</hibernate-mapping>
Student.java:
Student.java:
public class Student implements java.io.Serializable {
private long studentId;
private String studentName;
private Address studentAddress;
public Student() {
}
public Student(String studentName, Address studentAddress) {
this.studentName = studentName;
this.studentAddress = studentAddress;
}
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Address getStudentAddress() {
return this.studentAddress;
}
public void setStudentAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
Address.java:
Address.java :
public class Address implements java.io.Serializable {
private long addressId;
private String street;
private String city;
private String state;
private String zipcode;
public Address() {
}
public Address(String street, String city, String state, String zipcode) {
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}
public long getAddressId() {
return this.addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
我收到关注错误:
原因:org.hibernate.InvalidMappingException:无法从资源com/psl/student/Student.hbm.xml解析映射文档
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/psl/student/Student.hbm.xml
由以下原因引起:org.xml.sax.SAXParseException:必须为元素类型一对一"声明属性"not-null".
Caused by: org.xml.sax.SAXParseException: Attribute "not-null" must be declared for element type "one-to-one".
推荐答案
您不能同时在两个表上同时使用一对一和id生成器.
You can't use one-to-one and a id-generator on both tables at the same time.
表示主键已同步.在这种情况下:一个人和一个具有相同ID的地址将属于同一个人.因为学生引用了地址,所以学生的主键将充当外键来寻址,并且需要使用地址的主键.当它使用本地生成器生成自己的ID时,这是不可能的.
one-to-one in a relational database means that the primary keys get synchronized. In this case: a person and an address with the same id will belong together. Because the student references the address, the students primary key would act as a foreign key to address and need to use the addresses primary key. This isn't possible when it generates its own id using the native generator.
大多数一对一的关系实际上不应该是一对一的.如果您只想在该地址上使用外键,则只需将其映射为多对一即可.
Most one-to-one relations shouldn't actually be one-to-one. If you just want to have a foreign key to the address, map it simply as many-to-one.
这篇关于休眠一对一映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!