在多对一中使用 Spring Data JPA 保持错误关系
Posted
技术标签:
【中文标题】在多对一中使用 Spring Data JPA 保持错误关系【英文标题】:Error relationship persistence using Spring Data JPA in a many to one 【发布时间】:2021-08-08 06:47:32 【问题描述】:我有以下代码用于使用 Spring JPA 进行多对多或多对一关系持久性。
这是我的存储库测试https://github.com/Truebu/testJpa.git
这个类有三个一对多的关系,但没有一个很好用
@Entity(name = "routine_assignament")
@Table(name = "routine_assignament")
public class RoutineAssignament
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", updatable = false)
private Long id;
@Column(name = "date_start",nullable = true,columnDefinition = "DATE")
private Date date_start = new Date();
@Column(name = "date_end",nullable = true,columnDefinition = "DATE")
private Date date_end;
@ManyToOne
@JoinColumn(name = "id_user")
private User user;
@ManyToOne
@JoinColumn(name = "id_routine")
private Routine routine;
@OneToMany(mappedBy = "routine_assignament")
private Set<Score> scores = new HashSet<>();
@OneToMany(mappedBy = "routine_assignament")
private Set<Statistic> statistics = new HashSet<>();
@OneToMany(mappedBy = "routine_assignament")
private Set<KeepRoutine> keepRoutines = new HashSet<>();
其他类
@Entity(name = "score")
@Table(name = "score")
public class Score
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", updatable = false)
private Long id;
@Column(name = "commentary",nullable = false,columnDefinition = "TEXT", unique = true)
private String commentary;
@Column(name = "assessment",nullable = false,columnDefinition = "INT", unique = true)
private String assessment;
@ManyToOne
@JoinColumn(name = "id_routine_assignament")
private RoutineAssignament routineAssignament;
@Entity(name = "statistic")
@Table(name = "statistic")
public class Statistic
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", updatable = false)
private Long id;
@Column(name = "time",nullable = false,columnDefinition = "TEXT", unique = true)
private String time;
@ManyToOne
@JoinColumn(name = "id_routine_assignament")
private RoutineAssignament routineAssignament;
和
@Entity(name = "keep_routine")
@Table(name = "keep_routine")
public class KeepRoutine
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", updatable = false)
private Long id;
@ManyToOne
@JoinColumn(name = "id_routine_assignament")
private RoutineAssignament routineAssignament;
实体关系图是这样的:
我的错误是它没有正确检测这些关系。
当我运行它时,它会生成:
Failed to initialize JPA EntityManagerFactory: mappedBy reference an unknown target entity property: com.example.demo.model.entities.KeepRoutine.routine_assignament in com.example.demo.model.entities.RoutineAssignament.keepRoutines
这个错误在所有三个类(KeepRoutine、Statistic 和 Score)中都出现了,我不知道为什么
【问题讨论】:
【参考方案1】:您的OneToMany
映射不合适。您需要使用routineAssignament
属性名而不是表名routine_assignament
,如下所示。此属性名称在ManyToOne
关系中定义。
@OneToMany(mappedBy = "routineAssignament")
private Set<Score> scores = new HashSet<>();
@OneToMany(mappedBy = "routineAssignament")
private Set<Statistic> statistics = new HashSet<>();
@OneToMany(mappedBy = "routineAssignament")
private Set<KeepRoutine> keepRoutines = new HashSet<>();
【讨论】:
以上是关于在多对一中使用 Spring Data JPA 保持错误关系的主要内容,如果未能解决你的问题,请参考以下文章