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.










share|improve this question

















  • 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















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.










share|improve this question

















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 your Data and in this mystical _another table. Answering should not be any guessing game.
    – pirho
    Nov 10 at 10:41













  • 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








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













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.






share|improve this answer



























    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.






    share|improve this answer






















      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',
      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
      );



      );













       

      draft saved


      draft discarded


















      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

























      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.






      share|improve this answer
























        up vote
        0
        down vote













        Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.






        share|improve this answer






















          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.






          share|improve this answer












          Add updatable=false in @joincolumn of your child.So it will not update your child when parent table updates.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 7:17









          Ajish Thankachan

          612




          612






















              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.






              share|improve this answer


























                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.






                share|improve this answer
























                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 8:17

























                  answered Nov 10 at 8:10









                  Jorge C

                  309210




                  309210



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20