로컬에서 조회 기능 확인할 때 자동으로 생성되는 샘플데이터가 있으면 편하다.
다음과 같이 생성하자
@Component
@RequiredArgsConstructor
public class InitContents {
private final InitContentsService initContentsService;
@PostConstruct
public void init(){
initContentsService.init();
}
@Component
static class InitContentsService{
@PersistenceContext
private EntityManager em;
@Transactional
public void init(){
Board board = new Board("board1");
em.persist(board);
Topic topic1 = new Topic("topic1", board);
Topic topic2 = new Topic("topic2", board);
em.persist(topic1);
em.persist(topic2);
Card card1 = new Card("card1", "des1", topic1);
Card card2 = new Card("card2", "des2", topic1);
Card card3 = new Card("card3", "des3", topic2);
Card card4 = new Card("card4", "des4", topic2);
Card card5 = new Card("card5", "des5", topic2);
Card card6 = new Card("card6", "des6", topic2);
em.persist(card1);
em.persist(card2);
em.persist(card3);
em.persist(card4);
em.persist(card5);
em.persist(card6);
CheckList checkList1 = new CheckList("checkList1", card1);
CheckList checkList2 = new CheckList("checkList2", card1);
em.persist(checkList1);
em.persist(checkList2);
CheckItem checkItem1 = new CheckItem("checkItem1",checkList1);
CheckItem checkItem2 = new CheckItem("checkItem2",checkList1);
CheckItem checkItem3 = new CheckItem("checkItem3",checkList1);
CheckItem checkItem4 = new CheckItem("checkItem4",checkList2);
CheckItem checkItem5 = new CheckItem("checkItem5",checkList2);
checkItem1.update("checkItem1", "N", "Y");
checkItem2.update("checkItem2", "N", "Y");
em.persist(checkItem1);
em.persist(checkItem2);
em.persist(checkItem3);
em.persist(checkItem4);
em.persist(checkItem5);
checkList1.addCheckItem(checkItem1);
checkList1.addCheckItem(checkItem2);
checkList1.addCheckItem(checkItem3);
checkList2.addCheckItem(checkItem4);
checkList2.addCheckItem(checkItem5);
Label label1 = new Label("label1", "green");
Label label2 = new Label("label2", "red");
Label label3 = new Label("label3", "blue");
em.persist(label1);
em.persist(label2);
em.persist(label3);
CardLabel cardLabel1 = new CardLabel(card1, label1);
CardLabel cardLabel2 = new CardLabel(card2, label2);
CardLabel cardLabel3 = new CardLabel(card1, label3);
CardLabel cardLabel4 = new CardLabel(card4, label3);
card1.addCardLabel(cardLabel1);
card2.addCardLabel(cardLabel2);
card1.addCardLabel(cardLabel3);
card4.addCardLabel(cardLabel4);
}
}
}
왜 바로 @PostContruct 에 넣지 않고 따로 빈을 만들었는가?
This is as defined, actually: init methods (such as @PostConstruct methods) are always called on the target instance itself.
The proxy will only be generated once the target instance has been fully initialized...
In other words, the @Transactional proxy isn't even created at the point of the @PostConstruct call yet.
@PostConstructor 는 인스턴스 자체에서 호출됨
프록시는 인스턴스가 완전히 초기화된 후에 생성됨
그래서 @Transcational 프록시는 @PostConstruct 호출 시점에 생성되지 않음