想了2天了,hibernate的更新问题,请高手指点迷津,紧急求助(为何我的更新操作会变成插入操作??????) http://www.ads4cn.com/newsbar/refferer.asp?elflovebobo>看新闻,赚现金!无须点击广告,不必投入资金! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这段是批量更新的sample:调用update方法就可以了,下面是update里使用到的queryActionByFind和updateAction方法,这样更新一个和多个都可以了,加分吧朋友!!!public void update(){ String[] columnNames=EmployeeDetailTable.getColumnNames(); Object[] values={"null","a00X","null","null","null","null","null","null","null","null"}; Hashtable msgDetails=new Hashtable(); for(int i=0;i<columnNames.length;i++){ msgDetails.put(columnNames[i],values[i]); } Iterator it=EmployeeDetailTable.queryActionByFind( "from EmployeeDetail e where e.employeeid='a000'"); EmployeeDetailTable.updateAction(it,msgDetails); }public static Iterator queryActionByFind(String HQL){ Session session=HibernateUtility.currentSession(); Iterator i=null; try { i = session.find(HQL).iterator(); } catch (HibernateException e) { e.printStackTrace(); } return i; }public static void updateAction(Iterator it,Hashtable msgDetails){ EmployeeDetail employeeDetail=null; Session session=HibernateUtility.currentSession(); String[] columnNames=getColumnNames(); while(it.hasNext()){ employeeDetail=(EmployeeDetail)it.next(); for(int i=0;i<columnNames.length;i++){ //Hashtable中不支持null,只能用"null"代替 if(!msgDetails.get(columnNames[i]).toString().equals("null")){ switch(i){ case 0: employeeDetail.setSysid((Integer)msgDetails.get(columnNames[i])); break; case 1: employeeDetail.setEmployeeid((String)msgDetails.get(columnNames[i])); break; case 2: employeeDetail.setName((String)msgDetails.get(columnNames[i])); break; case 3: employeeDetail.setGender((String)msgDetails.get(columnNames[i])); break; case 4: employeeDetail.setAge((Integer)msgDetails.get(columnNames[i])); break; case 5: employeeDetail.setAddress((String)msgDetails.get(columnNames[i])); break; case 6: employeeDetail.setZipcode((String)msgDetails.get(columnNames[i])); break; case 7: employeeDetail.setMobile((String)msgDetails.get(columnNames[i])); break; case 8: employeeDetail.setUnitphone((String)msgDetails.get(columnNames[i])); break; case 9: employeeDetail.setEmail((String)msgDetails.get(columnNames[i])); break; default: } } } } try { session.flush(); session.evict(employeeDetail); HibernateUtility.closeSession(); } catch (HibernateException e) { e.printStackTrace(); } } 补充一下上面的EmployeeDetailTable.getColumnNames()方法是从自己写的xml文件里得到employeedetail表的列名,省得自己一个个去写了,xml原型如下,当然你也可以用final static String[]定死<?xml version="1.0" encoding="UTF-8"?><Table name="EmployeeDetail"> <Column name="sysid" type="INT" isNull="NOT NULL"/> <Column name="employeeid" type="CHAR(20)" isNull="NOT NULL"/> <Column name="name" type="CHAR(20)" isNull="NOT NULL"/> <Column name="gender" type="CHAR(1)" isNull="NOT NULL"> <ValueOptional>M</ValueOptional> <ValueOptional>F</ValueOptional> </Column> <Column name="age" type="SMALLINT" isNull="NOT NULL"/> <Column name="address" type="CHAR(100)" isNull="NULL"/> <Column name="zipcode" type="CHAR(20)" isNull="NULL"/> <Column name="mobile" type="CHAR(20)" isNull="NULL"/> <Column name="unitphone" type="CHAR(20)" isNull="NULL"/> <Column name="email" type="CHAR(50)" isNull="NULL"/></Table> LDAP密码过期提示的实现 请问怎样在SSH中使用AJAX??? jsp请教 请教网页密码加密思路 key graph based bbs for online chance discovery 数据库中的特殊字符问题 求解一个jsp页面的烦恼 java 邮件发送的问题! 一个简单的问题!答对者马上给分 JSP全局变量的问题(about getServletContext()的使用) 群讨论区13474690(Programmer)-ASP,PHP,JSP,JAVA,C等技术都可以讨论哦!近来是请做个我简介 JSP 动态生成Excel 文件的问题
调用update方法就可以了,下面是update里使用到的queryActionByFind和updateAction方法,这样更新一个和多个都可以了,加分吧朋友!!!
public void update(){
String[] columnNames=EmployeeDetailTable.getColumnNames();
Object[] values={"null","a00X","null","null","null","null","null","null","null","null"};
Hashtable msgDetails=new Hashtable();
for(int i=0;i<columnNames.length;i++){
msgDetails.put(columnNames[i],values[i]);
}
Iterator it=EmployeeDetailTable.queryActionByFind(
"from EmployeeDetail e where e.employeeid='a000'");
EmployeeDetailTable.updateAction(it,msgDetails);
}public static Iterator queryActionByFind(String HQL){
Session session=HibernateUtility.currentSession();
Iterator i=null;
try {
i = session.find(HQL).iterator();
} catch (HibernateException e) {
e.printStackTrace();
}
return i;
}public static void updateAction(Iterator it,Hashtable msgDetails){
EmployeeDetail employeeDetail=null;
Session session=HibernateUtility.currentSession();
String[] columnNames=getColumnNames();
while(it.hasNext()){
employeeDetail=(EmployeeDetail)it.next();
for(int i=0;i<columnNames.length;i++){
//Hashtable中不支持null,只能用"null"代替
if(!msgDetails.get(columnNames[i]).toString().equals("null")){
switch(i){
case 0:
employeeDetail.setSysid((Integer)msgDetails.get(columnNames[i]));
break;
case 1:
employeeDetail.setEmployeeid((String)msgDetails.get(columnNames[i]));
break;
case 2:
employeeDetail.setName((String)msgDetails.get(columnNames[i]));
break;
case 3:
employeeDetail.setGender((String)msgDetails.get(columnNames[i]));
break;
case 4:
employeeDetail.setAge((Integer)msgDetails.get(columnNames[i]));
break;
case 5:
employeeDetail.setAddress((String)msgDetails.get(columnNames[i]));
break;
case 6:
employeeDetail.setZipcode((String)msgDetails.get(columnNames[i]));
break;
case 7:
employeeDetail.setMobile((String)msgDetails.get(columnNames[i]));
break;
case 8:
employeeDetail.setUnitphone((String)msgDetails.get(columnNames[i]));
break;
case 9:
employeeDetail.setEmail((String)msgDetails.get(columnNames[i]));
break;
default:
}
}
}
}
try {
session.flush();
session.evict(employeeDetail);
HibernateUtility.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Table name="EmployeeDetail">
<Column name="sysid" type="INT" isNull="NOT NULL"/>
<Column name="employeeid" type="CHAR(20)" isNull="NOT NULL"/>
<Column name="name" type="CHAR(20)" isNull="NOT NULL"/>
<Column name="gender" type="CHAR(1)" isNull="NOT NULL">
<ValueOptional>M</ValueOptional>
<ValueOptional>F</ValueOptional>
</Column>
<Column name="age" type="SMALLINT" isNull="NOT NULL"/>
<Column name="address" type="CHAR(100)" isNull="NULL"/>
<Column name="zipcode" type="CHAR(20)" isNull="NULL"/>
<Column name="mobile" type="CHAR(20)" isNull="NULL"/>
<Column name="unitphone" type="CHAR(20)" isNull="NULL"/>
<Column name="email" type="CHAR(50)" isNull="NULL"/>
</Table>