使用Java 8 LocalDateTime的Spring Data JPA [英] Spring Data JPA with Java 8 LocalDateTime

查看:2140
本文介绍了使用Java 8 LocalDateTime的Spring Data JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去几个月一直在使用Spring Data JPA和MYSQL,这是一次非常成功和顺畅的体验。在那里我使用java 8 LocalDateTime来存储日期时间字段,JPA自动将这些字段映射到mysql tinyblob列。

I have been working with Spring Data JPA and MYSQL last couple of months and it has been a quite successful and smooth experience. In there i am using java 8 LocalDateTime to store the date time fields and the JPA automatically maps those fields in to mysql tinyblob columns.

最近我要求通过脚本向系统添加一些数据。为了填充日期时间列,我创建了MYSQL TIMESTAMP变量并插入到tinyblob列中。但是,系统开始抱怨SerializationException,其根本原因是此转换后的datetime列。
然后我查看通过应用程序插入的日期时间列如下所示

Recently i got a requirement to add some data through a script to the system. In Order to fill the date time columns, i have created MYSQL TIMESTAMP variables and inserted into tinyblob columns. However system started complaining SerializationException and the root cause for that is this converted datetime columns. then i had a look at the date time columns inserted through the application as below

select CAST(drop_off_time AS CHAR(10000) CHARACTER SET utf8) From job

看起来当你通过应用程序插入时,它被插入为某种java序列化。但是通过mysql脚本我们无法复制该功能。

it looks like that when you insert through the application, it inserted as some kind of java serialization. However through a mysql script we can not replicate that functionality.

现在我有两个选择。
1)我需要找到一种编写mysql脚本的方法来生成类似于应用程序的Date time列。
2)我需要将Spring数据JPA映射从tinyblob更改为mysql脚本可以支持的另一种数据类型

Now i have two options. 1) I need to find a way to write a mysql script to generate the Date time columns similar to the application. 2) I need to change the Spring data JPA mapping from tinyblob to an another data type which mysql scripts can support

感谢您的帮助

谢谢,
Keth

Thanks, Keth

编辑

按照下面提供的答案和评论后,我能够找到一个简单的解决方案

After following the answers and comments provided below, i was able to find a simple solution

如果你使用Hibernate 5.0+,你可以放入

if you use Hibernate 5.0+, you can drop in

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

然后系统开始将java 8 LocalDateTime属性映射到mysql DATETIME列

Then the system starts mapping the java 8 LocalDateTime prperties in to mysql DATETIME columns

推荐答案

根据JPA 2.1,LocalDateTime不是支持的(可能在短时间内JPA 2.,2将是官方的)。 Hibernate 5支持'早期发布'

According to JPA 2.1 LocalDateTime isn't suppoorted oficially (probably in short time JPA 2.,2 will be official). Hibernate 5 support as 'early release'

从JPA 2.0开始便携式和支持 javax.persistence.AttributeConverter ,有效在所有JPA提供商上做得很好(并且对Hibernate 5没什么好处)

Portable and supported since JPA 2.0 is javax.persistence.AttributeConverter, works very well on all JPA providers (and makes nothing bad on Hibernate 5)

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

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

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

这篇关于使用Java 8 LocalDateTime的Spring Data JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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