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

    @GeneratedValue in MappedSuperclass Problem

    Meinert Schwartau Newbie


      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 {
       public static abstract class EntityWithIntId {
       private long id = -1;
       @GeneratedValue(strategy = GenerationType.AUTO)
       public long getId() {
       return id;
       public void setId(long id) {
       this.id = id;
       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);
       assertEquals("Each entity must have an unique id", NO_ENTITIES, ids

      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