3 Replies Latest reply on Nov 19, 2006 10:33 AM by fhh

    @GeneratedValue in mappedSuperclass Problem

    hoeft

      Hi!

      Many of my entities have an id of type long. So I put the code (getters, setters ...) into an superclass. After I did it, the id wasn't initialized by hibernate any more, if I persisted an entity. Thats very strange.

      Here is the code:

      public class GenerateValueTestcase extends TestCase {
      
       @MappedSuperclass
       public static abstract class EntityWithIntId {
      
       private long id = -1;
      
       @Id
       @GeneratedValue(strategy = GenerationType.AUTO)
       public long getId() {
       return id;
       }
      
       public void setId(long id) {
       this.id = id;
       }
       }
      
       @Entity
       public static class TestEntity extends EntityWithIntId implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       }
      
       public void testGeneratedIds() {
      
       TestStarterRemote bean = UsefullFunctions.getTestStarterRemote();
      
       Set<Long> ids = new HashSet<Long>();
      
       final int NO_ENTITIES = 10;
      
       for (int i = 0; i < NO_ENTITIES; i++) {
       EntityWithIntId entity = new TestEntity();
       entity = bean.merge(entity);
       ids.add(entity.getId());
       }
      
       assertEquals("Each entity must have an unique id", NO_ENTITIES, ids
       .size());
      
       }
      
      }
      


      The bean.merge method:
       public <T> T merge(T o) {
       return entityManager.merge(o);
       }
      


      Any ideas or workarounds? Copying the code into each class is not the best idea I think....

      Thanks for your help
      Meinert

        • 1. Re: @GeneratedValue in mappedSuperclass Problem
          jc7442

          Use persist instead of merge. In your case, it looks to be a new instance not a detached one.

          I use MappedSuperClass in my app and it works fine.

          • 2. Re: @GeneratedValue in mappedSuperclass Problem
            hoeft

            Thank you for your answer, I'll try it.

            But: I defined the getId and setId in the TestEntity class in my current code. The id is generated if I call the merge method. And: If I look into the documentation I see that the merge method should work properly for my purposes. But I'll try your suggestion. Maybe it' s hibernate bug and the stuff works only with persist in MappedSupperclasses :-(.

            I use JBOSS 4.0.5GA.

            Best regards
            Meinert

            • 3. Re: @GeneratedValue in mappedSuperclass Problem

              Hi!

              I had the same problem. I do not have a solution but a work-around is to overwrite the getId()-method and put the @Id- and @Generated-annotation there:

              @Entity
              public static class TestEntity extends EntityWithIntId implements Serializable
              
               @Id
               @GeneratedValue
               getId() {
              
               return super.getId();
              
               }
              }
              

              Regards

              fhh