Why is data in table updating to null due to the following Java code?
up vote
0
down vote
favorite
The following method is supposed to update a row in a table by updating an object.
public void saveOrUpdate(final T data) throws CPDPersistenceException {
final EntityManager em = getEntityManager();
try {
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
em.merge(data);
transaction.commit();
}
} catch (final PersistenceException e) {
throw new CPDPersistenceException(e);
}
The updated object "data" passed to saveorUpdate() has the new data in it. After the transaction.commit(), though, data is updated in the table but there is one element of the Object - The ID - that is still OK in the table I am updating, but in another table it has been changed to null. Does anybody know how this might happen? Thanks for your time.
java hibernate jpa
add a comment |
up vote
0
down vote
favorite
The following method is supposed to update a row in a table by updating an object.
public void saveOrUpdate(final T data) throws CPDPersistenceException {
final EntityManager em = getEntityManager();
try {
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
em.merge(data);
transaction.commit();
}
} catch (final PersistenceException e) {
throw new CPDPersistenceException(e);
}
The updated object "data" passed to saveorUpdate() has the new data in it. After the transaction.commit(), though, data is updated in the table but there is one element of the Object - The ID - that is still OK in the table I am updating, but in another table it has been changed to null. Does anybody know how this might happen? Thanks for your time.
java hibernate jpa
1
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in yourData
and in this mystical _another table. Answering should not be any guessing game.
– pirho
Nov 10 at 10:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The following method is supposed to update a row in a table by updating an object.
public void saveOrUpdate(final T data) throws CPDPersistenceException {
final EntityManager em = getEntityManager();
try {
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
em.merge(data);
transaction.commit();
}
} catch (final PersistenceException e) {
throw new CPDPersistenceException(e);
}
The updated object "data" passed to saveorUpdate() has the new data in it. After the transaction.commit(), though, data is updated in the table but there is one element of the Object - The ID - that is still OK in the table I am updating, but in another table it has been changed to null. Does anybody know how this might happen? Thanks for your time.
java hibernate jpa
The following method is supposed to update a row in a table by updating an object.
public void saveOrUpdate(final T data) throws CPDPersistenceException {
final EntityManager em = getEntityManager();
try {
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
em.merge(data);
transaction.commit();
}
} catch (final PersistenceException e) {
throw new CPDPersistenceException(e);
}
The updated object "data" passed to saveorUpdate() has the new data in it. After the transaction.commit(), though, data is updated in the table but there is one element of the Object - The ID - that is still OK in the table I am updating, but in another table it has been changed to null. Does anybody know how this might happen? Thanks for your time.
java hibernate jpa
java hibernate jpa
asked Nov 9 at 21:04
calvinnme
113
113
1
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in yourData
and in this mystical _another table. Answering should not be any guessing game.
– pirho
Nov 10 at 10:41
add a comment |
1
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in yourData
and in this mystical _another table. Answering should not be any guessing game.
– pirho
Nov 10 at 10:41
1
1
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in your
Data
and in this mystical _another table. Answering should not be any guessing game.– pirho
Nov 10 at 10:41
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in your
Data
and in this mystical _another table. Answering should not be any guessing game.– pirho
Nov 10 at 10:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.
add a comment |
up vote
0
down vote
If you do not want some other table to be updated by this merge you need to only use the "Cascade" that you need but not the Cascade.MERGE for that relation on your data class. Check this link for some more information about how cascading works with JPA and Hibernate. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
If still you do not want to have that column updated to null (I guess that's the foreign key), then you need to make sure that the child data is also on the data element when you call this method.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.
add a comment |
up vote
0
down vote
Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.
add a comment |
up vote
0
down vote
up vote
0
down vote
Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.
Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.
answered Nov 10 at 7:17
Ajish Thankachan
612
612
add a comment |
add a comment |
up vote
0
down vote
If you do not want some other table to be updated by this merge you need to only use the "Cascade" that you need but not the Cascade.MERGE for that relation on your data class. Check this link for some more information about how cascading works with JPA and Hibernate. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
If still you do not want to have that column updated to null (I guess that's the foreign key), then you need to make sure that the child data is also on the data element when you call this method.
add a comment |
up vote
0
down vote
If you do not want some other table to be updated by this merge you need to only use the "Cascade" that you need but not the Cascade.MERGE for that relation on your data class. Check this link for some more information about how cascading works with JPA and Hibernate. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
If still you do not want to have that column updated to null (I guess that's the foreign key), then you need to make sure that the child data is also on the data element when you call this method.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you do not want some other table to be updated by this merge you need to only use the "Cascade" that you need but not the Cascade.MERGE for that relation on your data class. Check this link for some more information about how cascading works with JPA and Hibernate. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
If still you do not want to have that column updated to null (I guess that's the foreign key), then you need to make sure that the child data is also on the data element when you call this method.
If you do not want some other table to be updated by this merge you need to only use the "Cascade" that you need but not the Cascade.MERGE for that relation on your data class. Check this link for some more information about how cascading works with JPA and Hibernate. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
If still you do not want to have that column updated to null (I guess that's the foreign key), then you need to make sure that the child data is also on the data element when you call this method.
edited Nov 10 at 8:17
answered Nov 10 at 8:10
Jorge C
309210
309210
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53233250%2fwhy-is-data-in-table-updating-to-null-due-to-the-following-java-code%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
Can you show the mapping of the class that you pass as data?
– Simon Martinelli
Nov 10 at 1:42
I think I know what is your problem and might have an answer. BUT it is impossible to write an answer because you have not provided any code that shows what is in your
Data
and in this mystical _another table. Answering should not be any guessing game.– pirho
Nov 10 at 10:41