Reputation: 953
I know this question has been asked many times. But I could not find my solution.
I am using spring with hibernate. There is a connection between User and UserWord as one to many mapping I have done same logic for other models and everything went OK. However when I made a mapping, I got the error.
my 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>
This is error:
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
Upvotes: 1
Views: 5478
Reputation: 2417
your property name is userWordDao
in class UserWordServiceImpl
, and you are trying to set property userWordDAO
(case mismatch) which is not there . So in spring.xml change
<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl">
<property name="userWordDAO" ref="userWordDAO"></property>
</bean>
to
<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl">
<property name="userWordDao" ref="userWordDAO"></property>
</bean>
Upvotes: 1