在向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 setTodayAttendanceTodayAttendance todayAttendance {
        this.todayAttendance = todayAttendance;
    }
public TodayAttendance getTodayAttendance {
    return todayAttendance;
}//加入这个get方法后就不报错了?为什么?是因为我dao层里面有方法的互调吗?
public void updateTodayAttendance currentAttendance {
        //如果有记录则更新,没有则创建
        todayAttendance = getTodayAttendancecurrentAttendance.getUid;
        iftodayAttendance != null{
            //有记录,更新
            todayAttendance.setLatetodayAttendance.getLate+currentAttendance.getLate;
            todayAttendance.setAbsenteeismtodayAttendance.getAbsenteeism+currentAttendance.getAbsenteeism;
            todayAttendance.setWorkovertodayAttendance.getWorkover+currentAttendance.getWorkover;
            session.updatetodayAttendance;
        }else{
            //没记录,插入
            session.savecurrentAttendance;
        }
    }
    
    
    
    public TodayAttendance getTodayAttendanceint uId {
        //通过uId得到TodayAttendance
        String hql = "from TodayAttendance where uId = "+uId;
        Query query = session.createQueryhql;
        List list = query.list;
        iflist.size > 0{
            todayAttendance = TodayAttendancelist.get0;
            System.out.printlntodayAttendance.getTid;
            return todayAttendance;
        }else{
            return null;            
        }


    }

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

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

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

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

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

广告位

澳门银河官网