본문 바로가기

JPA

영속성 유닛(Persistence Unit)

A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. 

영속성 유닛은 응용프로그램의 EntityManager 인스턴스에 의해 관리되는 모든 엔티티 클래스 집합을 정의한다.

 

  • JPA 역시 VO객체와 테이블을 매핑하기 위한 다양한 설정 정보가 필요한데, 이를 위해 persistence.xml 파일을 환경설정 파일로 사용한다. persistence.xml 파일은 <persistence>를 루트 엘리먼트로 사용하며, 영속성 유닛(persistence-unit)과 관련된 다양한 정보가 설정된다.

 

  • 영속성 유닛은 연동할 연동할 데이터베이스당 하나씩 등록하며, 영속성 유닛에 설정된 이름은 나중에 DAO클래스를 구현할 때 EntityManagerFactory 객체에 사용된다.
Persistence Unit 설정 <persistence-unit name="JPAProject"></persistence-unit>
Java 소스 EntityManagerFactory emf =
       Persistence.createEntityManagerFactory("JPAProject");
EntityManager em = emf.createEntityManager();

 

  • JPA를 이용하여 DB연동을 구현하려며 EntityManager 객체가 필요하다. 그런데 EntityManager를 얻으려면 EntityManager 객체를 생성하기 위한 공장 기능의 EntityManagerFactory 객체가 필요하다. 이 EntityManagerFactory를 생성할 때, 영속성 유닛이 사용된다.

 

 

 

[참조 - 스프링 퀵 스타트]