异常

本文介绍了一个关于部门和员工实体间关系映射错误的问题,并提供了正确的配置方法。错误发生在Hibernate尝试设置部门属性时,由于类型不匹配引发异常。文章详细说明了如何修正XML映射文件中的关系定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Type Exception Report
Message IllegalArgumentException occurred while calling setter for property [com.lys.entity.Employee.department (expected type = com.lys.entity.Department)]; target = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}], property value = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}] setter of com.lys.entity.Employee.department; nested exception is IllegalArgumentException occurred while calling setter for property [com.lys.entity.Employee.department (expected type = com.lys.entity.Department)]; target = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}], property value = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}]


Description The server encountered an unexpected condition that prevented it from fulfilling the request.


Exception


org.springframework.orm.hibernate5.HibernateSystemException: IllegalArgumentException occurred while calling setter for property [com.lys.entity.Employee.department (expected type = com.lys.entity.Department)]; target = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}], property value = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}] setter of com.lys.entity.Employee.department; nested exception is IllegalArgumentException occurred while calling setter for property [com.lys.entity.Employee.department (expected type = com.lys.entity.Department)]; target = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}], property value = [EmployeeDaoImpl{id=1, sex='女', birthday=null, joinDay=null, password='123456', eno='null', userName='李'}]
org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:219)
org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:344)
org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
org.springframework.orm.hibernate5.HibernateTemplate.find(HibernateTemplate.java:863)
com.lys.dao.impl.EmployeeDaoImpl.getEmployee(EmployeeDaoImpl.java:19)

com.lys.service.impl.EmployeeServiceImpl.login(EmployeeServiceImpl.java:18)


非法数据异常:

映射文件出错,在Employee和Department中,Department和Employee是“一对多的关系”,在实体的两个映射文件中,

Employee.hbm.xml中(“多”的一方)

<many-to-one name="department"
             class="com.lys.entity.Employee"
             column="departmentId" lazy="false">
</many-to-one>

Department.hbm.xml中(“一”的一方)

<set name="employeeSet">
    <key column="departmentId"></key>
    <one-to-many class="com.lys.entity.Employee"></one-to-many>
</set>
正确的是:

Employee.hbm.xml中(“多”的一方)

<many-to-one name="department"
             class="com.lys.entity.Dempartment"
             column="departmentId" lazy="false">
</many-to-one>

Department.hbm.xml中(“一”的一方)

<set name="employeeSet">
    <key column="departmentId"></key>
    <one-to-many class="com.lys.entity.Employee"></one-to-many>
</set>

colume:外键

class:对方实体类的相对路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值