Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Entity With Abstract Class Inheritance

I have an abstract class that provides some common functionality that some of the EJB entities which to inherit. One of these is a timestamp column.

public abstract class AbstractEntity {

    ...
    private long lastModified;
    ...

    @Column
    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long ts) {
       lastModified = ts;
    }
}

and

@Table
@Entity
public class MyEntity extends AbstractEntity {
    ...
    private Long key;
    private String value;
    ...

    @Id
    public Long getKey() {
        return key;
    }

    public void setKey(Long k) {
        key = k;
    }

    @Column
    public String getValue() {
        return value;
    }

    public void setValue(String txt) {
        value = txt;
        setLastModified(System.currentTimeMillis());
    }
}

The issue is that the timestamp column is not being added to the database table. Is there some annotation that needs to be added to AbstractEntity in order for the lastModified fields to be inherited as a column?

I tried adding @Entity to the AbstractEntity but that caused an exception at deployment.

org.hibernate.AnnotationException: No identifier specified for entity:
AbstractEntity
like image 224
Dobbo Avatar asked Jan 17 '13 12:01

Dobbo


People also ask

Can entity be an abstract class?

Entity classes can be both abstract and concrete.

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

How does JPA inheritance work?

JPA Inheritence Overview Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code. The relational database doesn't support the mechanism of inheritance.

What JPA inheritance types can you name?

There are three inheritance strategies defined from the InheritanceType enum, SINGLE_TABLE , TABLE_PER_CLASS and JOINED . Single table inheritance is the default, and table per class is an optional feature of the JPA spec, so not all providers may support it.


1 Answers

You have several possibilities here.

You did not define a mapping for your superclass. If it is supposed to be a queryable type, you should annotate it with @Entity and you would also need an @Id attribute (this missing @Id attribute is the reason for the error you are getting after adding the @Entity annotation)

If you do not need the abstract superclass to be a queryable entity, but would like to have it's attributes as columns in tables of it's subclasses, you need to annotate it with @MappedSuperclass

If you do not annotate your superclass at all, it is considered to be transient by the provider and is not mapped at all.

EDIT: By the way, you do not have to modify the lastModified value yourself (except you really want to) - you can let the persistence provider do it for you each time you persist the entity with a lifecycle callback:

@PreUpdate
void updateModificationTimestamp() {
 lastModified = System.currentTimeMillis();
}
like image 199
kostja Avatar answered Jan 01 '23 23:01

kostja