简体   繁体   English

一对多映射休眠

[英]One to Many Mapping Hibernate

I'm trying to persist 6 comments with a foreign key of post id in the Comment table in the database, but the last 3 comments override the first 3 comments with a newly added foreign key.我试图在数据库的 Comment 表中使用 post id 的外键保留 6 条评论,但最后 3 条评论用新添加的外键覆盖前 3 条评论。

Test class:测试类:

Comments comments = new Comments("1st Comment", new Date(System.currentTimeMillis()));
Comments comments2 = new Comments("2st Comment", new Date(System.currentTimeMillis()));
Comments comments3 = new Comments("3st Comment", new Date(System.currentTimeMillis()));

ArrayList<Comments> arrayList = new ArrayList<>();
arrayList.add(comments);
arrayList.add(comments2);
arrayList.add(comments3);

// Insert Without Comment
Post post1 = new Post("1st Post", "1st Post Description", new ArrayList<Comments>(), new Date(System.currentTimeMillis()));
postReposetory.save(post1);

// Insert With Comment
Post post2 = new Post("2st Post", "2st Post Description", arrayList, new Date(System.currentTimeMillis()));
postReposetory.save(post2);

// Update (Insert Comment)
post1.setComments(arrayList);
post1.setUpdatedAt(new Date(System.currentTimeMillis()));
postReposetory.save(post1);

You are creating a total of 3 comments instances (so 3 records in the database table), not 3 instances per post.您正在创建总共 3 个评论实例(因此数据库表中有 3 条记录),而不是每个帖子 3 个实例。

When you update post1 comments you are giving post2 comments as argument, so the foreign key from comments to post2 will change to post1.当您更新 post1 评论时,您将 post2 评论作为参数,因此从评论到 post2 的外键将更改为 post1。

If you want to have 3 comments per post you need 6 comments instances total (2 posts * 3 comments) .如果您希望每个帖子有 3 条评论,则总共需要 6 个评论实例(2 条帖子 * 3 条评论)

This happens because you put the same objects of comments and then hibernate consider that you want to change the connections of the comments from post2 to post1 .发生这种情况是因为您放置了相同的评论对象,然后休眠考虑要将评论的连接从post2post1

Therefore you have to construct again the three comments.因此,您必须再次构建三个注释。

    Comments comments = new Comments("1st Comment", new Date(System.currentTimeMillis()));
    Comments comments2 = new Comments("2st Comment", new Date(System.currentTimeMillis()));
    Comments comments3 = new Comments("3st Comment", new Date(System.currentTimeMillis()));

    ArrayList<Comments> arrayList = new ArrayList<>();
    arrayList.add(comments);
    arrayList.add(comments2);
    arrayList.add(comments3);

    // Insert With Comment
    Post post1 = new Post("1st Post", "1st Post Description", new ArrayList<Comments>(), new Date(System.currentTimeMillis()));
    postReposetory.save(post1);
    
    // Insert Without Comment
    Post post2 = new Post("2st Post", "2st Post Description", arrayList, new Date(System.currentTimeMillis()));
    postReposetory.save(post2);

    // Update (Insert Comment)
    comments = new Comments("1st Comment", new Date(System.currentTimeMillis()));
    comments2 = new Comments("2st Comment", new Date(System.currentTimeMillis()));
    comments3 = new Comments("3st Comment", new Date(System.currentTimeMillis()));
    post1.setComments(List.of(comments, comments2, comments3));
    post1.setUpdatedAt(new Date(System.currentTimeMillis()));
    postReposetory.save(post1);

In this way, other three objects are created for comments.这样,就创建了其他三个对象用于注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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