2015-01-25 2 views
4

Persistence.xml 파일을 사용하지 않고 Spring에서 EclipseLink를 구성하는 방법을 알아 내는데 어려움이 있습니다. Hibernate의 성가신 LazyLoadExceptions를 피하기 위해 EclipseLink로 정적 제직을 구성하고 싶습니다.Persistence.xml이없는 EclipseLink 및 Spring Config

다음은 내 Hibernate & Spring 구성이 정상적으로 작동 한 것입니다. EclipseLink와 비슷한 것을하고 싶지만 완전하고 관련성 높은 문서를 찾기 위해 정말 고심하고 있습니다. 내가 PostgreSQL을 dbms.My 구성으로는 EclipseLink 봄의 데이터를 사용하고

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

    <context:annotation-config /> 

    <jpa:repositories base-package="com.something.ots.repository" /> 

    <jee:jndi-lookup jndi-name="${datasource.jndi.name}" id="dataSource" expected-type="javax.sql.DataSource" /> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> 
     <property name="jpaDialect" ref="jpaDialect" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.dscreative.honda.ots</value> 
      </list> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
       <prop key="hibernate.connection.charSet">${hibernate.connection.charSet}</prop> 
       <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> 
       <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
     <property name="database" value="${jpa.vendor.database}" /> 
     <property name="showSql" value="${jpa.vendor.showSql}"/> 
     <property name="generateDdl" value="${jpa.vendor.generateDdl}"/> 
     <property name="databasePlatform" value="${jpa.vendor.databasePlatform}"/> 
    </bean> 

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 

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

    <tx:annotation-driven transaction-manager="transactionManager" /> 

</beans> 

답변

0

는 주석을 기반으로합니다. XML 구성으로 변환하기 쉽습니다. 위의 내용은 영구 구성입니다.

import java.util.Properties; 
import javax.persistence.EntityManagerFactory; 
import javax.sql.DataSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.apache.commons.dbcp.BasicDataSource; 
import org.eclipse.persistence.config.PersistenceUnitProperties; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect; 
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@Configuration 
@EnableJpaRepositories(basePackages = { 
    "package.with.repositories1", 
    "package.with.repositories2" 
}) 
@EnableTransactionManagement 
public class PersistenceContext { 

    @Bean(destroyMethod = "close") 
    DataSource dataSource() { 
     BasicDataSource dataSource = new BasicDataSource(); 
     dataSource.setDriverClassName("org.postgresql.Driver"); 
     dataSource.setUrl("jdbc:postgresql://localhost:5432/dbName"); 
     dataSource.setUsername("usernameForDd"); 
     dataSource.setPassword("passwordForDd"); 
     return dataSource; 
    } 

    @Bean 
    EclipseLinkJpaVendorAdapter jpaVendorAdapter() { 
     EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter(); 
     jpaVendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.PostgreSQLPlatform"); 
     jpaVendorAdapter.setGenerateDdl(Boolean.FALSE); 
     jpaVendorAdapter.setShowSql(EnvironmentVariables.getInstance().getBoolean("app.showSql", Boolean.FALSE)); 

     return jpaVendorAdapter; 
    } 

    @Bean 
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { 
     LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactoryBean.setDataSource(dataSource); 
     entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter()); 
     entityManagerFactoryBean.setJpaDialect(new EclipseLinkJpaDialect()); 

     // Instead of persistence.xml 
     entityManagerFactoryBean.setPersistenceUnitName("yourPersistenceUnitName"); 
     entityManagerFactoryBean.setPackagesToScan(
       "package.with.entities1", 
       "package.with.entities2" 
     ); 

     Properties jpaProperties = new Properties(); 
     jpaProperties.put(PersistenceUnitProperties.WEAVING, "static"); 
     jpaProperties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "false"); 
     entityManagerFactoryBean.setJpaProperties(jpaProperties); 

     entityManagerFactoryBean.afterPropertiesSet(); 
     entityManagerFactoryBean.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver()); 

     return entityManagerFactoryBean; 
    } 

    @Bean 
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory); 
     return transactionManager; 
    } 
}