5 Replies Latest reply on Aug 6, 2009 8:15 PM by dhinojosa

    ManyToOne Unidirectional relationship and merge vs. flush

    dhinojosa

      Do you find that when you use flush with an entity that has a @ManyToOne unidirectional relationship with another entity you end up with javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: <bean-name of entity that the entity you are flushing is related to>?



      Some other references to this problem:





      If you do, do you choose to merge(), or do you choose to change your relationships?

        • 1. Re: ManyToOne Unidirectional relationship and merge vs. flush
          asookazian

          Do you have cascade enabled?  AFAIK if you are using SMPC then the EntityManager remains open until the LRC ends (which means you need to find out why your entity is being detached from the PC and thus no longer managed by the EntityManager).


          The nabble link has examples with cascade settings.  Perhaps that's involved in the root cause?


          And how does it behave if you have a @ManyToOne bidirectional relationship with or w/o cascading enabled?

          • 2. Re: ManyToOne Unidirectional relationship and merge vs. flush
            lvdberg

            Daniel,


            It depends a bit how you've defined an entity and their relation. I tend to annotate a Many-to-One relation between two Entities in such a way that I need to persist the other entity first prior to persisting the many-side. I always load the One-side prior to persisting the Many-side, so preventing any detached-problem. This is mostly the case in Entity to ValueObjects relations; so the entity should always DB-exist. Also in Entity-to-Entity I use the same approach, cascading is really nice but I tend to use it only for Owned Collections (composite aggegrations).


            Leo


            P.S. This is really a Hibernate-question and a fundamental domain-modelling discussion and should be asked in their forum.


            • 3. Re: ManyToOne Unidirectional relationship and merge vs. flush
              dhinojosa

              Except the problem in my case is the one relationship item is already persisted and managed by entitymanager.

              • 4. Re: ManyToOne Unidirectional relationship and merge vs. flush
                dhinojosa

                Arbi Sookazian wrote on Aug 06, 2009 01:32:


                Do you have cascade enabled?  AFAIK if you are using SMPC then the EntityManager remains open until the LRC ends (which means you need to find out why your entity is being detached from the PC and thus no longer managed by the EntityManager).


                I have verified that they are attached and I do have cascade enabled.



                The nabble link has examples with cascade settings.  Perhaps that's involved in the root cause?


                I just used that as an example, as to the problem I was having.



                And how does it behave if you have a @ManyToOne bidirectional relationship with or w/o cascading enabled?


                I'll find out.  Although I don't want bidirection since that would just make my User bean a mess. ;)

                • 5. Re: ManyToOne Unidirectional relationship and merge vs. flush
                  dhinojosa

                  Corrected, In a reference of a reference of reference I used an entity loaded from the session entity manager, instead of LRC entity manager.  Thanks everyone.