1

我在粘贴上一个线程时遇到问题。以下是我之前提出的问题:

package com.springhibernate.example;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class HibernateDaoImpl {
    @Autowired
    private SessionFactory sessionFactory;

    public int getCircleCount(){

        String hql = "Select count(*) from Circle";
        Query query = getSessionFactory().openSession().createQuery(hql);
        return (int) query.uniqueResult();

    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

我有以下错误:

Invalid property 'dataSource' of bean class [com.springhibernate.example.HibernateDaoImpl]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1385)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1107)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.springhibernate.example.SpringHibernateDemo.main(SpringHibernateDemo.java:13)
    Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [com.springhibernate.example.HibernateDaoImpl]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1057)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:920)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1382)
        ... 13 more

谢谢大家帮助....

4

3 回答 3

0

根据我的经验,这个错误可能是两个问题:

1)正如 Ale Zalazar 所说:xml 上没有数据源标签(写入器或读取器)

2)您必须在 Reader 或 writer 上实现(您需要数据源对象):

public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }

或类似的东西:

private HibernateOperations hibernateTemplate;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

取决于你如何实施。

希望能帮助到你!

于 2014-06-19T15:25:13.037 回答
0

你没有“数据源”HibernateDaoImpl 你应该使用这样的东西来让你的道工作。这是我为数据源和会话工厂设置的示例。并且要小心,因为那是区分大小写的:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url"
        value="jdbc:postgresql://localhost:9999/yourDBname" />
    <property name="username" value="userName" />
    <property name="password" value="passsssssword" />
</bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.people.net.model.PersonPersistenceModel</value>
            <value>com.people.net.model.EmailPersistenceModel</value>
            <value>com.people.net.model.AddressPersistenceModel</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="managerDAO" class="com.people.net.dao.ManagerDaoImpl">
    <property name="sFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>

“sFactory”是“sessionFactory”(必须是您在数据访问对象-DAO 中使用的 SessionFactory 的属性名称)

于 2017-04-16T07:33:27.263 回答
-1

您不得使用

<property name="dataSource" ref="dataSource"/>

在 applicationContext.xml 中 HibernateDaoImpl 的 bean 定义中。

如果你想这样做,你必须在 HibernateDaoImpl 中提供一个 setDataSource 方法和相应的属性 dataSource。

于 2012-07-17T03:00:59.557 回答