gpt4 book ai didi

java - hibernate-java8 依赖项无助于正确保存日期

转载 作者:行者123 更新时间:2023-12-05 01:05:20 24 4
gpt4 key购买 nike

我有以下字段声明:

@Entity
public class TransactionStateHistory {
...
@Column(nullable = false)
private LocalDateTime dateTime;
...
}

和以下依赖:

compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.2.10.Final'

但在数据库中我看到了<binary data> :
enter image description here

enter image description here

我哪里错了?

最佳答案

从问题中我看到你正在尝试使用 Hibernate-java8 模块而不是使用 Hibernate-core 5.2.10.Final 因为 Hibernate-java8 被合并到 Hibernate-负责将值转换为 LocalDateTime 的核心

这是工作示例

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-entitymanager,并在属性中将 hibernate.version 设置为 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了。

关于java - hibernate-java8 依赖项无助于正确保存日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43583155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com