简体   繁体   English

如何将 LocalDate 作为日期类型持久化到 Hibernate

[英]How to persist LocalDate into Hibernate as a Date type

I'd like to persist LocalDate into Hibernate as a Date type but I can't find it even in the Hibernate documentation.我想将LocalDate作为Date类型保留到Hibernate ,但即使在 Hibernate 文档中也找不到它。 I've tried once but it is stored as blob type.我试过一次,但它存储为 blob 类型。

Here is my Ticket entity:这是我的票务实体:

  <?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>

And here is my class entity:这是我的班级实体:

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;
}

It has getters and setters as a normal POJO class.它具有作为普通 POJO 类的 getter 和 setter。 I have seen in other websites one way to do that but they are using anotations.我在其他网站上看到过一种方法,但他们正在使用注释。 I would like something similar but I am not using anotations just normal POJO class and hbm.xml files.我想要类似的东西,但我不使用注释只是普通的 POJO 类和hbm.xml文件。 I'm pretty sure I have to create another class in order to convert LocalDate into Date but I don't know how to connect that class with my entity.我很确定我必须创建另一个类才能将LocalDate转换为Date但我不知道如何将该类与我的实体连接起来。

You have to create a converter:您必须创建一个转换器:

@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();
    }
}

Then in your hbm.xml file you add you converter as a type of that property:然后在您的 hbm.xml 文件中,您将转换器添加为该属性的类型:

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

Try this:尝试这个:

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

See Table 2 Java 8 Basic Types from hibernate 5.2 user guide .请参阅hibernate 5.2 用户指南中的表 2 Java 8 基本类型。

(I'm not a native English speaker) (我的母语不是英语)

I use Hibernate 5.4, and I found an answer to this problem, which is way easier.我使用 Hibernate 5.4,我找到了这个问题的答案,这更容易。

You just have to change the type of your property to org.hibernate.type.LocalDateType .您只需将属性的类型更改为org.hibernate.type.LocalDateType

This works also for LocalDateTime, you just need to change the type to org.hibernate.type.LocalDateTimeType .这也适用于 LocalDateTime,您只需要将类型更改为org.hibernate.type.LocalDateTimeType

For any other type, you should consider looking in org.hibernate.type .对于任何其他类型,您应该考虑查看org.hibernate.type

Although the post is very old, but still replying if someone is trying to use LocalDate with hibernate in hbm.虽然帖子很旧,但如果有人试图在 hbm 中使用 LocalDate 和 hibernate,仍然会回复。 In order to use the convertor written by @Maciej Kowalski, one should use the following entry in the hbm file as pointed in hibernate docs Example 28. HBM mapping for AttributeConverter "To map the MoneyConverter using HBM configuration files you need to use the converted:: prefix in the type attribute of the property element."为了使用@Maciej Kowalski 编写的转换器,应该使用 hbm 文件中的以下条目,如 hibernate docs Example 28. HBM mapping for AttributeConverter “要使用 HBM 配置文件映射 MoneyConverter,您需要使用转换后的文件: : 属性元素的类型属性中的前缀。”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM