gpt4 book ai didi

java - 即使调试正常,Hibernate 也不删除对象?

转载 作者:行者123 更新时间:2023-12-01 19:45:35 25 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 删除一个对象,但没有删除。

我调试了程序以确保对象正确并且确实如此,所以我猜测问题可能出在我不知道它是什么的地方......注释,配置?也许有人可以帮忙!

这是程序:

Controller :

// Erased the imports to make it simpler

@RestController
public class Controlador {

@Autowired
private FisicHostDao fisicHostDao;
@Autowired
private CredentialService credentialService;

@RequestMapping(value = "/fisicHost/{id}/credentials", method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE)
public List<Credential> credentialsByFisicHost(@PathVariable(value = "id") final Long fisicHostId, ModelMap modelMap){
FisicHost optionalFisicHost = fisicHostDao.findById(fisicHostId);

if (optionalFisicHost == null) {
// Responder con 404
}
FisicHost fisicHost = optionalFisicHost;
return fisicHost.getCredentials();
}

// This is the method handling the request / response

@RequestMapping(value = "/fisicHost/{id}/credentials", method = RequestMethod.POST)
public String deleteCredential(@PathVariable(value = "id") String credId){
String[] parts = credId.split("-");
int id = Integer.parseInt(parts[1]);
Credential c = credentialService.getCredentialById(id);
credentialService.delete(c);
return "justreturnsomething";
}
}

enter image description here

正如您在图片中看到的,该对象不为空,并且它确实与我要删除的对象匹配......

那为什么不删除呢?

最佳答案

我猜您需要提交一个事务,以便删除实际上被提交到数据库。

参见Transaction

例如:

Session session = sessionFactory.openSession();
try {
session.beginTransaction();
try {
doHibernateStuff(session);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
throw e;
}
} finally {
session.close();
}

关于java - 即使调试正常,Hibernate 也不删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526291/

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