Answer the question
In order to leave comments, you need to log in
What's wrong with @OneToMany and @ManyToOne bidirectional mapping?
When implementing bidirectional mapping, an error occurs, data is added to the database of the child element in which there is no pointer to the parent. I am using PostgreSQL.
Parent
@Entity
@Table(name = "users")
public class User{
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
@Column(name = "email")
private String email;
@Column(name = "token")
private String token;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "level")
private int level;
@Column(name = "coins")
private long coins;
@Column(name = "avatar")
private String avatar;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Quest> quests = new ArrayList<>();
//Конструктор,геттеры и сеттеры
@Entity
@Table(name = "quests")
public class Quest {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
//Конструктор, геттеры и сеттеры
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question