首页 > 在向Dao层注入entity时候总是报错,加入需要注入的属性的get方法后解决了,这是因为什么原因?

在向Dao层注入entity时候总是报错,加入需要注入的属性的get方法后解决了,这是因为什么原因?

1.在向Dao层注入entity类时候总是报错:Error creating bean with name 'todayAttendanceDao' defined in class path resource [springxml/yhspring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'todayAttendance' of bean class [com.chinasofti.dao.impl.yh.TodayAttendanceDaoImpl]: Bean property 'todayAttendance' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
2.经过检查没有发现entity层和dao层有什么错误,在Dao层加入属性的get方法后,不再报错。
3.spring.xml :

<bean id="todayAttendanceDao" class="com.chinasofti.dao.impl.yh.TodayAttendanceDaoImpl">
    <property name="todayAttendance" ref="todayAttendance"></property>
</bean>

对应Dao层 :

Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();

private TodayAttendance todayAttendance = null;
    public void setTodayAttendance(TodayAttendance todayAttendance) {
        this.todayAttendance = todayAttendance;
    }
public TodayAttendance getTodayAttendance() {
    return todayAttendance;
}//加入这个get方法后就不报错了?为什么?是因为我dao层里面有方法的互调吗?
public void update(TodayAttendance currentAttendance) {
        //如果有记录则更新,没有则创建
        todayAttendance = getTodayAttendance(currentAttendance.getUid());
        if(todayAttendance != null){
            //有记录,更新
            todayAttendance.setLate(todayAttendance.getLate()+currentAttendance.getLate());
            todayAttendance.setAbsenteeism(todayAttendance.getAbsenteeism()+currentAttendance.getAbsenteeism());
            todayAttendance.setWorkover(todayAttendance.getWorkover()+currentAttendance.getWorkover());
            session.update(todayAttendance);
        }else{
            //没记录,插入
            session.save(currentAttendance);
        }
    }
    
    
    
    public TodayAttendance getTodayAttendance(int uId) {
        //通过uId得到TodayAttendance
        String hql = "from TodayAttendance where uId = "+uId;
        Query query = session.createQuery(hql);
        List list = query.list();
        if(list.size() > 0){
            todayAttendance = (TodayAttendance)list.get(0);
            System.out.println(todayAttendance.getTid());
            return todayAttendance;
        }else{
            return null;            
        }


    }

    public void clear() {
        //得到todayAttendance表所有数据
        String hql = "from TodayAttendance";
        Query query = session.createQuery(hql);
        List list = query.list();
        for(int i = 0;i < list.size();i++){
            TodayAttendance attendanceToDelete = (TodayAttendance)list.get(i);
            System.out.println("111");
            session.delete(attendanceToDelete);
            System.out.println("222");
        }
        
    }
4.不知道为什么就不报错了,也不知道为什么需要些get方法,折腾半天了,是跟我update方法里面调用getTodayAttendance方法有关吗?

框架又不是你写的,不是你觉着只需要set方法就行的。。。

知道javaBean规范么,人家是按照规范来。


应该是你使用的时候报的错把,光是注入不是set就好了吗?你是启动就报错?


你的entity类就是必须同时有get和set方法,或者把属性声明成public,这是hibernate的规定,否则它没法做属性与数据库字段的互相映射。因为它急需要get到值往数据库里写,同时又要从数据库读值调set写进对象里

【热门文章】
【热门文章】