====== JPA persistence.xml ====== * [[http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/index.html|JPA Persistence XML Schemas]] * [[http://docs.oracle.com/cd/E16439_01/doc.1013/e13981/cfgdepds005.htm|Configuring the persistence.xml File]] * ''META-INF/persistence.xml'' 위치 고정이다. 파일은 여러개일 수 있다. 모두 읽고 persistence-unit-name으로 구분한다. * 위치와 파일명을 변경하고자 한다면 스스로 XML 파일로부터 ''PersistenceUnitInfo'' 인스턴스를 생성하여 넘기는 방법만 있는 듯 하다. Spring이 이렇게 처리하는 듯. * [[http://docs.jboss.org/hibernate/core/4.3/devguide/en-US/html/apa.html|Appendix A. Configuration properties]] ===== 기본 persistence.xml 뼈대 ===== ==== 2.0 ==== org.hibernate.ejb.HibernatePersistence true ormap.xml org.acme.Employee org.acme.Person org.acme.Address ==== 2.1 ==== org.hibernate.jpa.HibernatePersistenceProvider ... * ''exclude-unlisted-classes''를 ''true''로 하면 ''xx''로 명시한 클래스만 엔티티로 로딩한다. ''false''이면 CLASSPATH에서 자동 탐색한다. * 현재 Hibernate가 Java 클래스와 ''persistence.xml'' Resource가 존재하는 Classpath가 다를 경우 Entity 자동인식을 못한다. 따라서 '''' 명시가 더 확실할 수 있다. * JPA 2.1(Hibernate 4.3)에서 PersistenceProvider 이름이 변경되었다. ===== 기본 테스트 ===== JPA에 대한 기본 테스트는 다음과 같이 할 수 있다. ''/src/test/resources/META-INF/persistence.xml''이 있다고 간주한다. @Slf4j public class GenericJPATest { private static EntityManagerFactory emf; private EntityManager em; private EntityTransaction transaction; @BeforeClass public static void setUpClass() throws Exception { // 필요한 경우 아래에서 Map으로 프라퍼티들을 넘길 수 있다. emf = Persistence.createEntityManagerFactory("[test-persistence-unit]"); } @AfterClass public static void tearDownClass() throws Exception { if (emf != null) { emf.close(); } } @Before public void setUp() throws Exception { em = emf.createEntityManager(); transaction = em.getTransaction(); } @After public void tearDown() throws Exception { if (em != null) { em.close(); } } @Test public void test() throws Exception { transaction과 em을 사용해 테스트 } }