[英]Update a column using hibernate
I have two classes Employee and Department . 我有两类员工和部门 。
public class Employee {
int empId;
String empName;
Boolean isEmpAvailable;
String empAddress;
Department department;
}
public class Department {
int deptId;
String deptName;
}
I have created hibernate files files for both classes Department.hbm.xml and Employee.hbm.xml 我已经为Department.hbm.xml和Employee.hbm.xml两个类创建了休眠文件文件
I like to update the column isEmpAvailable in the table Employee basing on a deptid in Department table. 我想根据Department表中的部门更新Employee表中的isEmpAvailable列。
Here I am facing problem with update query which I am not clear after reading in online documentation 在这里,我面临更新查询的问题,在阅读在线文档后仍不清楚
public void updateEmployee(Employee emp, Department deptid){
String query= " update Employee set isEmpAvailable=? where deptid=?
Object[] values= {"true","133"};
getHibernateTemplate.update(query,values);
}
When i run the code the column doesn't get update. 当我运行代码时,列不会更新。 A error is thrown as Entity not recognized: update Employee set isEmpAvailable=? 由于无法识别实体而引发错误:update Employee set isEmpAvailable =? where deptid=? 哪里deptid =?
I have read online docs which have methods of getHibernateTemplate() which have return type as integer. 我已经阅读了在线文档,其中包含getHibernateTemplate()方法,这些方法的返回类型为整数。 Here I like to update the database directy by calling dao.updateEmployee without any returntype. 在这里,我想通过调用dao.updateEmployee而不使用任何returntype来直接更新数据库。 I am unable do it. 我做不到 Please suggest me 请建议我
Update in hibernate is done this way : 休眠中的更新是通过以下方式完成的:
String hqlUpdate =
"update Employee e " +
"set e.isEmpAvailable = :isEmpAvailable " +
"where e.deptid = :deptid";
int updatedEntities = session.createQuery( hqlUpdate )
.setBoolean( "isEmpAvailable", isEmpAvailable )
.setInt( "deptid", deptid )
.executeUpdate();
OR
String jpqlUpdate =
"update Employee e " +
"set e.isEmpAvailable = :isEmpAvailable " +
"where e.deptid = :deptid";
int updatedEntities = entityManager.createQuery( jpqlUpdate )
.setBoolean( "isEmpAvailable", isEmpAvailable )
.setInt( "deptid", deptid )
.executeUpdate();
OR
String hqlVersionedUpdate =
"update versioned Employee e " +
"set e.isEmpAvailable = :isEmpAvailable " +
"where e.deptid = :deptid";
int updatedEntities = s.createQuery( hqlUpdate )
.setBoolean( "isEmpAvailable", isEmpAvailable )
.setInt( "deptid", deptid )
.executeUpdate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.