일대다 매핑
P粉770375450
P粉770375450 2024-04-01 19:57:24
0
2
380

게시물 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粉770375450
P粉770375450

모든 응답(2)
P粉038161873

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

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

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

P粉187677012

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

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

으아아아

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

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!