-
修改全部字段。
在建立实体类的基础上,我们只需简单的调用如下update()方法就可实现对数据库对应表的修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
void
update(Object obj)
throws
HibernateException{
Session session=HibernateSessionFactory.getSession();
Transaction tran=
null
;
try
{
tran=session.beginTransaction();
session.update(obj);
tran.commit();
}
catch
(HibernateException e){
if
(tran!=
null
)tran.rollback();
throw
e;
}
finally
{
session.close();
}
}
|
1
2
3
4
5
6
|
public
boolean
buidingupdate(
int
buildingId,StringbuildingName,String propertyAdress){
Object[] o={ buildingName,propertyAdress,buildingId};
String hql=
"update Building set propertyAdress=?,buildingName=? where buildingId = ?"
;
hds.batchUpdate(hql,o);
return
true
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
int
batchUpdate(String hql,Object[] params){
int
ret=
0
;
Session session=HibernateSessionFactory.getSession();
Transaction t=
null
;;
try
{
Query q=session.createQuery(hql);
t=session.beginTransaction();
if
(params!=
null
){
for
(
int
i=
0
;i<params.length;i++){
q.setParameter(i, params[i]);
}
}
ret=q.executeUpdate();
t.commit();
}
catch
(HibernateException e){
if
(t!=
null
) t.rollback();
throw
e;
}
finally
{
session.close();
}
return
ret;
}
}
|