spring - 收到错误 bean 类 [..] 的无效属性 'packagesToScan'。 Bean 属性 'packagesToScan' 不可写或具有无效的 setter 方法

标签 spring spring-mvc spring-data-jpa

我收到以下错误

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?\

我使用的是spring3.2。使用 MAven 添加依赖项。一切看起来都很好。为什么我收到此错误。这是我的文件

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                    http://www.springframework.org/schema/data/jpa 
                    http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

....
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>        
    <property name="packagesToScan" value="pk.training.basitMahmood.domain"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>              
        </props>        
    </property>
</bean>    

<context:annotation-config/> 

.....

</beans>

谢谢

最佳答案

不知道您的packageToScan? 希望您使用 @Table、@Entity、@column ... 等注释 您不需要扫描任何包裹。 当您尝试删除实体时,hibernate 将读取注释 我的答案是删除该行

更新 config.dao.xml

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml" />
    <property name="dataSource" ref="dataSource" />

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
        </props>
    </property>
</bean>

persitence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

    </persistence-unit>

</persistence>

关于spring - 收到错误 bean 类 [..] 的无效属性 'packagesToScan'。 Bean 属性 'packagesToScan' 不可写或具有无效的 setter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16776531/

相关文章:

java - spring中form-login标签的作用是什么

java - 在 spring 数据 jpa 查询中默认一个值?

java - 了解 Spring JPA 底层原生查询

hibernate - JpaRepository native 查询未检测参数

java - Spring Mvc Java 无法在名称为 'home' 的 servlet 中解析名称为 'dispatcher' 的 View

java - Redisson Spring - 处理命令时出现意外异常

java - 自定义 Spring Scope 不适用于消息 channel

spring - GCM XMPP - 启动 Web 应用程序服务器时出错

java - 配置文件不存在时的默认 Spring bean

java - Spring Security getPrincipal() 方法返回 anonymousUser