Hibernate中@Temporal注解有什么用? [英] What is the use of the @Temporal annotation in Hibernate?

查看:35
本文介绍了Hibernate中@Temporal注解有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate 文档包含以下 @Temporal 注释的信息:

The Hibernate Documentation has the information below for the @Temporal annotation:

在普通 Java API 中,没有定义时间的时间精度.在处理时间数据时,您可能想要描述数据库中的预期精度.时态数据可以有 DATE、TIME 或TIMESTAMP 精度(即实际日期、仅时间或两者).用@Temporal 注释来微调.

In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

时间的时间精度未定义是什么意思?什么是 temporal 数据及其精度?它是如何微调的?

What does temporal precision of time is not defined mean? What is temporal data and its precision? How does it fine tune?

推荐答案

必须为 java.util.Datejava.util.Calendar 类型的持久字段或属性指定此注释.只能为这些类型的字段或属性指定.

This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

Temporal 注释可以与 Basic 注释、Id 注释或 ElementCollection 注释结合使用> 注释(当元素集合值是这样的时间类型时.

The Temporal annotation may be used in conjunction with the Basic annotation, the Id annotation, or the ElementCollection annotation (when the element collection value is of such a temporal type.

在普通 Java API 中,没有定义时间的时间精度.在处理时态数据时,您可能希望描述数据库中的预期精度.时态数据可以具有 DATE、TIME 或 TIMESTAMP 精度(即实际日期、仅时间或两者).使用 @Temporal 注释来微调.

In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data, you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (i.e., the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

时间数据是与时间相关的数据.例如,在内容管理系统中,文章的创建日期和最后更新日期是时间数据.在某些情况下,时态数据需要精度并且您希望在数据库表中存储精确的日期/时间或两者(TIMESTAMP).

The temporal data is the data related to time. For example, in a content management system, the creation-date and last-updated date of an article are temporal data. In some cases, temporal data needs precision and you want to store precise date/time or both (TIMESTAMP) in database table.

核心 Java API 中未指定时间精度.@Temporal 是一个 JPA 注释,用于在时间戳和 java.util.Date 之间来回转换.它还将time-stamp 转换为时间.例如,在下面的代码段中,@Temporal(TemporalType.DATE) 删除时间值,只保留日期.

The temporal precision is not specified in core Java APIs. @Temporal is a JPA annotation that converts back and forth between timestamp and java.util.Date. It also converts time-stamp into time. For example, in the snippet below, @Temporal(TemporalType.DATE) drops the time value and only preserves the date.

@Temporal(TemporalType.DATE)
private java.util.Date creationDate;

根据 javadoc,

As per javadocs,

用于在查询中声明适当的 {@code TemporalType} 的注释方法参数.请注意,此注释只能用于类型为 {@link Date} 的参数,带有 default TemporalType.DATE

Annotation to declare an appropriate {@code TemporalType} on query method parameters. Note that this annotation can only be used on parameters of type {@link Date} with default TemporalType.DATE

[以上信息收集自各种来源]

[Information above collected from various sources]

这篇关于Hibernate中@Temporal注解有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆