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";
}
}
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 ?
java spring hibernate spring-mvc
add a comment |
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";
}
}
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 ?
java spring hibernate spring-mvc
1
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
This is the code in my DAO for deleting ... is it ok ?public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer
– Nacho Zve De La Torre
Nov 28 '18 at 19:20
add a comment |
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";
}
}
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 ?
java spring hibernate spring-mvc
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";
}
}
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 ?
java spring hibernate spring-mvc
java spring hibernate spring-mvc
asked Nov 28 '18 at 18:56
Nacho Zve De La TorreNacho Zve De La Torre
18710
18710
1
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
This is the code in my DAO for deleting ... is it ok ?public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer
– Nacho Zve De La Torre
Nov 28 '18 at 19:20
add a comment |
1
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
This is the code in my DAO for deleting ... is it ok ?public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer
– Nacho Zve De La Torre
Nov 28 '18 at 19:20
1
1
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
This is the code in my DAO for deleting ... is it ok ?
public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
This is the code in my DAO for deleting ... is it ok ?
public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:
Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer– Nacho Zve De La Torre
Nov 28 '18 at 19:20
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:
Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer– Nacho Zve De La Torre
Nov 28 '18 at 19:20
add a comment |
1 Answer
1
active
oldest
votes
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();
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526291%2fhibernate-not-deleting-object-even-though-the-debug-is-ok%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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();
}
add a comment |
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();
}
add a comment |
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();
}
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();
}
edited Nov 28 '18 at 20:08
answered Nov 28 '18 at 20:03
lance-javalance-java
16.9k12963
16.9k12963
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526291%2fhibernate-not-deleting-object-even-though-the-debug-is-ok%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Perhaps you need to commit a transaction?
– lance-java
Nov 28 '18 at 19:02
This is the code in my DAO for deleting ... is it ok ?
public void delete(Credential credential) { Session session = sessionFactory.openSession(); session.delete(credential); session.close(); }
– Nacho Zve De La Torre
Nov 28 '18 at 19:11
Well ... it was that !! THANKS brother ... I googled for transaction and I added these lines to the code:
Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(credential); session.getTransaction().commit(); session.close();
If you want to post that as an answer and explain a little I will accept your answer– Nacho Zve De La Torre
Nov 28 '18 at 19:20