integrating JPA and SpringIOc

左心房为你撑大大i 提交于 2019-12-24 02:06:31

问题


Hi I write small application and trying connect jpa. in my spring configuration file I write this:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver.manager}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.login}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.epam.newsmanagement.entity</value>
        </list>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="ORACLE" />
        </bean>
    </property>
</bean>

<bean id="jpaDao" class="com.epam.newsmanagement.dao.JPANewsDao" />

and when I start my application browser giges me 404 error and console give such error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/jpa-configuration.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

please help)


回答1:


org.springframework.orm.jpa.LocalEntityManagerFactoryBean does not have a dataSource/ getDataSource() field/method defined. thats why you are getting that exception.

use LocalContainerEntityManagerFactoryBean instead

This may be helps you




回答2:


In integration of JPA and Spring, If you configure PersistenceUnit in LocalContainerEntityManagerFactoryBean. Make sure to use the same name of PersistenceUnit in persistence.xml and spring-bean.xml. If so, DataSource configuration of LocalEntityManagerFactoryBean may be optional. You might need to configure JpaTransactionManager.

(Assume : jpa-configuration.xml may be spring bean configuraion.)

Optional : org.springframework.orm.jpa.vendor.HibernateJpaDialect or com.company.util.HibernateExtendedJpaDialect is used for jpaDialect.

Example : spring-bean.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver.manager}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.login}" />
    <property name="password" value="${jdbc.password}" />
</bean>

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

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!--<property name="dataSource" ref="dataSource"/>-->
    <property name="persistenceUnitName" value="your_persistence_unit_name"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    </property>
    <!--<property name="jpaPropertyMap"></property>-->
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
        </bean>
    </property>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <!-- For MySQL-->
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
    <property name="generateDdl" value="false"/>
    <property name="showSql" value="true"/>
</bean>


来源:https://stackoverflow.com/questions/13357533/integrating-jpa-and-springioc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!