gpt4 book ai didi

java - Hibernate 自动版本控制不起作用(使用 Spring)

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:55 25 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 的自动版本控制,但是当调用 Session 的更新方法 f 时,我在查询的 where 子句中看不到版本字段,也没有在数据库中增加版本。我可能正在做一些根本性错误的事情,但是什么?调用 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;
}


}

我的 Controller :

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 集合并将其显示给用户。该集合存储在 Controller 中。然后,用户更改一个或多个实体中的状态,我调用recallManager 的 saveRecalls 方法,该方法创建仅包含已更改实体的列表(与存储在 Controller 中的集合相比)。

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 session 的一个更新方法:

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.";
}
}

最佳答案

所以显然 @Version 必须位于属性声明之上,而不是 getter 方法之上。我确信我在某处看到过这个。浪费了很多时间:(

关于java - Hibernate 自动版本控制不起作用(使用 Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com