我正在尝试使用Hibernate的自动版本控制,但是当调用Session的更新方法f时,我没有在查询的where子句中看到version字段,也没有在数据库中增加版本。我可能做了一些根本错误的事情,但是什么呢?调用sesssionFactory的getCurrentSession是个问题吗?
我有以下实体类:
package dslibweb.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "dsXCT_Recalls")
public class DsXCT_Recalls {
@Id
public String recallId;
public int version;
public String status;
//...... more properties.....
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getRecallId() {
return recallId;
}
public void setRecallId(String recallId) {
this.recallId = recallId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
我的控制器:
package recalls.controller;
@Controller
public class RecallsDataController {
@Autowired
RecallsService recallsManager;
@Autowired
AuthenticationService authService;
private static final Logger logger = Logger.getLogger(RecallsDataController.class);
private static final String SAVE_RECALLS = "MODIFY XCT RECALLS";
RecallsGrid recalls;
@RequestMapping(value = "/showRecallsGrid")
@ResponseBody
public RecallsGrid showRecallsGrid( HttpSession session, HttpServletResponse response) {
recalls = recallsManager.getRecallsDataGrid((String) session.getAttribute("socketToken"), new GridFilters(0, 0, "", "", "", "", ""));
if (recalls.getError() == null || recalls.getError().equals("")) { // no error
return recalls;
} else {
try {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, recalls.getError());
} catch (IOException e) {
e.printStackTrace();
}
return recalls;
}
}
@RequestMapping(value = "/saveRecalls" , method= RequestMethod.POST)
@ResponseBody
public String saveRecalls( HttpSession session, @RequestParam(value="ids[]", required = false) String [] ids, @RequestParam(value="statuses[]", required = false) String [] statuses){
boolean result = authService.validateUserAction((String) session.getAttribute("socketToken"), SAVE_RECALLS);
if(result)
return recallsManager.saveRecalls(ids, statuses, recalls);
else
return "You do not have authority to perform this action.";
}
}
我在哪里检索DsXCT_Recalls的集合并将其显示给用户。该集合存储在控制器中。然后,用户在一个或多个实体中更改状态,并调用recallManager的saveRecalls方法,该方法仅创建已更改实体的列表(与存储在控制器中的集合进行比较)。
recallsManager(服务层)是:
package recalls.service.defaultimpl;
@Service("recallManager")
public class HibernateRecallsDataService implements RecallsService {
@Autowired
JsonRpcRequest jsonReq;
@Autowired
JsonRpcSocketWriterReader socketWriterReader;
@Autowired
JsonRpcRequestConstructor reqConstructor;
@Autowired
RecallsDao hibernateRecallsDao;
private static final Logger logger = Logger.getLogger(HibernateRecallsDataService.class);
@Transactional
public RecallsGrid getRecallsDataGrid(String socketToken, GridFilters filters) {
List<DsXCT_Recalls> recalls = hibernateRecallsDao.findRangeOfRecordsFiltered(filters);
return new RecallsGrid(recalls);
}
@Transactional()
public String saveRecalls(String[] ids, String[] statuses, RecallsGrid recalls) {
List<DsXCT_Recalls> recallList = recalls.getRecalls();
List<DsXCT_Recalls> updatedRecallList = new ArrayList<DsXCT_Recalls>();
for (int i = 0; i < ids.length; i++) {
for (DsXCT_Recalls recall : recallList) {
if (recall.recallId.equals(ids[i])) { // recall is found in the list
if (!statuses[i].equals(recall.getStatus())) { // status has changed
recall.setStatus(statuses[i]);
updatedRecallList.add(recall);
}
}
}
}
return hibernateRecallsDao.saveAll(updatedRecallList);
}
}
我的DAO的saveAll方法通过实体更改调用hibernate会话的一种更新方法:
package recalls.dao.hibernate;
@Repository
public class HibernateRecallsDao implements RecallsDao {
@Autowired(required = true)
@Resource(name = "mySessionFactory")
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public List<DsXCT_Recalls> findRangeOfRecordsFiltered(GridFilters filters) {
return sessionFactory.getCurrentSession().createQuery("from DsXCT_Recalls r WHERE SID = 0 ORDER BY Org, Bank, BIC, SetlDate").list();
}
public String saveAll(List<DsXCT_Recalls> recallList){
int count = 0;
for(DsXCT_Recalls recall:recallList){
sessionFactory.getCurrentSession().update(recall);
count++;
}
return count + " recalls were modified.";
}
}
答案 0 :(得分:1)
显然@Version必须高于属性声明而不是getter方法之上..我确信我在某处看到了这个。浪费了太多时间:(