如何将 LocalDate 作为 Date 类型持久保存到 Hibernate

Posted

技术标签:

【中文标题】如何将 LocalDate 作为 Date 类型持久保存到 Hibernate【英文标题】:How to persist LocalDate into Hibernate as a Date type 【发布时间】:2017-02-11 16:49:08 【问题描述】:

我想将LocalDate 持久化为Hibernate 作为Date 类型,但即使在Hibernate 文档中也找不到它。我试过一次,但它存储为 blob 类型。

这是我的 Ticket 实体:

  <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
        <hibernate-mapping package="com.clustertech.entity">

   <class name="Ticket"  table="ticket">
    <id name="id" type="int" column="id">
        <generator class="native" />
    </id>
    <property name="date" column="tb_date" type="date"  length="35"/> 
    <property name="topic" column="tb_topic" type="string" length="35"/>
    <property name="subject" column="tb_subject" type="string" length="35"/>
    <property name="status" column="tb_status" type="string" length="35"/>
    <property name="message" column="tb_message" type="string"     length="255"/>

    <many-to-one name="person" column="person_id"/>

      </class>
  </hibernate-mapping>

这是我的班级实体:

public class Ticket implements Comparable<Ticket> 

   private int id;
   private LocalDate date;
   private String topic;
   private String Subject;
   private String message;
   private String status;
   private Person person;

它像普通的 POJO 类一样具有 getter 和 setter。我在其他网站上看到了一种方法,但他们正在使用注释。我想要类似的东西,但我没有使用普通 POJO 类和 hbm.xml 文件的注释。我很确定我必须创建另一个类才能将 LocalDate 转换为 Date 但我不知道如何将该类与我的实体联系起来。

【问题讨论】:

【参考方案1】:

你必须创建一个转换器:

@Converter
public class MyConverter implements AttributeConverter<LocalDate, Date> 

    @Override
    public Date convertToDatabaseColumn(LocalDate localDate) 
        if(localDate == null)
            return null;
        

        return Date.valueOf(localDate);
    

    @Override
    public LocalDate convertToEntityAttribute(Date date) 
        if(date == null)
            return null;
        

        return date.toLocalDate();
    

然后在您的 hbm.xml 文件中添加转换器作为该属性的类型:

<property name="date" column="tb_date" type="date"/>
<convert converter="com.mypkg.MyConverter" attribute-name="date"/>

【讨论】:

我试过了,但我遇到了这个错误 无法确定类型:com.clustertech.entity.TypeConverter,在表:票证,列:[org.hibernate.mapping.Column(tb_date )]。我不知道发生了什么,我做了你写的。我错过了什么吗?似乎 Hibernate 在启动时找不到我的转换器类。 尝试添加 【参考方案2】:

试试这个:

<property name="date" column="tb_date" type="LocalDate" /> 

参见hibernate 5.2 user guide 中的表 2 Java 8 基本类型。

【讨论】:

【参考方案3】:

(我的母语不是英语)

我使用 Hibernate 5.4,我找到了这个问题的答案,这更容易。

您只需将属性类型更改为org.hibernate.type.LocalDateType

这也适用于 LocalDateTime,您只需将类型更改为 org.hibernate.type.LocalDateTimeType

对于任何其他类型,您应该考虑查看org.hibernate.type

【讨论】:

【参考方案4】:

虽然帖子很旧,但如果有人试图在 hbm 中使用 LocalDate 和休眠,仍然会回复。为了使用由@Maciej Kowalski 编写的转换器,应使用 hibernate 文档示例 28 中指出的 hbm 文件中的以下条目。 AttributeConverter 的 HBM 映射“要使用 HBM 配置文件映射 MoneyConverter,您需要使用转换后的: : 属性元素的 type 属性中的前缀。”

<property name="birthDate" column="birth_date"
                  type="converted::com.mypkg.LocalDateConverter"/>

【讨论】:

以上是关于如何将 LocalDate 作为 Date 类型持久保存到 Hibernate的主要内容,如果未能解决你的问题,请参考以下文章