集成JPA和SpringIOc

时间:2012-11-13 08:28:09

标签: java spring jpa-2.0

您好我写小应用程序并尝试连接jpa。 在我的spring配置文件中,我写了这个:

<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" />

当我启动我的应用程序浏览器时,我会发现404错误,而控制台会出现这样的错误

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?

请帮忙)

2 个答案:

答案 0 :(得分:3)

org.springframework.orm.jpa.LocalEntityManagerFactoryBean没有定义dataSource / getDataSource()字段/方法。这就是为什么你得到那个例外。

使用LocalContainerEntityManagerFactoryBean代替

This可能会帮助您

答案 1 :(得分:2)

JPASpring的集成中,如果您在PersistenceUnit中配置LocalContainerEntityManagerFactoryBean。请务必在PersistenceUnitpersistence.xml中使用相同名称spring-bean.xml。如果是这样,DataSource的{​​{1}}配置可能是可选的。您可能需要配置LocalEntityManagerFactoryBean

(假设:JpaTransactionManager可能是spring bean配置。)

可选: jpa-configuration.xmlorg.springframework.orm.jpa.vendor.HibernateJpaDialect用于com.company.util.HibernateExtendedJpaDialect

示例:spring-bean.xml

jpaDialect