사용자 도구

사이트 도구


java:jpa:persistence.xml

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:jpa:persistence.xml [2013/12/06 14:49]
kwon37xi
java:jpa:persistence.xml [2015/08/25 13:57] (현재)
kwon37xi [2.1]
줄 1: 줄 1:
 ====== JPA persistence.xml ====== ====== 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]]   * [[http://docs.oracle.com/cd/E16439_01/doc.1013/e13981/cfgdepds005.htm|Configuring the persistence.xml File]]
-  * ''META-INF'' 디렉토리 안에 넣는다. +  * ''META-INF/persistence.xml'' 위치 고정이다. 파일은 여러개일 수 있다. 모두 읽고 persistence-unit-name으로 구분한다. 
-  * 기본 뼈대<code xml>+  * 위치와 파일명을 변경하고자 한다면 스스로 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 ==== 
 +<code xml
 +<?xml version="1.0" encoding="utf-8"?>
 <persistence xmlns="http://java.sun.com/xml/ns/persistence" <persistence xmlns="http://java.sun.com/xml/ns/persistence"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
-         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" +    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
-         version="2.0"> +    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
-<persistence-unit name="unitname" transaction-type="RESOURCE_LOCAL"> +    version="2.0"> 
-  <provider>org.hibernate.ejb.HibernatePersistence</provider> +    <persistence-unit name="unitname" transaction-type="RESOURCE_LOCAL"> 
-  <exclude-unlisted-classes>false</exclude-unlisted-classes> +        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
-  <mapping-file>ormap.xml</mapping-file> +        <exclude-unlisted-classes>true</exclude-unlisted-classes> 
-  <class>org.acme.Employee</class> +        <mapping-file>ormap.xml</mapping-file> <!-- 필요할 경우 --
-  <class>org.acme.Person</class> +        <class>org.acme.Employee</class> 
-  <class>org.acme.Address</class> +        <class>org.acme.Person</class> 
-  <properties> +        <class>org.acme.Address</class> 
-     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> +        <properties> 
-   </properties> +            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> 
-  </persistence-unit>+            <property name="hibernate.hbm2ddl.auto" value="create-drop"/> 
 +            <property name="hibernate.show_sql" value="true" /> 
 +            <property name="hibernate.format_sql" value="true" /> 
 +            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> 
 +            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1"/> 
 +            <property name="javax.persistence.jdbc.user" value="sa"/> 
 +            <property name="javax.persistence.jdbc.password" value=""/> 
 +        </properties> 
 +    </persistence-unit>
 </persistence> </persistence>
 </code> </code>
 +==== 2.1 ====
 +<code xml>
 +<?xml version="1.0" encoding="UTF-8" ?>
 +<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
 +             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 +             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
 +                http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
 +             version="2.1">
 +    <persistence-unit name="unitname" transaction-type="RESOURCE_LOCAL">
 +        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 +...
 +    </persistence-unit>
 +</persistence>
 +</code>
 +  * ''exclude-unlisted-classes''를 ''true''로 하면 ''<class>xx</class>''로 명시한 클래스만 엔티티로 로딩한다. ''false''이면 CLASSPATH에서 자동 탐색한다.
 +  * 현재 Hibernate가 Java 클래스와 ''persistence.xml'' Resource가 존재하는 Classpath가 다를 경우 Entity 자동인식을 못한다. 따라서 ''<class>'' 명시가 더 확실할 수 있다.
 +  * JPA 2.1(Hibernate 4.3)에서 PersistenceProvider 이름이 변경되었다.
 +===== 기본 테스트 =====
 +JPA에 대한 기본 테스트는 다음과 같이 할 수 있다. ''/src/test/resources/META-INF/persistence.xml''이 있다고 간주한다.
 +<code java>
 +@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을 사용해 테스트
 +    }
 +}
 +</code>
 +
java/jpa/persistence.xml.1386308942.txt.gz · 마지막으로 수정됨: 2013/12/06 14:49 저자 kwon37xi