Tapas Bose
Tapas Bose

Reputation: 29806

Hibernate Annotation Inheriting Abstract Base Class

In my application I am trying to implement Hibernate mappings through annotation. There I have a base class which is abstract, in this class the Id attribute is present. I am inheriting this base class with a child class. The code is given below:

@MappedSuperclass
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Base implements IBase {

    private static final long serialVersionUID = -1433573674276444516L;

    private int id;

    public Base() {

    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)   
    @Column(name="ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

And the child class is:

@Entity
@Table(name="USER")
public class User extends Base implements IUser {

    private static final long serialVersionUID = 344528694909088439L;

    private String name;

    public User() {

    }

    @Column(name="NAME", nullable=false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}

It is creating the USER Table and working fine.

I was wondering whether or not I am doing it in right way.

Thanks.

Upvotes: 1

Views: 7546

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

If the goal is just to have several independant entities to inherit a common field from a base class, then no, you're not doing it correctly. The annotation @Inheritance is unnecessary. @Inheritance is necessary when you have an entity (Vehicle, for example), and several sub-entities (Car, Bike, for example).

Upvotes: 4

Related Questions