Search code examples
jpaeclipselink

EclipseLink JPA - @OneToMany / @ManyToOne


I am using Java 8, EE7, NetBeans 8.0.2, EclipseLink 2.5.2 & Derby

Originally my entity classes were created for me by NetBeans which probably defaults to defining both sides of a relationship when foreign keys are involved. Recently I did a bunch of research to understand this better and have optimized the classes to uni-directional relationships when the non-owning side won't be maintained and to eliminate CASCADE when it isn't desired.

After rebuilding (with no compile errors) I can't get it to run due to an error that I don't understand relative to my code.

Caused by: Exception [EclipseLink-7154] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [clubCourseList] in entity class [class com.clubscores.entity.Course] has a mappedBy value of [COURSE] which does not exist in its owning entity class [class com.clubscores.entity.ClubCourse]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.

In the example Course defines an auto-generated field in the DB "ID" and ClubCourse defines two foreign keys "CLUB" and "COURSE" so that each club can relate to many golf courses. Course does not map back to ClubCourse in the DB but in this case it is a relationship I will maintain in my code.

Course: //defines golf courses

@OneToMany(mappedBy = "COURSE")
private List<ClubCourse> clubCourseList;

ClubCourse: //intermediary table linking clubs and courses

@JoinColumn(name = "COURSE", referencedColumnName = "ID")
@ManyToOne(optional = false)
private Course course;

If I rename the "course" property in ClubCourse to something else and adjust the mappedBy accordingly then the error just moves on to another relationship from other tables.

Is there something wrong with this definition or what else could cause this error?


Solution

  • You specified mappedBy = "COURSE", but your field name is course. This is case sensitive. Note that mappedBy expects field name, and not column name. Just change to mappedBy = "course" and it should work.