赞
踩
JPA 2.1是一个在Java 1.8之前推出的规范,所以不要求任何支持。 显然有些实现可能支持一些Java 1.8特性。 有些Java 1.8字节码有问题(例如EclipseLink)。 我知道DataNucleus支持java.time和Java 1.8,因为这是我使用的。 你必须检查你的实现是什么样的支持水平。
对于Hibernate 5.X只是添加
org.hibernatehibernate-java8${hibernate.version}
和
@NotNull @Column(name = "date_time", nullable = false) protected LocalDateTime dateTime;
更新:
请看看Jeff Morin的评论:自从Hibernate 5.2.x就足够了
org.hibernatehibernate-core5.2.1.Finalorg.springframeworkspring-...4.3.1.RELEASE
我在我的项目中使用Java 8,EclipseLink(JPA 2.1),PostgreSQL 9.3和PostgreSQL驱动程序-Postgresql-9.2-1002.jdbc4.jar,我可以使用新API的LocalDateTimevariables,但没有问题,但列的数据types是数据库中的bytea,所以你只能从Java应用程序中读取它,据我所知。 您可以使用AttributeConverter将新类转换为java.sql.Date我从Java.net中find这个代码
@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 310date和时间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; }
我知道这是一个老问题,但我想到了一个可能有用的替代解决scheme。
而不是尝试将新的java.time。*类映射到现有的数据库types,您可以利用@Transient:
@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 8date/时间类的公共getter / setter方法,但在后台getter / setter使用旧版date/时间类。 当持久化实体时,遗留的date/时间属性将被保留,但不会保留新的Java 8属性,因为它使用@Transient进行了注释。
JPA 2.2支持java.time
JPA 2.2现在支持LocalDate , OffsetTime , LocalDateTime , OffsetTime和OffsetDateTime 。
javax.persistencejavax.persistence-api2.2
对于JPA 2.2实现,可以使用Hibernate 5.2或EclipseLink 2.7 。
Hibernate 5支持比JPA 2.2更多的Javatypes,比如Duration , Instant和ZonedDateTime 。
更多信息:
如何使用 Thorben Janssen的JPA 2.2映射date和时间API
JPA 2.2有哪些新特性?
JSR 338(维护版本)
维基百科
有很多办法可以做,也取决于你的框架工作:如果你的框架工作在现场转换器这样的spring做这样的:1-
@DateTimeFormat(pattern = "dd.MM.yyyy - HH:mm") private Long createdDate;
2-其他方法是使用jpa @PostLoad @PreUpdate @PrePersist
@PostLoad public void convert() { this.jva8Date= LocalDate.now().plusDays(1); }
或使用温度这样的
@Transient public LocalDateTime getCreatedDateTime() { return createdTime.getLocalDateTime(); }
对于typesTIMESTAMP,你可以使用这个转换器:
@Converter(autoApply = true) public class LocalDateTimeAttributeConverter implements AttributeConverter { @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(); } }
对于DATEtypes,您可以使用此转换器:
@Converter(autoApply = true) public class LocalDateAttributeConverter implements AttributeConverter { @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(); } }
对于时间types你可以使用这个转换器:
@Converter(autoApply = true) public class LocalTimeAttributeConverter implements AttributeConverter { @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(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。