I
I
IceR0ck2017-10-03 10:34:47
Hibernate
IceR0ck, 2017-10-03 10:34:47

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<>();
//Конструктор,геттеры и сеттеры

child element
@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;
//Конструктор, геттеры и сеттеры

In the child's database, the pointer to the parent is always null. If you write user_id manually, then everything works.
59d33d553c5a8765780939.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceR0ck, 2017-10-03
@IceR0ck

Sorry for the stupid question, I thought Hibernate itself prescribes the Parent element. Issue resolved

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question