Nacho Zve De La Torre
Nacho Zve De La Torre

Reputation: 354

Hibernate not deleting object even though the debug is ok?

I'm trying to delete an Object using Hibernate but the thing is not deleting.

I debugged the program to make sure the Object is correct and it is, so I'm guessing the problem might be in something I have no idea what it is ... annotations, configuration ?? Maybe someone can help !

Here's the program:

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

As you can see in the picture the object is not null and it does matches the object I want to delete ...

So why is it not deleting ?

Upvotes: 0

Views: 199

Answers (1)

lance-java
lance-java

Reputation: 27958

I'm guessing you need to commit a transaction so that the delete actually gets committed to the database.

See Transaction

Eg:

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

Upvotes: 3

Related Questions