JPA commit() row skiped after persist()
I'm try to add a new entity on my database, but everytime I try the commit row is skiped after the persist().
public T salvar(T t) {
EntityManager em = daoHelper.getEM();
try {
em.getTransaction().begin();
if (t.getId() == null) {
em.persist(t);
em.getTransaction().commit();
}
} catch (Exception e) {
} finally {
em.close();
}
return t;
}
I don't know what is wrong, because the object t is complete.
Edit 1: These are my entity atributes
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Column(name = "idItem")
private Integer idItem;
@Column(name = "pedidoData")
@Temporal(TemporalType.DATE)
private Date pedidoData;
@Size(max = 9)
@Column(name = "pedidoEnderecoCep")
private String pedidoEnderecoCep;
@Column(name = "pedidoEnderecoNumero")
private Integer pedidoEnderecoNumero;
@Size(max = 85)
@Column(name = "pedidoEnderecoBairro")
private String pedidoEnderecoBairro;
And this is the object that I'm trying to add:
java jpa
add a comment |
I'm try to add a new entity on my database, but everytime I try the commit row is skiped after the persist().
public T salvar(T t) {
EntityManager em = daoHelper.getEM();
try {
em.getTransaction().begin();
if (t.getId() == null) {
em.persist(t);
em.getTransaction().commit();
}
} catch (Exception e) {
} finally {
em.close();
}
return t;
}
I don't know what is wrong, because the object t is complete.
Edit 1: These are my entity atributes
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Column(name = "idItem")
private Integer idItem;
@Column(name = "pedidoData")
@Temporal(TemporalType.DATE)
private Date pedidoData;
@Size(max = 9)
@Column(name = "pedidoEnderecoCep")
private String pedidoEnderecoCep;
@Column(name = "pedidoEnderecoNumero")
private Integer pedidoEnderecoNumero;
@Size(max = 85)
@Column(name = "pedidoEnderecoBairro")
private String pedidoEnderecoBairro;
And this is the object that I'm trying to add:
java jpa
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
If commit is "skipped" then aThrowable
is thrown. End of. So debug why an exception is thrown. Basic java.
– Billy Frost
Nov 22 '18 at 10:50
add a comment |
I'm try to add a new entity on my database, but everytime I try the commit row is skiped after the persist().
public T salvar(T t) {
EntityManager em = daoHelper.getEM();
try {
em.getTransaction().begin();
if (t.getId() == null) {
em.persist(t);
em.getTransaction().commit();
}
} catch (Exception e) {
} finally {
em.close();
}
return t;
}
I don't know what is wrong, because the object t is complete.
Edit 1: These are my entity atributes
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Column(name = "idItem")
private Integer idItem;
@Column(name = "pedidoData")
@Temporal(TemporalType.DATE)
private Date pedidoData;
@Size(max = 9)
@Column(name = "pedidoEnderecoCep")
private String pedidoEnderecoCep;
@Column(name = "pedidoEnderecoNumero")
private Integer pedidoEnderecoNumero;
@Size(max = 85)
@Column(name = "pedidoEnderecoBairro")
private String pedidoEnderecoBairro;
And this is the object that I'm trying to add:
java jpa
I'm try to add a new entity on my database, but everytime I try the commit row is skiped after the persist().
public T salvar(T t) {
EntityManager em = daoHelper.getEM();
try {
em.getTransaction().begin();
if (t.getId() == null) {
em.persist(t);
em.getTransaction().commit();
}
} catch (Exception e) {
} finally {
em.close();
}
return t;
}
I don't know what is wrong, because the object t is complete.
Edit 1: These are my entity atributes
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Column(name = "idItem")
private Integer idItem;
@Column(name = "pedidoData")
@Temporal(TemporalType.DATE)
private Date pedidoData;
@Size(max = 9)
@Column(name = "pedidoEnderecoCep")
private String pedidoEnderecoCep;
@Column(name = "pedidoEnderecoNumero")
private Integer pedidoEnderecoNumero;
@Size(max = 85)
@Column(name = "pedidoEnderecoBairro")
private String pedidoEnderecoBairro;
And this is the object that I'm trying to add:
java jpa
java jpa
edited Nov 22 '18 at 4:24
Leandro Souza
asked Nov 22 '18 at 4:07
Leandro SouzaLeandro Souza
165
165
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
If commit is "skipped" then aThrowable
is thrown. End of. So debug why an exception is thrown. Basic java.
– Billy Frost
Nov 22 '18 at 10:50
add a comment |
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
If commit is "skipped" then aThrowable
is thrown. End of. So debug why an exception is thrown. Basic java.
– Billy Frost
Nov 22 '18 at 10:50
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
If commit is "skipped" then a
Throwable
is thrown. End of. So debug why an exception is thrown. Basic java.– Billy Frost
Nov 22 '18 at 10:50
If commit is "skipped" then a
Throwable
is thrown. End of. So debug why an exception is thrown. Basic java.– Billy Frost
Nov 22 '18 at 10:50
add a comment |
1 Answer
1
active
oldest
votes
On a higher level, it seems the issue is because of the place you have kept your commit statement. You have kept it inside the if condition, so the commit happens only when the if condition is satisfied. For instance, in the sample input data shared by you id is not null, therefore, if condition is not satisfied, thus, commit won't take place, on the contrary if you will send entity with null id, commit will take place. You should move the commit statement outside the if block as:
if (t.getId() == null) {
em.persist(t);
}
em.getTransaction().commit();
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
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%2f53423750%2fjpa-commit-row-skiped-after-persist%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
On a higher level, it seems the issue is because of the place you have kept your commit statement. You have kept it inside the if condition, so the commit happens only when the if condition is satisfied. For instance, in the sample input data shared by you id is not null, therefore, if condition is not satisfied, thus, commit won't take place, on the contrary if you will send entity with null id, commit will take place. You should move the commit statement outside the if block as:
if (t.getId() == null) {
em.persist(t);
}
em.getTransaction().commit();
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
add a comment |
On a higher level, it seems the issue is because of the place you have kept your commit statement. You have kept it inside the if condition, so the commit happens only when the if condition is satisfied. For instance, in the sample input data shared by you id is not null, therefore, if condition is not satisfied, thus, commit won't take place, on the contrary if you will send entity with null id, commit will take place. You should move the commit statement outside the if block as:
if (t.getId() == null) {
em.persist(t);
}
em.getTransaction().commit();
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
add a comment |
On a higher level, it seems the issue is because of the place you have kept your commit statement. You have kept it inside the if condition, so the commit happens only when the if condition is satisfied. For instance, in the sample input data shared by you id is not null, therefore, if condition is not satisfied, thus, commit won't take place, on the contrary if you will send entity with null id, commit will take place. You should move the commit statement outside the if block as:
if (t.getId() == null) {
em.persist(t);
}
em.getTransaction().commit();
On a higher level, it seems the issue is because of the place you have kept your commit statement. You have kept it inside the if condition, so the commit happens only when the if condition is satisfied. For instance, in the sample input data shared by you id is not null, therefore, if condition is not satisfied, thus, commit won't take place, on the contrary if you will send entity with null id, commit will take place. You should move the commit statement outside the if block as:
if (t.getId() == null) {
em.persist(t);
}
em.getTransaction().commit();
answered Nov 22 '18 at 4:32
codeLovercodeLover
2,2591520
2,2591520
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
add a comment |
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
The commit() row still been skipped
– Leandro Souza
Nov 22 '18 at 4:38
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
Can you explain what exactly you mean by " commit() row still been skipped "
– codeLover
Nov 22 '18 at 4:41
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
When I get to persist() row and click to move to the next row it goes directly to em.close() on debug mode. It's like em.getTransaction().commit(); doesn't exist.
– Leandro Souza
Nov 22 '18 at 4:52
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
you mean a Throwable is thrown. So DEBUG YOUR CODE and look at what Throwable is thrown!
– Billy Frost
Nov 22 '18 at 10:02
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%2f53423750%2fjpa-commit-row-skiped-after-persist%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
It only happens with one entity. I just tested and the same code worked with another entity
– Leandro Souza
Nov 22 '18 at 4:10
Can you share the sequence of inputs you are sending to this method?
– codeLover
Nov 22 '18 at 4:15
@codeLover Sure. I edited the question with more information
– Leandro Souza
Nov 22 '18 at 4:25
If commit is "skipped" then a
Throwable
is thrown. End of. So debug why an exception is thrown. Basic java.– Billy Frost
Nov 22 '18 at 10:50