3 Replies Latest reply on Nov 5, 2008 3:14 PM by Guillaume Jeudy

    Entity.forClassand Hibernate mapping files.

    Flavio Costa Newbie

      Entity.forClass() / forBean() is unable to detect entities deployed through Hibernate mapping, which is bad, since most people using Seam use Hibernate as the persistence provider.


      Is there any reason for not handling properly Hibernate mapping files?

        • 1. Re: Entity.forClassand Hibernate mapping files.
          Guillaume Jeudy Master

          Can you explain where you are having issues specifically? 


          I use Seam 2.0.1.GA successfully with hibernate XML mapping files, I noticed that s:convertEntity doesn't work but in my case I had to create my own custom generic converter that is based on a custom Identifiable interface with only 1 generic method on it.


          T getId() which matches the hibernate mapped id property.

          • 2. Re: Entity.forClassand Hibernate mapping files.
            Flavio Costa Newbie

            Well, to be honest, s:convertEntity work just fine here, even using HBM's.


            I'm currently trying to implement some audit to my application, and in order to do that, I'm using Hibernate's Interceptors.


            Let's say a Have 3 entities:


            Student
            - id
            - name
            - school_id
            
            School
            - id
            - address
            
            Audit
            - id
            - oldValue (string)
            - newValue (string)
            



            Whenever I call student.setSchool() and flush the EntityManager, the Hibernate's Interceptor tells the application that student's school was oldValue and is now newValue. So far so good.


            The thing is that oldValue and newValue are both instances of School and I'm not interested in serialazing the both instances and saving them in the database as Blobs. Instead I want to retrieve the IDs of both oldValue and newValue and save them.


            I tried using Entity.forClass(oldValue.getClass()).getIdentifier(oldValue) but that didn't work, since Seam was not aware that this class was indeed an Entity.


            I try my best not to use proprietary code (use as much as possible the JavaEE spec), so everytime I can use a simple EntityManager I do so, so I'm currently using a ManagedPersistenceContext instead of a ManagedHibernateSession (which has a getIdentifier() method if i'm not mistaken). In order to keep using JPA's EntityManager and still being able to retrieve the ID of a given entity I had to use the EntityManager.getDelegate() method in order to obtain an instance of Hibernate's Session, and call Session.getIdentifier() to obtain the ID. This kinda works, but requires some class castings and ugly code, I think using Entity.forClass().getIdentifier() would be much nicer.

            • 3. Re: Entity.forClassand Hibernate mapping files.
              Guillaume Jeudy Master

              I don't know, maybe you want to trace how s:convertEntity works with HBM mapped entities. Maybe some other Seam framework code can get you to get the ID without using hibernate proprietary API.