Reputation: 141
I'd like to persist LocalDate
into Hibernate
as a Date
type but I can't find it even in the Hibernate documentation. I've tried once but it is stored as blob type.
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. 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. 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.
Upvotes: 1
Views: 3113
Reputation: 197
Although the post is very old, but still replying if someone is trying to use LocalDate with hibernate in hbm. 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."
<property name="birthDate" column="birth_date"
type="converted::com.mypkg.LocalDateConverter"/>
Upvotes: 0
Reputation: 27
(I'm not a native English speaker)
I use Hibernate 5.4, and I found an answer to this problem, which is way easier.
You just have to change the type of your property to org.hibernate.type.LocalDateType
.
This works also for LocalDateTime, you just need to change the type to org.hibernate.type.LocalDateTimeType
.
For any other type, you should consider looking in org.hibernate.type
.
Upvotes: 0
Reputation: 2961
Try this:
<property name="date" column="tb_date" type="LocalDate" />
See Table 2 Java 8 Basic Types from hibernate 5.2 user guide.
Upvotes: 1
Reputation: 26522
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:
<property name="date" column="tb_date" type="date"/>
<convert converter="com.mypkg.MyConverter" attribute-name="date"/>
Upvotes: 1