작성
·
169
0
package hellojpa;
import jakarta.persistence.*;
import java.util.List;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
//code
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member();
member.setUsername("member1");
member.setHomeAddress(new Address("homeCity", "street", "1000"));
member.getFavoritesFoods().add("치킨");
member.getFavoritesFoods().add("족발");
member.getFavoritesFoods().add("피자");
member.getAddressHistory().add(new Address("old1", "street1", "10001"));
member.getAddressHistory().add(new Address("old2", "street2", "10002"));
em.persist(member);
em.flush();
em.clear();
System.out.println("=============");
Member findMember = em.find(Member.class, member.getId());
Address a = findMember.getHomeAddress();
findMember.setHomeAddress(new Address("city", a.getStreet(), a.getZipcode()));
findMember.getFavoritesFoods().remove("치킨");
findMember.getFavoritesFoods().add("한식");
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
private static Member saveMember(EntityManager em) {
Member member = new Member();
member.setUsername("member1");
em.persist(member);
return member;
}
}
이 때
member.getAddressHistory().add(new Address("old1", "street1", "10001"));
위 코드는 update문이 나갑니다. 그 이유는 알고 있는데
findMember.getFavoritesFoods().add("한식");
이 코드는 update가 아니라 insert문이 나가더라고요. 왜 update가 아니라 insert문이 나가는 건가요?