EntityManager和persist方法无法正常工作

时间:2013-01-17 12:46:26

标签: java spring hibernate entitymanager

我在这里遇到麻烦.. 我是Java SE应用程序中使用Spring + Hibernate的新手......

我正在尝试实例化entityManager,但它无法正常工作

我正在使用注释@PersistenceUnit,如下所示:

@PersistenceUnit
public void setEmf(EntityManager emf) {
    this.emf = emf;
}

它工作“很好”,但它不会持续= / 当我改为

@PersistenceContext
public void setEmf(EntityManager emf) {
    this.emf = emf;
}

出现以下错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoAbstract' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy10 implementing org.hibernate.ejb.HibernateEntityManagerFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo' to required type 'javax.persistence.EntityManager' for property 'emf'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy10 implementing org.hibernate.ejb.HibernateEntityManagerFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo] to required type [javax.persistence.EntityManager] for property 'emf': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)

这是我的applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="ConfiguradorDePropriedades"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <description>The service properties file</description>
        <property name="location" value="file:AppConfig.properties" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!-- <property name="persistenceUnitName" value="ChatJpa" /> -->
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="dataSource" ref="dataSourceLocal" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="${database}" />
                <property name="showSql" value="false" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>
    </bean>

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

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

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

    <bean id="daoAbstract" class="com.planner.pvc.dao.DaoAbstract">
        <property name="emf" ref="entityManagerFactory" />
    </bean>

    <bean id="clienteDao" class="com.planner.pvc.dao.ClienteDaoImpl">
    </bean>

    <bean id="pvcMainController" class="com.planner.pvc.controller.PVCMainController">
        <property name="dao" ref="clienteDao" />
    </bean>



</beans>

和persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    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">
    <persistence-unit name="ChatJpa">
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <!-- <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />  -->
        </properties>
    </persistence-unit>
</persistence>

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:3)

这一行是问题所在:

<bean id="daoAbstract" class="com.planner.pvc.dao.DaoAbstract">
    <property name="emf" ref="entityManagerFactory" />
</bean>

不要这样做,让Spring注入用@PersistenceContext注释的emf字段,如果你的应用程序上下文中有<context:component-scan.../><context:annotation-config/>标记,或者只是添加后者,就会发生这种情况