--Use Doma2. --Automatically generate Entity with Doma-Gen. --UTC is epoch seconds. --The DB TIMESTAMP type shall be able to store UTC (epoch seconds).
When there is a TIMESTAMP type (date and time) in the DB table, I was wondering which type to handle. Please give us your opinion.
Doma2 supports the following linked types as date / time types. http://doma.readthedocs.io/ja/stable/basic/#id4
Among them, when dealing with date + time, it will be one of the following types.
In Doma-Gen in the Doma1 era, the type corresponding to the TIMESTAMP type (date and time) was java.sql.Timestamp. http://doma.seasar.org/extension/doma_gen_gen_task.html#EntityConfig
However, in Doma-Gen of Doma2, it is java.time.LocalDateTime. http://doma-gen.readthedocs.io/ja/stable/gen/#entityconfig
For more information on the following site, java.time.LocalDateTime does not store the date and time in UTC, but rather the date and time, which does not contain any timezone information. https://qiita.com/dmikurube/items/15899ec9de643e91497c#javatimelocaldatetime
In DB TIMESTAMP type (date and time), the date and time are stored in UTC, so if you handle it with java.time.LocalDateTime, the time zone information will be lost.
By the way, in PostgreSQL, it is stored internally in UTC. https://www.postgresql.jp/document/9.6/html/datatype-datetime.html
java.sql.Timestamp can hold UTC, so if you specify a time zone, the date and time in that time zone will be determined. In other words, if the date and time are handled in UTC inside the system, you can specify the time zone and convert it to the date and time representation of that time zone at the time of output. In that sense, I think it's better to use java.sql.Timestamp.
On the other hand, in the case of java.time.LocalDateTime, it is necessary to be aware of which time zone the date and time representation is, which is the date and time that does not include any time zone information. For example, if both the DB and the system can be limited to one time zone, such as only the time zone in Japan, it may be sufficient.
Prepare a custom class by inheriting the GenDialect class of the DB to be used (GenDialect class of PostgreSQL in this example).
/**
*A class that customizes dialects for PostgreSQL
*/
public class CustomPostgresGenDialect extends PostgresGenDialect {
public CustomPostgresGenDialect() {
//Timestamp with timezone is java.sql.Map to Timestamp.
classNameMap.put("timestamptz", Timestamp.class.getName());
}
}
Run Doma-Gen with the above class in the top level genDialectClassName
. (The specified class must be included in the class path when executing the task.)
http://doma-gen.readthedocs.io/ja/stable/gen/#id2
Recommended Posts