简体   繁体   中英

Error relationship persistence using Spring Data JPA in a many to one

I have the following code for many to many or many to one relationship persistence using Spring JPA.

This is my repository test https://github.com/Truebu/testJpa.git

This class has three one-to-many relationships, but none work well

@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<>();

The other 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;

}

and

@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;

}

The entity relationship diagram is this:

在此处输入图像描述

My mistake is that it doesn't detect these relationships correctly.

When I run it it generates this:

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

This error is reproduced with all three classes (KeepRoutine, Statistic and Score), I don't know why

Your OneToMany mapping is not appropriate. You need to use routineAssignament the property name instead of the table name routine_assignament as shown below. This property name is defined in the ManyToOne relationship.

    @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<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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