我正在尝试使用Hibernate删除对象,但是事情并没有删除。
我调试了程序以确保Object是正确的,所以我猜测问题可能出在我不知道它是什么的地方……注解,配置?也许有人可以帮忙!
这是程序:
控制器:
// 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";
}
}
您可以在图片中看到该对象不为null,并且与我要删除的对象匹配...
那为什么不删除呢?
答案 0 :(得分:3)
我猜您需要提交一个事务,以便删除实际上可以提交到数据库。
请参见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();
}