hibernate不调用save也保存上了

 1 List<Instrument> insts = instService.search(search);
 2 
 3     if (insts.size() == 1) {
 4           Instrument inst = insts.get(0);
 5 
 6     if (inst != null) {
 7 
 8       inst.setBarCode("imported");
 9 
10       instService.save(inst)
11 
12   }
13 }

第10行加不加都会保存上。所有以下的方式应该避免

 1 List<Instrument> insts = instService.search(search);
 2 
 3     if (insts.size() == 1) {
 4           Instrument inst = insts.get(0);
 5 
 6     if (inst != null) {
 7 
 8       inst.setBarCode("imported");
 9 
10         if(cond) {
11           instService.save(inst)
12         }
13 
14   }
15 }

应该改为这样

 1 List<Instrument> insts = instService.search(search);
 2 
 3     if (insts.size() == 1) {
 4           Instrument inst = insts.get(0);
 5 
 6         
 7           if (inst != null) {
 8         if(cond) {
 9               inst.setBarCode("imported");
10 
11               instService.save(inst)
12         }
13     }
14 }
原文地址:https://www.cnblogs.com/cuizhf/p/4537925.html