Hibernate pemetaan satu-ke-banyak
P粉770375450
P粉770375450 2024-04-01 19:57:24
0
2
462

Saya cuba menyimpan 6 ulasan dalam jadual Komen pangkalan data menggunakan kunci asing pada id siaran, tetapi 3 ulasan terakhir menimpa 3 ulasan pertama dengan kunci asing yang baru ditambah.

Kategori ujian:

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

P粉770375450
P粉770375450

membalas semua(2)
P粉038161873

Anda mencipta sejumlah 3 contoh ulasan (jadi 3 rekod dalam jadual pangkalan data) dan bukannya 3 kejadian setiap siaran.

Apabila anda mengemas kini ulasan post1, anda akan mempunyai ulasan post2 sebagai parameter, jadi kunci asing dari ulasan ke post2 akan bertukar kepada post1.

Jika anda mahukan 3 ulasan setiap siaran, anda memerlukan 6 contoh ulasan secara keseluruhan (2 siaran * 3 ulasan) .

P粉187677012

Ini berlaku kerana anda meletakkan objek anotasi yang sama dan kemudian hibernate fikir anda mahu menyambungkan anotasi daripada post2 更改为 post1.

Jadi anda perlu membina semula tiga anotasi ini.

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

Ini mencipta tiga lagi objek untuk ulasan.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan