>  Q&A  >  본문

일대다 매핑

게시물 ID에 대한 외래 키를 사용하여 데이터베이스의 댓글 테이블에 6개의 댓글을 유지하려고 하는데, 새로 추가된 외래 키로 마지막 3개의 댓글이 처음 3개의 댓글을 덮어씁니다.

테스트 카테고리:

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粉770375450P粉770375450181일 전387

모든 응답(2)나는 대답할 것이다

  • P粉038161873

    P粉0381618732024-04-02 11:19:49

    게시물당 3개의 인스턴스가 아닌 총 3개의 댓글 인스턴스(데이터베이스 테이블의 레코드 3개)를 생성합니다.

    post1 댓글을 업데이트하면 post2 댓글이 매개변수로 포함되므로 댓글에서 post2에 대한 외래 키가 post1로 변경됩니다.

    게시물당 3개의 댓글을 원할 경우 총 6개의 댓글 인스턴스(게시물 2개 * 댓글 3개)가 필요합니다.

    회신하다
    0
  • P粉187677012

    P粉1876770122024-04-02 00:07:37

    이는 동일한 주석 개체를 배치한 다음 최대 절전 모드에서 post2 更改为 post1의 주석을 연결하려고 한다고 생각하기 때문에 발생합니다.

    따라서 이 세 가지 주석을 다시 작성해야 합니다.

    으아아아

    이렇게 하면 댓글용 개체가 세 개 더 생성됩니다.

    회신하다
    0
  • 취소회신하다