Я пробую сопоставление один-к-одному в спящем режиме. Файлы hbm и классы выглядят следующим образом:
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:
<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:
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:
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
Причина: org.xml.sax.SAXParseException: Атрибут «not-null» должен быть объявлен для типа элемента «один-к-одному».