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
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
@Entityand you would also need an@Idattribute (this missing@Idattribute is the reason for the error you are getting after adding the@Entityannotation)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
@MappedSuperclassIf 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
lastModifiedvalue 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: