开发者

Hibernate one to one mapping problem

开发者 https://www.devze.com 2023-03-05 12:39 出处:网络
I\'m trying one-to-one mapping in hibernate, The hbm files and the classes are as follows, Student.hbm.xml :

I'm trying one-to-one mapping in hibernate, The hbm files and the classes are as follows,

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;
}
}

I'm getting followign errors:

Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/psl/student/Student.hbm.xml

Caused by: org.xml.sax.SAXParseException: Attribute "not-null" must be declared for element type "one-to-one".


You can't use one-to-one and a id-generator on both tables at the same time.

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.


for one to one mapping you have to do like following

<class name="dto.StudentDto" table="STUDENTDTO">
    <id name="studentId" type="long">
        <column name="STUDENTID" />
        <generator class="assigned" />
    </id>
    <property name="serialVersionUID" type="long" access="field">
        <column name="SERIALVERSIONUID" />
    </property>
    <property name="studentName" type="java.lang.String">
        <column name="STUDENTNAME" />
    </property>
    <many-to-one name="studentAddress" class="dto.Address" not-null="true" cascade="all" unique="true">
        <column name="STUDENTADDRESS" />
    </many-to-one>
</class>

note: to make one to one use unique="true" in many to one


Just remove not-null="true" in the one-to-one mapping.


The problem is hibernate-mapping-3.0.dtd does not declare an element < column> under a < one-to-one> you can only use only use elements < meta> or < formula> inside which are optional here is the segment of the dtd that refers to this.

< !-- Declares a one-to-one association between two entities (Or from a component, component element, etc. to an entity). -->

< !ELEMENT one-to-one (meta*,formula*) >

< !ATTLIST one-to-one name CDATA #REQUIRED> < !ATTLIST one-to-one formula CDATA #IMPLIED> < !ATTLIST one-to-one access CDATA #IMPLIED> < !ATTLIST one-to-one class CDATA #IMPLIED> < !ATTLIST one-to-one entity-name CDATA #IMPLIED> < !ATTLIST one-to-one cascade CDATA #IMPLIED> < !ATTLIST one-to-one outer-join (true|false|auto) #IMPLIED> < !ATTLIST one-to-one fetch (join|select) #IMPLIED> < !ATTLIST one-to-one constrained (true|false) "false"> < !ATTLIST one-to-one foreign-key CDATA #IMPLIED> < !ATTLIST one-to-one property-ref CDATA #IMPLIED> < !ATTLIST one-to-one lazy (false|proxy|no-proxy) #IMPLIED> < !ATTLIST one-to-one node CDATA #IMPLIED> < !ATTLIST one-to-one embed-xml (true|false) "true">

and for element one to one there is no attribute called not-null.

0

精彩评论

暂无评论...
验证码 换一张
取 消