【问题标题】:Bean property 'entityManager' is not writable or has an invalid setter method (using Spring and Hibernate)Bean 属性 'entityManager' 不可写或具有无效的 setter 方法(使用 Spring 和 Hibernate)
【发布时间】:2016-05-19 13:10:39
【问题描述】:

Spring 和 hibernate 对我来说都是新工具,同时使用这两种工具我有点吃力。

spring.xml:

<bean id="my.supervisionFramesProcess" class="supervision.frames.SupervisionFramesProcess"
    init-method="startup" destroy-method="shutdown">
    <property name="SupervisionDAO">
        <bean class="supervision.dao.jpa.JpaSupervisionDao">
            <property name="entityManager" ref="my.entity-manager-factory"     />
        </bean>
    </property>
</bean>

<bean id="my.dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://XXXX/supervision?autoReconnect=true" />
    <property name="username" value="***" />
    <property name="password" value="***" />
    <property name="maxIdle" value="5" />
    <property name="maxActive" value="100" />
    <property name="maxWait" value="30000" />
    <property name="testOnBorrow" value="true" />
    <property name="validationQuery" value="SELECT 1" />
</bean>

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

<bean id="my.entity-manager-factory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
    <property name="dataSource" ref="my.dataSource" />
    <property name="persistenceUnitName" value="SUPERVISION-1.0" />
</bean>

JpaSupervisionDao.xml:

@PersistenceContext(unitName = "SUPERVISION-1.0")
private EntityManager entityManager;

public JpaSupervisionDao() {
    if (logger.isDebugEnabled())
        logger.debug("New instance DAO : " + this);
}
protected void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}
protected EntityManager getEntityManager() {
    return entityManager;
}

@Override
public SupervisionDbObject selectSupervisionDbObject(SupervisionDbObject supervision) {
    Query query = getEntityManager().createQuery(SELECT_SUPERVISION);
}

persistence.xml:

<persistence-unit name="SUPERVISION-1.0">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>supervision.dao.SupervisionDbObject</class>
</persistence-unit>

使用 JDBC,我的 DataSource 可以被实例化并且完全可以工作,但是使用 Hibernate 和 entityManager,我只得到一个错误:

Bean property 'entityManager' is not writable or has an invalid setter method.

我尝试改用 EntityManagerFactory 对象,但发生了同样的错误。

有人可以帮帮我吗?

【问题讨论】:

  • 您的setEntityManagerprotected。但是,您应该删除它并让@PersistenceContext 完成它的工作。确保您的配置中有&lt;context:annotation-config /&gt;
  • 如果我已经有一个像 PersistenceAnnotationBeanPostProcessor 这样的 bean,我真的需要 吗? (在操作中添加)
  • 不,你没有,但是&lt;context:annotation-config/&gt;PersistenceAnnotationBeanPostProcessor 做得更多(比如@Autowired)。

标签: spring hibernate jpa entitymanager


【解决方案1】:

感谢 M. Deinum 的回答,添加 &lt;context:annotation-config/&gt; 解决了我的问题。

如果这可以帮助其他人,我也遇到了导入问题,我正在导入 org.hibernate.annotations.Entity 而不是 javax.persistence.Entity

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 2015-09-07
    • 1970-01-01
    • 2023-03-25
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多