EntityManager和persist方法无法正常工作 [英] EntityManager and persist method not working properly

查看:104
本文介绍了EntityManager和persist方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm in trouble here.. I'm new using Spring + Hibernate in a Java SE application...

我正在尝试实例化entityManager,但是它不起作用

I'm trying to instantiate the entityManager, but it's not working

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

I'm using the annotation @PersistenceUnit, like this:

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

它可以正常"运行,但不会持久=/ 当我更改为

And it works "fine", but it doesn't persist =/ When I change to

@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

Here's my 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

And 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>

任何帮助将不胜感激.

谢谢

推荐答案

这是问题所在:

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

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

Instead of doing this, let Spring inject the emf field annotated with @PersistenceContext, this will happen if you have a <context:component-scan.../> or <context:annotation-config/> tags in your application context or just add the latter.

这篇关于EntityManager和persist方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆