package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory enf = Persistence.createEntityManagerFactory("hello");
EntityManager em = enf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member();
member.setUsername("member1");
em.persist(member);
Team team = new Team();
team.setName("TeamA");
team.getMembers().add(member); //...1
em.persist(team);
em.flush();
em.clear();
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
enf.close();
}
}
이렇게 실행하면 어떻게 될까요?
insert 쿼리는 분명 2개가 나갔습니다.
그리고 DB를 확인해봅시다.
이상하게도 TEAM_ID가 null입니다.
Member 엔티티에 있는 team이 연관관계 주인입니다.
Team 엔티티에 있는 members는 mappedBy라서 읽기 전용입니다. 즉, JPA에서 update나 insert할 때 아예 보질 않습니다.
그래서 코드를 수정해봅니다.
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory enf = Persistence.createEntityManagerFactory("hello");
EntityManager em = enf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Team team = new Team();
team.setName("TeamA");
//team.getMembers().add(member);
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setTeam(team); //...1
em.persist(member);
em.flush();
em.clear();
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
enf.close();
}
}