2015-06-21 58 views
1

我知道这个问题已被询问很多次。但我找不到我的解决方案。Bean属性xxxDAO不可写或具有无效的setter方法

我正在使用spring进行休眠。 用户和UserWord之间有一个连接作为一对多映射 我已经为其他模型完成了相同的逻辑,并且一切正常。 但是当我做了一个映射,我得到了错误。

我spring.xml

 <?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:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

     <tx:annotation-driven transaction-manager="txManager"/> 

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/springdb" /> 
     <property name="username" value="xxx" /> 
     <property name="password" value="xxx" /> 
     </bean> 

     <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
      <property name="dataSource" ref="dataSource" /> 
      <property name="mappingResources"> 
       <list> 
        <value>com/memorize/dao/mapping/Users.hbm.xml</value> 
        <value>com/memorize/dao/mapping/Word.hbm.xml</value> 
        <value>com/memorize/dao/mapping/UserWord.hbm.xml</value> 
       </list> 
      </property> 
      <property name="hibernateProperties"> 
       <props> 
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
        <prop key="hibernate.show_sql">true</prop> 
        <prop key="hibernate.hbm2ddl.auto">update</prop> 
       </props> 
      </property> 
     </bean> 

     <!-- <bean id="genericDAO" class="com.memorize.dao.impl.GenericDAOImpl"> 
      <constructor-arg> 
       <value>com.memorize.model.Users</value> 
      </constructor-arg> 
     </bean>--> 

     <bean id="usersDAO" class="com.memorize.dao.impl.UsersDAOImpl"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
     </bean> 

     <bean id="userService" class="com.memorize.service.impl.UsersServiceImpl"> 
      <property name="usersDAO" ref="usersDAO"></property> 
     </bean> 

     <bean id="wordDAO" class="com.memorize.dao.impl.WordDAOImpl"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
     </bean> 

     <bean id="wordService" class="com.memorize.service.impl.WordServiceImpl"> 
      <property name="wordDAO" ref="wordDAO"></property> 
     </bean> 

     <bean id="userWordDAO" class="com.memorize.dao.impl.UserWordDAOImpl"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
     </bean> 

     <bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl"> 
      <property name="userWordDAO" ref="userWordDAO"></property> 
     </bean> 

     <bean id="txManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
     </bean> 
    </beans> 

UserWord.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?> 
     <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

     <hibernate-mapping> 
      <class name="com.memorize.model.UserWord" table="userwords" catalog="springdb"> 
       <id name="userWordId" type="java.lang.Integer"> 
        <column name="ID" /> 
        <generator class="identity" /> 
       </id> 
       <many-to-one name="user" class="com.memorize.model.Users" fetch="select"> 
        <column name="USER_ID" not-null="true"></column> 
       </many-to-one> 
      </class> 
     </hibernate-mapping> 

UserWordServiceImpl.java

 package com.memorize.service.impl; 

     import org.springframework.transaction.annotation.Transactional; 

     import com.memorize.dao.UserWordDAO; 
     import com.memorize.model.UserWord; 
     import com.memorize.service.UserWordService; 

     public class UserWordServiceImpl implements UserWordService{ 

      private UserWordDAO userWordDao; 

      public void setUserWordDao(UserWordDAO userWordDao) { 
       this.userWordDao = userWordDao; 
      } 

      @Override 
      @Transactional 
      public void saveOrUpdateUserWord(UserWord uw) { 
       // TODO Auto-generated method stub 
       this.userWordDao.saveOrUpdate(uw); 
      } 
     } 

UserWordDAOImpl.java

 package com.memorize.dao.impl; 

     import org.hibernate.Session; 
     import org.hibernate.SessionFactory; 
     import org.springframework.beans.factory.annotation.Autowired; 

     import com.memorize.dao.UserWordDAO; 
     import com.memorize.model.UserWord; 

     public class UserWordDAOImpl implements UserWordDAO{ 

      @Autowired 
      private SessionFactory sessionFactory; 

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

      @Override 
      public void saveOrUpdate(UserWord uw) { 
       // TODO Auto-generated method stub 
       Session session = this.sessionFactory.getCurrentSession(); 
       session.persist(uw); 
      } 
     } 

Users.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?> 
     <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

     <hibernate-mapping> 
      <class name="com.memorize.model.Users" table="users" catalog="springdb"> 
       <id name="userId" type="java.lang.Integer"> 
        <column name="ID" /> 
        <generator class="identity"/> 
       </id> 
       <property name="userName" type="string"> 
        <column name="USER_NAME" not-null="true" unique="true" /> 
       </property> 

       <set name="userWords" table="userwords" fetch="select"> 
        <key> 
         <column name="ID" not-null="true"></column> 
        </key> 
        <one-to-many class="com.memorize.model.UserWord"/> 
       </set> 
      </class> 
     </hibernate-mapping> 

这是错误:

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userWordService' defined in class path resource [spring/config/spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userWordDAO' of bean class [com.memorize.service.impl.UserWordServiceImpl]: Bean property 'userWordDAO' is not writable or has an invalid setter method. Did you mean 'userWordDao'? 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1493) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
     at com.memorize.common.Test.main(Test.java:18) 
     Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userWordDAO' of bean class [com.memorize.service.impl.UserWordServiceImpl]: Bean property 'userWordDAO' is not writable or has an invalid setter method. Did you mean 'userWordDao'? 
     at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064) 
     at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:922) 
     at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82) 
     at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489) 
     ... 13 more 

    Thanks for helping 

回答

1

你的属性名称是UserWordServiceImpluserWordDao,和你想设置属性userWordDAO(不匹配的情况下),这是不那里 。因此,在spring.xml变化

<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl"> 
      <property name="userWordDAO" ref="userWordDAO"></property> 
     </bean> 

<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl"> 
      <property name="userWordDao" ref="userWordDAO"></property> 
     </bean> 
+0

感谢。我对此做了一些研究,我猜想这完全是关于二传手注射的 –

相关问题