Search code examples
javaormlite

Current Timestamp with ORMLite?


When I insert a date in my my table using ORMLite I usually do something like this:

@DatabaseField(dataType=DataType.DATE_STRING, format="yyyy-MM-dd HH:mm:ss")
private Date MatchDate;

What if I want something like

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 

?


Solution

  • You can specify the custom SQL to create the field using the @DatabaseField columnDefinition field.

    @DatabaseField(dataType = DataType.DATE_STRING, format = "yyyy-MM-dd HH:mm:ss",
          columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    private Date MatchDate;
    

    Another option would be to make the schema yourself if you need more control. You can get the create table statements from TableUtils.getCreateTableStatements() method and then you will have to tune them to adjust for the ON UPDATE CURRENT_TIMESTAMP, etc..

    You can execute raw SQL with the dao.executeSql("...") method. Here are the javadocs for this method.

    Here's a link to the documentation of what raw SQL statements you can execute in ORMLite.