1 2 Previous Next 15 Replies Latest reply on Dec 30, 2005 3:19 PM by epbernard

    merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

      Hi,
      I have a strange behaviour (may be I am missing something)

      I have an object A that has a relation to B,
      I detach the A with lazy B,
      I merge the A and get the A.B .. I obtain an enhanced object,
      when I try to merge the A.B it crashes with:

      Caused by: org.hibernate.MappingException: Unknown entity: ..............$$EnhancerByCGLIB$$b1e5cf42
      10:50:27,467 ERROR [STDERR] at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:610)
      10:50:27,467 ERROR [STDERR] at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1249)
      10:50:27,467 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:130)
      10:50:27,467 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:108)
      10:50:27,467 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
      10:50:27,467 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:636)
      10:50:27,467 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:622)
      10:50:27,468 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:140)

      -------------
      thanks for help

        • 1. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

          I know I try to merge the same object twice.. (once when I merge the A the cascade persist will merge also the B, and also I call the merge secondary in some other method...) but this is permitted by the specification... it should do nothing second time

          • 2. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

            any help ??

            • 3. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )
              patrick_ibg

              What is your call to merge? Perhaps you are passing objectA.getClass() instead of A.class?

              • 4. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                no, the call to merge is
                a1 = manager.merge(a)

                where a is a detached instance of A, and a has a collection of B (lazy initialized)

                now the a1(that is an instance of A) contains the loaded relations...

                and after that somewhere after that call it is called the
                manager.merge(an instance of B from the a1 relations)
                (example manager.merge((B)a1.getBs().iterator().next())

                ... this last call merge crashes ... with that exception

                • 5. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                  it looks like the manager does not recognize the enhanced class :
                  MY_ENTITY_CLASS_NAME + $$EnhancerByCGLIB$$b1e5cf42

                  • 6. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )
                    patrick_ibg

                    Hrm. Have you tried explicitly getting the B before doing a merge? Something like:

                    B b = a.getBs().iterator().next() ;
                    B b2 = em.merge (b) ;

                    • 7. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                      the a is not merged ... so the lazy relation is not loaded...
                      the a.getBs will not work ...

                      if you mean the conversion ... it is only an example,
                      --------------------------------------
                      B b = a.getBs().iterator().next() ;
                      B b2 = em.merge (b) ;
                      --------------------------------------
                      IS the same thing with
                      --------------------------------------
                      em.merge((B)a.getBs().iterator().next())
                      --------------------------------------

                      • 8. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )
                        patrick_ibg

                         

                        --------------------------------------
                        B b = a.getBs().iterator().next() ;
                        B b2 = em.merge (b) ;
                        --------------------------------------
                        IS the same thing with
                        --------------------------------------
                        em.merge((B)a.getBs().iterator().next())
                        --------------------------------------
                        


                        Right, I did not see the explicit cast.

                        Anyway, from the looks of the error, what you're trying to merge is a proxy object... NOT an instance of your entity class B.

                        It's hard to fathom why this is happening w/o seeing your code or the mappings...


                        • 9. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                          it is not a proxy (the proxy names are : $Proxy...)
                          it is some enhanced object...

                          • 10. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                            I found this very strange thing ....

                            normally the merge operation traverse all the relations marked at CASCADE merge or all.
                            when I merge my object, the relation is not merged....
                            the related object instead to become from
                            b= B (id=249)
                            a= A$$EnhancerByCGLIB$$b0ad8802 (id=255)
                            id= 1
                            name= "b"
                            into
                            b1= B (id=275)
                            a= A (id=276)
                            id= 1
                            name= "b"
                            ---------------------
                            the a relation remains in A$$EnhancerByCGLIB$$
                            ... so it is not traversed by merge ....

                            does anybody know why ??

                            I am sure the annotation are correct (merge contained):
                            @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)

                            • 11. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                              is there any other condition that blocks the merge operation to merge the related objects ?

                              • 12. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                                also I found this in hibernate logs:

                                DEBUG [org.hibernate.engine.CascadingAction] cascading to merge: test.B
                                DEBUG [org.hibernate.event.def.DefaultMergeEventListener] ignoring uninitialized proxy
                                DEBUG [org.hibernate.event.def.DefaultLoadEventListener] loading entity: [test.B#2842]
                                DEBUG [org.hibernate.event.def.DefaultLoadEventListener] entity proxy found in session cache
                                DEBUG [org.hibernate.engine.Cascade] done processing cascade ACTION_MERGE for: test.A
                                DEBUG [org.hibernate.event.def.DefaultLoadEventListener] loading entity: [test.B#2842]
                                DEBUG [org.hibernate.event.def.DefaultLoadEventListener] entity proxy found in session cache

                                • 13. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )
                                  jwynett

                                  I was getting a very similar error when I tried to access objects in a relationship and got an instance with the "EnhancerByCGLIB"

                                  It seems like a very complex issue but appears to be related to whether the relationship is LAZY and whether you have to rely on the EntityManager to load the object for you. It also seems to be connected with crossing multiple session bean boundaries where you implicitly work with different entity managers.

                                  In my case, I had a session bean method that called other session beans to retrieve data and then later update it. Most of my relationships are loaded LAZILY however if I know I want it eager, I use INNER JOIN FETCH in my query. In this case, I loaded a particular entity, DeviceInterface, that was related to an entity, IpAddress, as an N-1. Since I didn't need the IpAddress at the time, I didn't explicitly load it. However, the EntityManager seems to have generated this EnhancerByCGLIB class for it. Later when I ran another query specifically to get this IpAddress, I would get the mapping exception when I tried to do an update. I also had this problem on another entity.

                                  I found three things that avoided the error:

                                  1. Remove the transaction on the outer session bean method. When not in the transaction, all the objects became detached and I always relied on a fresh entity manager.

                                  2. In my initial query, I would use INNER JOIN FETCH to eagerly load all the objects I would need for the entire transaction, even if I didn't need them right away.

                                  3. Make that IpAddress relationship EAGER.

                                  In any case, I got my code to work, however I'm not particularly satisfied with the solution because I would like to understand why this is happening and come up with a general coding strategy that will consistently avoid this problem without also making everything load eagerly. Otherwise, it's a real hit-or-miss situation where I just respond to the error whenever I see it. And I don't particularly like to be in that position.

                                  Jon

                                  • 14. Re: merge failed (Unknown entity: ... $$EnhancerByCGLIB$$ )

                                    you are perfectly right ... I reached the same conclusions.
                                    It seems it is a bug .... (or I am doing something wrong)
                                    the manager.merge should never leave a relation (that is uninitialized (lazy) and also marked as CASCADE.MERGE) uninitialized ...
                                    But because I could'nt replicate my error case in a simple example I did not post it.

                                    1 2 Previous Next