Fah*_*zil 3 jpa spring-data-jpa java-time
我的一个实体中有一个Java 8 LocalTime.
private final LocalTime departureTime;
Run Code Online (Sandbox Code Playgroud)
它是一个带有Spring Data Rest的Spring Boot 1.3.6应用程序.我使用Jsr310JpaConverters来支持JPA的Java 8时间模块.
当我将LocalTime变量保存到MySql时,保存LocalTime变量的日期也被持久化到数据库.假设我在2016年1月1日18:00:00保存,它将持续为2016-01-01 18:00:00.我只想节省时间.即18:00:00.有解决方案吗
提前致谢
这个例子运行:java 8,JPA 2.1,Hibernate 4.3.5.
如果您使用的是JPA 2.1和Hibernate早期版本的5.0版本,则可以实现JPA AttributeConverter:
import java.sql.Time;
import java.time.LocalTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
* Converter to persist LocalDate and LocalDateTime with
* JPA 2.1 and Hibernate older than 5.0 version
**/
@Converter(autoApply = true)
public class LocalTimeAttributeConverter implements AttributeConverter<LocalTime, Time>{
@Override
public Time convertToDatabaseColumn(LocalTime localTime) {
return (localTime == null ? null : Time.valueOf(localTime));
}
@Override
public LocalTime convertToEntityAttribute(Time time) {
return (time == null ? null : time.toLocalTime());
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是Hibernate 5.0(或更高版本),则可以添加maven的依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
注意 - >实体的映射是相同的.
归档时间: |
|
查看次数: |
3339 次 |
最近记录: |