![](/img/trans.png)
[英]Spring Data JPA Specification using CriteriaBuilder with a one to many relationship
[英]Error relationship persistence using Spring Data JPA in a many to one
我有以下代碼用於使用 Spring JPA 的多對多或多對一關系持久性。
這是我的存儲庫測試https://github.com/Truebu/testJpa.git
這個 class 有三個一對多的關系,但沒有一個很好用
@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)中都出現了,我不知道為什么
您的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<>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.