我有以下字段声明:
@Entity
public class TransactionStateHistory {
...
@Column(nullable = false)
private LocalDateTime dateTime;
...
}
以及依赖:
compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.2.10.Final'
我哪里错了?
答案 0 :(得分:4)
从问题我看到你正在尝试使用Hibernate-java8模块而不是使用Hibernate-core
5.2.10.Final,因为Hibernate-java8
被合并在Hibernate-core中,它负责将值转换为LocalDateTime < / p>
这是工作样本
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LocalDateTimeDemoApplication {
public static void main(String[] args) {
SpringApplication.run(LocalDateTimeDemoApplication.class, args);
}
}
我的实体类
import java.time.LocalDateTime;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class DateTimeEntity
{
@Id
@GeneratedValue
private Integer id;
private LocalDateTime localDate;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public LocalDateTime getLocalDate()
{
return localDate;
}
public void setLocalDate(LocalDateTime localDate)
{
this.localDate = localDate;
}
}
我的存储库
import org.springframework.data.jpa.repository.JpaRepository;
public interface LocalDateTimeRep extends JpaRepository<DateTimeEntity, Integer>
{
}
现在测试时间。
import java.time.LocalDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.entity.DateTimeEntity;
import com.example.entity.LocalDateTimeRep;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LocalDateTimeDemoApplicationTests {
@Autowired LocalDateTimeRep repo;
@Test
public void contextLoads() {
DateTimeEntity dateTimeEntity = new DateTimeEntity();
dateTimeEntity.setLocalDate(LocalDateTime.now());
DateTimeEntity persistedEntry = repo.save(dateTimeEntity);
System.out.println(persistedEntry.getLocalDate());
}
}
您可以使用Hibernate 5.2.10.Final保留并检索LocalDateTime类型的值,而不进行任何转换。
键是 POM.XML
,你需要从jpa模块和属性集hibernate.version中排除hibernate-entitymanager到5.2.10.Final
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>LocalDateTimeDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LocalDateTimeDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<hibernate.version>5.2.10.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
**<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
</exclusions>**
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这样你可以在springboot和Hibernate中使用LocalDateTime。
答案 1 :(得分:1)
在您的主应用程序类(例如MainApplication)中,您可以尝试添加以下内容吗?
@EnableJpaRepositories
@EntityScan(basePackageClasses = {MainApplication.class, Jsr310JpaConverters.class})
Jsr310 来自
org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters
答案 2 :(得分:0)
请注意,jpa @Temporal
不支持类似LocalDateTime的java.time
类。
您需要编写转换器并将其设置为自动绑定,如下所示:
import java.sql.Date;
import java.time.LocalDate;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
* Converter for java.time persistence.
*
* @author m.elbehi
*/
@Converter(autoApply = true)
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate date) {
return date != null ? Date.valueOf(date) : null;
}
@Override
public LocalDate convertToEntityAttribute(Date value) {
return value != null ? value.toLocalDate() : null;
}
}