patternjavaMinor
Making a database entity superclass
Viewed 0 times
databasemakingsuperclassentity
Problem
I am using JPA for my database work and I need to create a database entity superclass which all of my table entities will extend.
Every database entity will have a primary key named
Anything that I am doing wrong or that I should add in the superclass?
Every database entity will have a primary key named
id (which in some cases is an int and in some a String) and also a lastUpdate field which stores the time at which the database row was last updated (set from within the code).DatabaseEntity superclass:import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class DatabaseEntity {
@Id
@Basic(optional = false)
@Column(name = "id")
private T _id;
@Basic(optional = false)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_update")
private Date _lastUpdate;
public T getId() {
return _id;
}
public void setId(T id) {
_id = id;
}
public Date getLastUpdate() {
return _lastUpdate;
}
public void setLastUpdate() {
_lastUpdate = new Date();
}
}Anything that I am doing wrong or that I should add in the superclass?
Solution
Looks fine for me, but such a class should be
Making it abstract will reflect the actual abstraction of this entity.
One more thing, try to avoid
abstract, because it does not make sense to be able create instances of this type.public abstract class DatabaseEntity {
//
}Making it abstract will reflect the actual abstraction of this entity.
One more thing, try to avoid
java.util.Date as much as you can, it is just awful. You can use Joda time instead, or the new java date api if you using Java 8.Code Snippets
public abstract class DatabaseEntity <T>{
//
}Context
StackExchange Code Review Q#64736, answer score: 2
Revisions (0)
No revisions yet.