본문 바로가기

Programming!

Spring + ( JPA Hibernate ) 로 프로젝트 진행하기. P_00

* 우선 필요 Lib는 기본적으로 받고 시작 합니다.
- Spring 2.5 : http://www.springframework.org
- hibernate 3 : http://www.hibernate.org
- JPA : http://java.sun.com
- IDE : eclipse 3.4 with J2EE

* 설정.
 EJB 환경이 아니므로 persistence.xml의 위치가 난해 합니다. 사실 생각없이 개발하는 저로써는 그닥 난해하지도 않았다는..

SJHProject/src
............................../META-INF/MANIFEST.MF
............................................./persistence.xml
.............../test
.............../WebContent
............................../META-INF
............................../WEB-INF
............................................./web.xml

1. 우선 위의 위치에 persistence.xml을 생성한 후, 기본설정을 지정합니다.
-------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="
http://java.sun.com/xml/ns/persistence"
   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_1_0.xsd"
   version="1.0">
 <persistence-unit name="-CustomUnitName-" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <properties>
   <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
   <property name="hibernate.hbm2ddl.auto" value="false"/>
   <property name="hibernate.show_sql" value="true"/>
   <property name="hibernate.format_sql" value="false"/>
   <property name="hibernate.jdbc.batch_size" value="100"/>
  </properties>
 </persistence-unit>
</persistence>
-------------------------------------------------------------------------------
-- 위의 설정은 spring 설정으로 빼셔도 됩니다.(hibernate관련 부분만..)
-- 별도로 Transaction이 묶일 일이 없어서 transaction-type 이 RESOURCE_LOCAL로 되어 있습니다. 만일 여러개의 DB의 경우 JTA(+JOTM)을 사용하시면 되고 transaction-type="JTA"로 설정하시면 됩니다.

2. spring 설정. applicationContext.xml 에 추가합니다.
-------------------------------------------------------------------------------
..............
 <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/eclipse4jDS"/>
 
 <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  <property name="persistenceXmlLocations">
   <list>
    <value>classpath:/META-INF/persistence.xml</value>
   </list>
  </property>
 </bean>

 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource" p:persistenceUnitName="-CustomUnitName-" >
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:generateDdl="false" p:database="ORACLE" p:databasePlatform="org.hibernate.dialect.Oracle9Dialect" p:showSql="false"/>
  </property>
 </bean>

 <bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory"/>
 </bean>

 <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate" p:entityManagerFactory-ref="entityManagerFactory">
 </bean>

 <tx:advice id="txAdvice" transaction-manager="jpaTxManager">
  <tx:attributes>
   <tx:method name="get*" read-only="true"/>
   <tx:method name="*"/>
  </tx:attributes>
 </tx:advice>

 <aop:config>
  <aop:pointcut id="requiredTx" expression="execution(* package.*Service.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx"/>
 </aop:config>

-------------------------------------------------------------------------------