users@glassfish.java.net

Re: JPA mapping file

From: <glassfish_at_javadesktop.org>
Date: Mon, 12 Nov 2007 23:05:12 PST

Hi:

I think what is missing here is the mapped-by attribute in the <one-to-many> element.
The mapped-by attribute must contain the name of the field that represents the many side of the relationship.

For example consider a Course - Student (one-to-many) relationship that looks like this (code snippets) without annotations being used for specifying the relationship:

*****Course.java******
@Entity
public class Course implements Serializable {

    @Id
    private Integer id;

    private Set<Student> students = new HashSet();
.......

*****Student.java*****
@Entity
public class Student implements Serializable {

    @Id
    private Integer id;

    private Course course;
......
*************************
This is how the mapping would look in the mapping file:

    <entity class="entities.norelationshipannotations.Course" >
        <attributes>
            <one-to-many name="students" mapped-by="course">
            </one-to-many>
        </attributes>
    </entity>

    <entity class="entities.norelationshipannotations.Student" >
        <attributes>
            <many-to-one name="course" />
        </attributes>
    </entity>

****************

The use of <cascade> in Lucas's example looks correct.

thanks.

varun.
[Message sent by forum member 'vr143562' (vr143562)]

http://forums.java.net/jive/thread.jspa?messageID=245157