Search code examples
hibernatespring-mvcjavabeansdaohibernate-annotations

org.springframework.beans.NotWritablePropertyException. Does the parameter type of the setter match the return type of the getter?


This is the bean created in my applicationContext.xml file.

    <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/employeelist" />
            <property name="username" value="root" />
            <property name="password" value= xxxxxx />
      </bean>

      <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

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

            <property name="hibernateProperties">
              <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
              </props>
            </property>
            <property name="annotatedClasses">
            <list>
                  <value>com.aspire.model.Employee</value>
            </list>
            </property>
      </bean>
      <bean id ="registrationService" class = "com.aspire.service.RegisterServiceImpl">
      <property name="registerDAO" ref="registerDAO"></property>
      <!-- <property name ="sessionFactory" ref = "sessionFactory"></property> -->
      </bean>
<bean id="registerDAO" class ="com.aspire.dao.RegisterDAOImpl">
      <property name ="sessionFactory" ref = "sessionFactory"></property>
      </bean>
</beans>

This is the RegisterService interface along with its implementation class file.

public interface RegisterService {
    public void checkRegister(String name,String email,Date dob,String address,String office,String serviceLine,String username,String passwd) throws SQLException,IOException, IllegalStateException, SystemException;
}


    @Service("registrationService")
    public class RegisterServiceImpl implements RegisterService{

        @Autowired
        private RegisterDAO register;

        public RegisterServiceImpl(){
        }

        public RegisterServiceImpl(RegisterDAO register){
            this.register = register;
        }

        public void checkRegister(String name, String email, Date dob, String   address, String office, String serviceLine,
                String username, String passwd) throws SQLException, IOException, IllegalStateException, SystemException {
            System.out.println("In Service class...Check Register");
        register.checkRegister(name, email, dob, address, office, serviceLine, username, passwd);
    }
}

And this is the RegisterDAOImpl.java file.

@Repository("registerDAO")
public class RegisterDAOImpl implements RegisterDAO{

       @Resource(name="sessionFactory")
       protected SessionFactory sessionFactory;


       protected Session session;

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

       public SessionFactory getSessionFactory(){
           return sessionFactory;
       }

       protected Session getSession(){
              return sessionFactory.openSession();
       }


    public void checkRegister(String name, String email, Date dob, String address, String office, String serviceLine,
            String username, String passwd) throws SQLException,IOException, IllegalStateException, SystemException
    {
        System.out.println("In Check Register");
        Integer emp_id = null;
        try
        {
            session = getSession();
            /*String SQL_QUERY = "INSERT INTO employee(emp_id,name,dob,address,office,sline,username,password,email)"+
                           "s";
            Query query = session.createQuery(SQL_QUERY);
            int result = query.executeUpdate();*/
            org.hibernate.Transaction tx1=session.beginTransaction();
            Employee emp = new Employee(name,dob,address,office,serviceLine,username,passwd,email);
            emp.setEmp_id(111);
            emp.setAddress("Chennai");
            emp.setDob(dob);
            emp.setEmail(email);
            emp.setEmp_name("emp_name");
            emp.setEmp_pwd("emp_pwd");
            emp.setOffice_loc("office_loc");
            emp.setServiceLine(serviceLine);
            emp.setUsername("username");
            emp_id = (Integer)session.save(emp);
            System.out.println(emp_id);
            tx1.commit();

        }catch(HibernateException e)
        {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        finally
        {
            session.close();
        }

    }
}

This is the error message that I am receiving when the bean is created. Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'registerDAO' of bean class [com.aspire.service.RegisterServiceImpl]: Bean property 'registerDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Thank you in advance. Having a hard time fixing the issue.


Solution

  • <bean id ="registrationService" class = "com.aspire.service.RegisterServiceImpl">
    <property name="registerDAO" ref="registerDAO"></property>
    <!-- <property name ="sessionFactory" ref = "sessionFactory"></property> -->
    </bean>
    

    This is setter dependency injection, you must have a setter method in your class RegisterServiceImpl. That is what the error says.

    Add the method in your service class like this:

    public void setRegisterDAO(RegisterDAO registerDAO) {
        this.registerDAO = registerDAO;
    }
    

    As said by Deinum, you already have annotations, so you can remove the xml configuration beans for registrationService & registerDAO and use the component-scan like this:

    <context:component-scan base-package="your-package" />
    

    Refer to this example to know how to do that.