해결된 질문
23.06.23 22:59 작성
·
487
0
[질문 템플릿]1. 강의 내용과 관련된 질문인가요? (예)2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)[질문 내용]
package hellojpa;
import javax.persistence.*;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Team team = new Team();
team.setName("teamA");
em.persist(team);
Member member1 = new Member();
member1.setUsername("member1");
member1.setTeam(team);
em.persist(member1);
em.flush();
em.clear();
Member m = em.find(Member.class, member1.getId());
System.out.println("m = " + m.getTeam().getClass());
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} finally {
em.close();
}
emf.close();
}
}
위와 같이 강사님의 코드를 그대로 받아쓰고 실행을 하였습니다.
java.lang.NullPointerException
at hellojpa.JpaMain.main(JpaMain.java:30)
Jun 23, 2023 10:53:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:h2:tcp://localhost/~/test]
위와 같은 에러가 발생하였습니다. team 객체를 생성하였는데 왜 NullPointerException 에러가 발생한 것일까요? 답변주시면 감사하겠습니다!!
답변 1
0
2023. 06. 26. 20:45
답변 감사합니다 David님. 주말간에 계속 코드를 뜯어보다가 member 클래스의 team team 객체를 생성하고 어노테이션으로 JoinColumn 조건에 nullable과 updatable을 false로 설정해둔 것이 문제였던 것으로 확인했습니다. 감사합니다:)
2023. 06. 25. 17:57
실행 시 나오는 코드 전문입니다!