1

JPA JoinColumn vs mappedBy says

The annotation @JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table),

Now I have below code where I have mentioned @JoinColumn under college entity which is non owner entity and student is the owner entity as it has column pointing to primary key column in student table.
So it is reverse of above statement.

Does it mean above statement is applicable for one to one or many to one association not for one to many ?

@Entity
public class College {

    .........
    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="COLLEGE_COLLEGEID")
    private List<Student> students;


}

@Entity
public class Student {
...

}

UPDATE :- i have college and student tables. Student has column college_collegeId which is foreign key to collegeId in college table. With below code i am able to save college and student entity where college_collegeId contains value pointing to foreign key to collegeId in college table

public  void saveCollege() {
    Student student1= new Student();
    student1.setStudentName("Student1");


    ArrayList<Student> list= new ArrayList<Student>();
    list.add(student1);

    College college1= new College();
    college1.setCollegeName("College3");
    college1.setCollegeId(1);

    college1.setStudents(list);


    getEntityManager().merge(college1);
}
Community
  • 1
  • 1
emilly
  • 10,060
  • 33
  • 97
  • 172
  • No, it means that your mapping is incorrect, since you're not respecting the rules. – JB Nizet May 01 '15 at 10:39
  • 1
    @NeilStockton No its required only if we need bidirectional mapping. Otherwise mappedBy is not required. – emilly May 01 '15 at 10:40
  • @JBNizet which mapping is incorrect and which rule i am not respecting ? – emilly May 01 '15 at 10:45
  • i need unidirectional relationship. In my update i have removed college property from student class – emilly May 01 '15 at 10:59
  • You had a bidirectional association, so the OneToMany had to have a mappedBy attribute, and not have a JoinColumn. Now that the association is unidirectional, it's correct. – JB Nizet May 01 '15 at 11:06
  • OR-Mapping is always a little bit tricky. Have a look at this site: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany I think it's quite useful. Maybe this helps you, too. – LarsBauer May 01 '15 at 11:08
  • @JBNizet so in terms of unidirectional one to many mapping statement "The annotation JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table)," is not correct .In that case it is reverse. Am i correct now ? – emilly May 01 '15 at 11:18
  • 2
    In the case of a unidirectional association, JoinColumn indicates the name of the join column on the many side. This is well documented: http://docs.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html – JB Nizet May 01 '15 at 11:37
  • @JBNizet Thanks. Then my understanding is correct. Got bit confused by that statement. – emilly May 01 '15 at 11:59

0 Answers0