繁体   English   中英

JPA 支持 Java 8 新日期和时间 API

[英]JPA support for Java 8 new date and time API

我在我的新项目中使用 Java 8。

我正在尝试在 java 8 中使用新的日期和时间 API,但是我不知道JPA 2.1完全支持这个新的日期和时间 API。

请分享您在 JPA 对 Java 8 中新日期和时间 API 的支持方面的经验/意见。

我可以在 JPA 2.1 中安全地使用 Java 8 中的新日期和时间 api 吗?

更新:

我使用 Hibernate (4.3.5.Final) 作为 JPA 实现。

对于 Hibernate 5.X 只需添加

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

@NotNull
@Column(name = "date_time", nullable = false)
protected LocalDateTime dateTime;

无需任何额外努力即可工作。 https://hibernate.atlassian.net/browse/HHH-8844

更新:

请看一下 Jeff Morin 的评论:自从 Hibernate 5.2.x 就足够了

 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>5.2.1.Final</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-...</artifactId>
     <version>4.3.1.RELEASE</version>
 </dependency>

请参阅https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2并将Hibernate 5.2 与 Spring 框架 4.x 集成

JPA 2.1 是在 Java 1.8 之前出现的规范,因此不强制要求对其提供任何支持。 显然,某些实现可能支持某些 Java 1.8 特性。 有些人在 Java 1.8 字节码(例如 EclipseLink)方面存在问题。 我知道 DataNucleus 支持 java.time 和 Java 1.8,因为这是我使用的那个。 您必须检查您的实现以了解其支持级别。

已请求 JPA 2.2 支持 java.time 类型,请参阅此问题https://java.net/jira/browse/JPA_SPEC-63

JPA 2.2 支持 java.time

JPA 2.2现在支持LocalDateLocalTimeLocalDateTimeOffsetTimeOffsetDateTime

<dependency>
  <groupId>javax.persistence</groupId>
  <artifactId>javax.persistence-api</artifactId>
  <version>2.2</version>
</dependency>

对于 JPA 2.2 实现,可以使用Hibernate 5.2EclipseLink 2.7

Hibernate 5 支持比 JPA 2.2 更多的 java 类型,如DurationInstantZonedDateTime

更多信息:

我在我的项目中使用 Java 8、EclipseLink(JPA 2.1)、PostgreSQL 9.3 和 PostgreSQL Driver -Postgresql-9.2-1002.jdbc4.jar,我可以使用新 API 中的LocalDateTime变量而没有错误,但是列将是数据库中的字节。 据我所知,您只能从 Java 应用程序中读取它。

但是,您可以使用AttributeConverter将这些类转换为java.sql.Date 我从Java.net找到了这段代码

@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements
AttributeConverter {
@Override
public java.sql.Date convertToDatabaseColumn(LocalDate entityValue) {
    return java.sql.Date.valueOf(entityValue);
}

@Override
public LocalDate convertToEntityAttribute(java.sql.Date databaseValue) {
    return databaseValue.toLocalDate();
}

org.jadira.usertype可用于持久化 JSR 310 日期和时间 API。

查看此示例项目

从示例项目,

@MappedSuperclass
public class AbstractEntity {

    @Id @GeneratedValue Long id;

    @CreatedDate//
    @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime")//
    ZonedDateTime createdDate;

    @LastModifiedDate//
    @Type(type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime")//
    ZonedDateTime modifiedDate;
}

我知道这是一个老问题,但我想到了一个可能有用的替代解决方案。

您可以利用@Transient,而不是尝试将新的 java.time.* 类映射到现有的数据库类型:

@Entity
public class Person {
    private Long id;        
    private Timestamp createdTimestamp;

    @Id
    @GeneratedValue
    public Long getId() { return id; }

    private Timestamp getCreatedTimestamp() {
        return createdTime;
    }

    private void setCreatedTimestamp(final Timestamp ts) {
        this.createdTimestamp = ts;
    }

    @Transient
    public LocalDateTime getCreatedDateTime() {
        return createdTime.getLocalDateTime();
    }

    public void setCreatedDateTime(final LocalDateTime dt) {
        this.createdTime = Timestamp.valueOf(dt);
    }
}

您使用使用新 Java 8 日期/时间类的公共 getter/setter 方法,但在幕后,getter/setter 使用旧的日期/时间类。 当您保留实体时,将保留旧的日期/时间属性,但不会保留新的 Java 8 属性,因为它使用 @Transient 进行了注释。

对于TIMESTAMP类型,您可以使用此转换器:

@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime datetime) {
        return datetime == null ? null : Timestamp.valueOf(datetime);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
        return timestamp == null ? null : timestamp.toLocalDateTime();
    }

}

对于DATE类型,您可以使用此转换器:

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate date) {
        return date == null ? null : Date.valueOf(date);
    }

    @Override
    public LocalDate convertToEntityAttribute(Date date) {
        return date == null ? null : date.toLocalDate();
    }

}

对于TIME类型,您可以使用此转换器:

@Converter(autoApply = true)
public class LocalTimeAttributeConverter implements AttributeConverter<LocalTime, Time> {

    @Override
    public Time convertToDatabaseColumn(LocalTime time) {
        return time == null ? null : Time.valueOf(time);
    }

    @Override
    public LocalTime convertToEntityAttribute(Time time) {
        return time == null ? null : time.toLocalTime();
    }

}

有很多方法可以做,这也取决于您的框架:如果您的框架在现场转换器上有这样的弹簧,请执行以下操作:1-

@DateTimeFormat(pattern = "dd.MM.yyyy - HH:mm")
private Long createdDate;

在这里,我使用旧时代格式https://www.epochconverter.com/时代是非常灵活和可接受的格式

2-其他方法是使用 jpa @PostLoad @PreUpdate @PrePersist

@PostLoad
      public void convert() {
        this.jva8Date= LocalDate.now().plusDays(1);
      }

或使用 temp 之一

@Transient
public LocalDateTime getCreatedDateTime() {
    return createdTime.getLocalDateTime();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM