【问题标题】:IllegalStateException: Specified field type EntityManager is incompatible with resource type EntityManagerFactoryIllegalStateException:指定的字段类型 EntityManager 与资源类型 EntityManagerFactory 不兼容
【发布时间】:2015-11-28 08:57:19
【问题描述】:

当我尝试将 entitymanager 自动装配到我的 DAO 类时遇到问题。我想我已经用@PersistenceContext 正确配置了它,但错误仍然存​​在。

提前致谢。

我的 DAO

@Repository
public class UserDAOImpl implements UserDAO{

    @Autowired
    @PersistenceContext
    private EntityManager entityManager;

    public void setEntityManager(EntityManager entityManager) {
       this.entityManager = entityManager;
    }

     ...
}

我的persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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" version="1.0">

    <persistence-unit name="unit">
       <properties>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
       </properties>
   </persistence-unit>

</persistence>

我的 hibernateContext.xml

<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:p="http://www.springframework.org/schema/p"
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.5.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- Auto-detect DAOs -->
  <context:component-scan base-package="com.plainart.dao"/>



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


   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" 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" />


</beans>

【问题讨论】:

  • 你的例外是什么?
  • 谢谢。问题解决了。 @MedhiRedjem

标签: hibernate jakarta-ee entitymanager


【解决方案1】:

删除你不需要的 entityManager 声明上方的 @Autowired 注释。

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2017-12-22
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多