JPQL 和带时区的时间戳
我的一个 JPA 实体中有以下字段定义:
@Column(nullable = false, name = "start_time",
columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date startTime = new Date();
@Column(nullable = true, name = "end_time",
columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date endTime = null;
数据库是 PostgreSQL,不能选择使用没有时区的时间戳。 关于时区的插入和更新进展顺利。 当我尝试对某些测试数据运行 SELECT JPQL 查询并且过滤器参数是 Date 对象时,会出现问题(至少我这么认为)。 OpenJPA 生成的查询在参数中不包含时区。 谁能给我一些关于 JPA 和带有时区列的时间戳的指导?
I have the following field definitions in one of my JPA entities:
@Column(nullable = false, name = "start_time",
columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date startTime = new Date();
@Column(nullable = true, name = "end_time",
columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date endTime = null;
The database is PostgreSQL and using timestamp without timezone is not an option. The inserts and updates are going fine regarding the timezone. A problem arises (at least I think so) when I try to run a SELECT JPQL query on some test data and the filter parameters are Date objects. The query OpenJPA generates doesn't include the timezone in the parameters.
Can anyone give me some guidance regarding JPA and timestamp with timezone columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 JPA 实体存储在数据库中时,我会避免在它们中使用日期和日历。 标准 JDBC 时间值没有时间戳的概念。
我使用以下样式,实际上
,存储在数据库中的数据是一个简单的长值。 但是,在应用程序层上,您仍然使用具有时区概念的 Date 对象。
现在有时您仍然希望在数据库级别存储日期以供查询之用。 在这种情况下,您可以执行以下操作
I would avoid the use of Date and Calendar in JPA entities when they are being stored in the database. Standard JDBC temporal values have no concept of timestamps.
I use the following style instead
Effectively the data that gets stored in the database is a simple long value. However, on the application tier you still use the Date objects which have the concept of timezones.
Now sometimes you would still want to store the date for query purposes on the database level. In which case you can do the following
你可以看看java.util.Calendar。
You could have a look on java.util.Calendar.