6 Replies Latest reply on Dec 14, 2007 1:20 PM by vanyatka

    Fundamental question about em.persist

    vanyatka

      Hi,

      Is there any functional difference between these two snippets (note the order of the last two statements)? When I look at the DB tables in the first case the name of the Card is null. Why so? I was dead sure any changes made to persistent object are flushed at the end of the transaction, so those two examples should give the same result. Everything is as expected in the second, the name of the Card is set properly.

      Can anyone comment on this?
      Thanks,

      @Name("initDBBean")
      @Scope(ScopeType.APPLICATION)
      public class InitDBBean {
       @Create
       @Transactional
       public void init() {
       Card card = new Card();
       entityManager.persist(card);
       card.setName("cardname");
       }
       }
      }
      


      @Name("initDBBean")
      @Scope(ScopeType.APPLICATION)
      public class InitDBBean {
       @Create
       @Transactional
       public void init() {
       Card card = new Card();
       card.setName("cardname");
       entityManager.persist(card);
       }
       }
      }
      


        • 1. Re: Fundamental question about em.persist
          vanyatka

          I have to come back to this issue once again. There must be some easy explanation.

          The following code produces "Test Card" in the DB. Why the last change doesn't get persisted? It is a part of the transaction, after all.

          @Create
           @Transactional
           public void init() {
           Card card = new Card();
           card.setName("Test Card");
           entityManager.persist(card);
           card.setName("The final name");
           }
          


          • 2. Re: Fundamental question about em.persist
            junkie

            I always managed to avoid entity updates after a persist in the same method call... however what happens if you add

            card = entityManager.merge(card);

            directly after the persist call?

            • 3. Re: Fundamental question about em.persist
              vanyatka

               

              "JUnkie" wrote:
              I always managed to avoid entity updates after a persist in the same method call...


              May I ask why? By itself persist() doesn't necessarily triggers DB call, just introduces the object to the persistence context. If I make an update later, should be fine, the important thing is that all changes are flushed before the transaction commits.


              however what happens if you add
              card = entityManager.merge(card);
              directly after the persist call?


              Nothing changes. The same "old" record in the DB.

              Now again, after persist() finishes the object is already in the persistent context, unless it is wrapped in some sort of dynamic proxy object which is later saved into the DB instead of the original object.

              However I don't recall anything anywhere that would mention calling persist() the last method in your transaction, so to avoid changes between persist() and transaction commit.



              • 4. Re: Fundamental question about em.persist
                vanyatka

                Black magic, it works now, even without merge!!!! :)

                matt.drees gave me a hint how to @Startup application scoped components properly and it works now
                http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4112676#4112676

                The only change I made is that now the component starts up with @Startup, and when I reported I referenced it from some page so it would start up.

                So far I don't get what happened.

                • 5. Re: Fundamental question about em.persist
                  pmuir

                  What sort of persistence context are you using?

                  • 6. Re: Fundamental question about em.persist
                    vanyatka

                     

                    "pete.muir@jboss.org" wrote:
                    What sort of persistence context are you using?


                    Standard out-of-the-box conversation-scoped SMPC injected with @In.