[求助]使用hibernate编写一个复制函数的问题
数据库里有几十条数据时,或不停的复制下去,也就是获取信息,一边保存,大量重复的信息,怎么回事啊?/**
* @Description @TODO 获取信息-复制
*/
@Override
public Map<String, Object> copyLjbInfo(String scbh,String fzscbh) {
Session session = this.getSession();
Map<String, Object> map = new HashMap<String, Object>();
Query query = session.createQuery("from ScLjbBO where scbh=:scId");
query.setString("scId", fzscbh);
@SuppressWarnings("unchecked")
List<ScLjbBO> ljbList = (List<ScLjbBO>) query.list();
session.close();
if (null == ljbList || ljbList.size() == 0) {
map.put("success", false);
map.put("result", "获取信息失败");
return map;
}else{
for(int i=0;i<ljbList.size();i++){
ljbList.get(i).setScbh(scbh);
sclrLjb(ljbList);
}}
map.put("success", true);
map.put("ljbList", ljbList);
return map;
}
/**
* @Description @TODO 新增的料件表保存
* @param
* @return
* @throws
*
* @Changed By
* @reason
* @Changed date
*/
@Override
public String sclrLjb(List<ScLjbBO> ljbList) {
Session session = getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
for (int i = 0; i < ljbList.size(); i++) {
session.save(ljbList.get(i));
if (i % 10 == 0) {
session.flush();
session.clear();
}
}
tx.commit();
session.close();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
session.flush();
session.clear();
session.close();
}
}
return null;
} --------------------编程问答-------------------- session.save(ljbList.get(i));你这里一直在save。。
补充:Java , Java SE