EclipseLink JPA - @OneToMany / @ManyToOne

EclipseLink JPA - @OneToMany / @ManyToOne

我正在使用 Java 8、EE7、NetBeans 8.0.2、EclipseLink 2.5.2 和 Derby

最初我的实体 类 是由 NetBeans 为我创建的,当涉及外键时,它可能默认定义关系的双方。最近我做了很多研究来更好地理解这一点,并优化了 类 在非拥有方不维护时的单向关系,并在不需要时消除 CASCADE。

重建后(没有编译错误)由于我无法理解与我的代码相关的错误,我无法将其转换为 运行。

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.

在示例中,Course 在数据库中定义了一个自动生成的字段 "ID",而 ClubCourse 定义了两个外键 "CLUB" 和 "COURSE",因此每个俱乐部都可以关联到多个高尔夫球场. Course 不会映射回数据库中的 ClubCourse,但在这种情况下,这是我将在我的代码中维护的关系。

课程://定义高尔夫球场

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

ClubCourse://中介 table 链接俱乐部和课程

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

如果我将 ClubCourse 中的 "course" 属性 重命名为其他名称并相应地调整 mappedBy,那么错误就会从其他 table 转移到另一个关系。

这个定义有问题吗?或者还有什么可能导致这个错误?

您指定了 mappedBy = "COURSE",但您的字段名称是 course。这是区分大小写的。请注意,mappedBy 需要字段名,而不是列名。只需更改为 mappedBy = "course" 即可。