繁体   English   中英

使用 Spring 数据 JPA 在多对一中的错误关系持久性

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM