0 Replies Latest reply on Nov 16, 2006 6:17 AM by hoeft

    @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