我在Spring JPA集成方面遇到问题。我写了简单的应用程序,并有这样的实体元素

@Entity
@Table(name = "NEWS")
public class News{
    @Id
    @SequenceGenerator(sequenceName = "AUTO_INCREMENT", name = "sequence")
    @GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
    @Column(name = "ID", nullable = false, unique = true)
    private int id;
    @Column(name = "NEWSTITLE", nullable = false)
    private String newsTitle;
    @Column(name = "NEWSDATE", nullable = false)
    private Date newsDate;
    @Column(name = "BRIEF", nullable = false)
    private String brief;
    @Column(name = "CONTENT", nullable = false)
    private String content;
    private String date;


这是我的春季配置

    <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver.manager}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.login}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
   <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.epam.newsmanagement.entity"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="ORACLE" />
            </bean>
        </property>
    </bean>

    <bean id="jpaDao" class="com.epam.newsmanagementserver.dao.JPANewsDao">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>


在tomcat中部署时,出现此错误:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in Ser
vletContext resource [/WEB-INF/jpa-configuration.xml]: Error setting property values; nested exception is org.springframewor
k.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.jpa.LocalCont
ainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invalid setter method. Does the par
ameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.spri
ngframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Bean property 'packagesToScan' is not writable or has an invali
d setter method. Does the parameter type of the setter match the return type of the getter?
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:751)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:608)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowire
CapableBeanFactory.java:970)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapable
BeanFactory.java:729)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBe
anFactory.java:416)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.
java:141)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBean
Factory.java:287)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
        at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebAp
plicationContext.java:156)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)


展望互联网,我发现要解决此问题,我们需要使用Spring 3.1及更高版本。但是我使用的是春季版本3.2.3,并收到此错误。
请帮忙(

更新

@Avi发现Spring ORM 3.2.3版本没有方法setPackagesToScan。因此,我将版本更改为3.2.0,并在此版本中找到了这种方法。但是开始后我有同样的错误

最佳答案

似乎您正在尝试设置一个不存在的属性。类LocalContainerEntityManagerFactoryBean没有名为setPackagesToScan的setter,但是您尝试设置此类属性。

09-11 09:12