How can I implement created_at
and updated_at
columns using Room Persistence ORM
tools in Android, that can update the timestamp automatically when creating or updating a row in a table?
To follow on the above with a Java example.
The Base entity for all the Room entities This maps to a table that contains an id, created_at and updated_at column.
public abstract class BaseEntity implements Serializable {
@PrimaryKey(autoGenerate = true)
private long id;
@ColumnInfo(name = "created_at", defaultValue = "CURRENT_TIMESTAMP")
@TypeConverters(DateConverter.class)
private Date createdAt;
@ColumnInfo(name = "updated_at", defaultValue = "CURRENT_TIMESTAMP")
@TypeConverters(DateConverter.class)
private Date updatedAt;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
The Room Date TypeConverter This converts Java Dates into numbers which can be saved in the sqlite database.
public class DateConverter {
@TypeConverter
public static Date toDate(Long timestamp) {
if (timestamp == null) {
return null;
}
return new Date(timestamp);
}
@TypeConverter
public static Long toTimestamp(Date date) {
if (date == null) {
return null;
}
return date.getTime();
}
}
The abstract BaseDao This Dao implements all the basic Insert, Update and Delete methods.
@Dao
public abstract class AbstractBaseEntityDao<T extends BaseEntity> {
@Insert
public abstract long actualInsert(T t);
public long insert(T t) {
t.setCreatedAt(new Date());
t.setUpdatedAt(new Date());
return actualInsert(t);
}
@Insert
public abstract List<Long> actualInsertAll(List<T> ts);
public List<Long> insertAll(List<T> ts) {
if (ts != null) {
for (T t : ts) {
t.setCreatedAt(new Date());
t.setUpdatedAt(new Date());
}
}
return actualInsertAll(ts);
}
@Update
public abstract void actualUpdate(T t);
public void update(T t) {
t.setUpdatedAt(new Date());
actualUpdate(t);
}
@Update
public abstract void actualUpdateAll(List<T> ts);
public void updateAll(List<T> ts) {
if (ts != null) {
for (T t : ts) {
t.setUpdatedAt(new Date());
}
}
actualUpdateAll(ts);
}
@Delete
public abstract void delete(T t);
@Delete
public abstract void deleteAll(List<T> ts);
}
User and UserDao Here is an example of a concrete Entity and Dao for a User (typical use-case).
@Entity(tableName = "users")
public class User extends BaseEntity {
@ColumnInfo(name = "name")
private String name;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Dao
public abstract class UserDao extends AbstractBaseEntityDao<User> {
@Query("select * from users")
public abstract List<User> getAllUsers();
}
How to insert a User This will use the AbstractBaseEntityDao which sets the created_at and updated_at timestamps.
Note: do not do this on the UI thread!!
YourDatabase database = YourDatabase.getInstance(getApplicationContext());
UserDao userDao = database.userDao();
long userId = userDao.insert(userToAdd);
userToAdd.setId(userId);
How to update the User
YourDatabase database = YourDatabase.getInstance(getApplicationContext());
UserDao userDao = database.userDao();
userDao.update(userToEdit);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With