在多对一中使用 Spring Data JPA 的错误关系持久性
Error relationship persistence using Spring Data JPA in a many to one
我有以下代码用于使用 Spring JPA 的多对多或多对一关系持久性。
这是我的存储库测试https://github.com/Truebu/testJpa.git
这个class有3个一对多关系,但是none效果很好
@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<>();
其他classes
@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
所有三个 classes(KeepRoutine、Statistic 和 Score)都重现此错误,我不知道为什么
您的 OneToMany
映射不合适。您需要使用 routineAssignament
属性 名称而不是 table 名称 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 JPA 的多对多或多对一关系持久性。
这是我的存储库测试https://github.com/Truebu/testJpa.git
这个class有3个一对多关系,但是none效果很好
@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<>();
其他classes
@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
所有三个 classes(KeepRoutine、Statistic 和 Score)都重现此错误,我不知道为什么
您的 OneToMany
映射不合适。您需要使用 routineAssignament
属性 名称而不是 table 名称 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<>();