【问题标题】:EclipseLink & Spring Config without Persistence.xml没有 Persistence.xml 的 EclipseLink 和 Spring Config
【发布时间】:2015-03-24 04:56:57
【问题描述】:

我无法弄清楚如何在不使用 Persistence.xml 文件的情况下使用 Spring 配置 EclipseLink。我也想用 EclipseLink 配置静态编织以避免来自 Hibernate 的所有那些讨厌的 LazyLoadExceptions。

以下是我的 Hibernate & Spring 配置,运行良好。我想用 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>

【问题讨论】:

    标签: java spring jpa eclipselink


    【解决方案1】:

    我正在使用带有 PostgreSql dbms 的 EclipseLink 的 spring 数据。我的配置是基于注释的。很容易将其转换为 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;
        }
    }
    

    【讨论】:

    • 不知道为什么,但entityManagerFactoryBean.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver()) 给了我一个错误。注释掉它可以让我的应用程序启动。
    • SessionLog.ALL 抛出 ClassCastException 的任何原因?无论如何,即使使用FINEST_LABEL,我也没有看到任何 SQL 被记录。我也有jpaVendorAdapter.setShowSql(true)
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2016-06-04
    • 2011-06-18
    • 2011-10-22
    相关资源
    最近更新 更多