2 Replies Latest reply on Dec 2, 2007 9:55 AM by Piergiuseppe Spinelli

    JPA/Hibernate Entity: broken identity between Entity & Objec

    Piergiuseppe Spinelli Newbie

      Since I've some trouble in extracting a significant frame of code, I'll try to put the question in general terms:

      During the same SEAM request to an action (an EJB method), I build a set of entities navigating relations from other entities returned by different queries, expecting the same entity can be navigated more than once.

      I expected that, in this case, the same Entity (same class & id) would be even the same Java Object instance (since all the navigations & queries were inside the same request): but in some case I HAVE TWO DIFFERENT OBJECT INSTANCES (both verified to be contained in the same EntityManager) FOR THE SAME ENTITY (same class & id).

      I thought it were against the JPA specification, can anyone confirm this?

      I am trying to play with the Hibernate properties & with the query hints about cache, but I have had no luck till now.

      Does anyone know if the are some specific situations where more objects can represent the same entity inside the same EntityManager and if it exists some method to avoid this?

      Thanks

        • 1. Re: JPA/Hibernate Entity: broken identity between Entity & O
          Andy Gibson Newbie

          While the entities are all within the same navigation and request, you may get different entities due to entity manager flushes when queries are run.

          When you execute a query, it can flush the entity manager therefore invalidating the entity manager entity cache.

          For example :

           MyClass a = entityManager.find(MyClass.class,id);
           MyList = entityManager.createQuery("...").getResultList();
           MyClass b = entityManager.find(MyClass.class,id);
          


          Here, I believe 'a' and 'b' would be different objects representing the same entity with the same id because the query caused a flush which invalidated the cache and caused the entity manager to create and load a new instance of the entity.

          What you could try, to diagnose the problem a bit is adding :

           entityManager.save(a);
           entityManager.save(b);
          


          If one of them is a stale detached object, then the entity manager will complain.



          • 2. Re: JPA/Hibernate Entity: broken identity between Entity & O
            Piergiuseppe Spinelli Newbie

            Ok Andy, first of all many thanks for your reply.

            What you wrote is very interesting, but I am not sure about a point: I understand I can get two objects for the same entity during the same request due to some implicit flush before a query (I think this behaviour is controlled by hints), but wouldn't the flushed objects have to return false at EntityManager.contains?

            The problem is that I got two "valid" objects for the same entity, that is both contained by the same EntityManager at the same moment.

            It seems I found a way to fix it by a better implementation of the methods: compareTo, hashCode and, overall, equals in all my entities, even those I have to navigate to get the duplicated instances: so my understanding (but I ask for confirmation), is that the identity within an EntityManager is kept by using equals and not (as I believed having used some JDO implementation) by using the pair class/id.

            I don't know if it depends on a specific implementation, but I think it should be well defined in the JPA standard (I know, this forum could not be the right place for the discussion), since it could have some side effects, or (at least) it would require a more detailed specification about the canonical ways to implements equality, hashing and ordering for the entities.