Ressource
@ManyToOne unidirectionnelle

Au lieu de mapper la colonne de clé étrangère post_id, PostComment utilise une relation @ManyToOne
avec l’entité parent Post. PostComment peut être associé à une référence d’objet Post existante,
et PostComment peut également être récupéré avec l’entité Post.
public class PostComment {
private Integer post_id; // ❌ préférer faire une référence
}public class PostComment {
@ManyToOne
@JoinColumn(name = "post_id")
private Post post;
}Code SQL généré
Si l’attribut @ManyToOne est défini sur une référence d’entité Post valide, alors Hibernate génère une instruction INSERT qui remplit la colonne post_id avec l’identifiant de l’entité Post associée.
Post post = entityManager.find(Post.class, 1L);
PostComment comment = new PostComment("My review");
comment.setPost(post);
entityManager.persist(comment);Donnera l’instruction SQL
INSERT INTO post_comment (post_id, review, id) VALUES (1, 'My review', 2)Si plus tard, si l’attribut Post de PostComment est défini à null, nous obtiendrons
comment.setPost(null);Un update avec un SET = NULL
UPDATE post_comment SET post_id = NULL, review = 'My review' WHERE id = 2@ManyToOne bidirectionnelle
Voir la section sur le @OneToMany