2

I have a hibernate entity Super Class:

@MappedSuperclass
public abstract class Pojo_Entity_SuperClass 
{
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="ID", unique=true, nullable=false, precision=18, scale=0)
   protected long id;
   public Long getId() {return id;}

   //Other entity fields and methods
}

Next I inherite other entity classes like this:

@Entity
@Table(name="USR")
public class Usr extends Pojo_Entity_SuperClass 
{
   //Columns, fileds and others
}

But in some cases I want to inherit entity with "id" field without @GeneratedValue annotation. The question is - how to disable @GeneratedValue annotation for id in child class ?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911

2 Answers2

2

You can simply move the @Id from the base class to the sub-classes and then decide the generation strategies.

So you can have:

@MappedSuperclass
public abstract class Pojo_Entity_SuperClass 
{   
    public abstract Long getId();

    public abstract void setId(Long id);

    //Other entity fields and methods
}

@Entity
@Table(name="USR")
public class Usr extends Pojo_Entity_SuperClass {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID", unique=true, nullable=false, precision=18, scale=0)
    protected long id;  
}

@Entity
@Table(name="ADMIN")
public class Admin extends Pojo_Entity_SuperClass {
    @Id
    @SequenceGenerator(name = "ID_GENERATOR", sequenceName = "admin_id_seq")
    @GeneratedValue(generator = "ID_GENERATOR")
    @Column(name="ID", unique=true, nullable=false, precision=18, scale=0)
    protected long id;  
}

Is this what you need?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • It is not resolution in my case becouse I use id in all of my entities and sometomes the classes are inherited three or more levels. I also have a lot of methods working on id on my super class. – user3566245 May 17 '14 at 09:45
  • Maybe I should write my own ID generator in hibernate and cover it by some annotations in my child classes? – user3566245 May 17 '14 at 09:48
  • I updated my response to accommodate the abstract getId and setId methods. That way you could reference those in your super class methods too. I think you could try to move the @Id from field to method and it could be that overriding that would override the generation as well. But method level [annotation is much trickier than field level](http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access). – Vlad Mihalcea May 17 '14 at 10:50
  • You can even mix [field with property level access type](http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e535). I think it's worth trying to have the @Id at property level and see if the overriding works for you better this way. – Vlad Mihalcea May 17 '14 at 10:55
0

Alternatively, you perhaps actually don't want to avoid inheritance. I tried to do the same before I realized I wanted to create User entity with id from external system and force this id as id in my system. Since entities are otherwise created with our id, common ancestor with id and sequence generator is valid solution while User with external id would be nonsystematic.

Hence it was IMHO cleaner to generate id of User just as in any other case and add another column external_id to table and entity class.

Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64