[英]Hibernate - java.time.LocalDate cannot be cast to java.util.Date
I'm using Maven, Java, and Hibernate to create a simple app 我正在使用Maven,Java和Hibernate创建一个简单的应用程序
I know there are a couple of duplicates regarding this question but what I want to know is why this error pops up when I'm using the latest version of org.hibernate.hibernate-core which is 5.3.4 . 我知道有两个重复的问题,但是我想知道的是为什么当我使用最新版本的org.hibernate.hibernate-core 5.3.4时会弹出此错误。
Based on the questions I've read, I shouldn't have to use hibernate-java8 dependency because hibernate-core has builtin java 8 support and hibernate-java8 is deprecated in Maven. 基于我已阅读的问题,我不必使用hibernate-java8依赖项,因为hibernate-core具有内置的Java 8支持,并且Maven中已弃用了hibernate-java8。
Any idea what's wrong? 知道有什么问题吗? Here're some snippets
这是一些片段
service.addPerson("Rob","Thomas",LocalDate.of(1985,5,5));
the method declaration would be 方法声明将是
public Integer addPerson(String firstName, String lastName, LocalDate birthdate){
Session sesh = factory.openSession();
Transaction trans = null;
try{
tx = sesh.beginTransaction();
person = new Person(firstName,lastName,birthdate);
personID = (Integer) session.save(person);
tx.commit();
} catch (...) {
}
finally{
session.close();
}
return personID;
}
You have to convert LocalDate to Date. 您必须将LocalDate转换为Date。 Here in the first part of the code I convert LocalDate to Date and back to LocalDate object.
在代码的第一部分中,我将LocalDate转换为Date并返回到LocalDate对象。 On the second part I convert LocalDateTime to Date and back to LocalDateTime object.
在第二部分中,我将LocalDateTime转换为Date并返回到LocalDateTime对象。
public class LocalDateToDate {
public static void main(String[] args) {
// Convert java.time.LocalDate to java.util.Date and back to
// java.time.LocalDate
LocalDate localDate = LocalDate.now();
System.out.println("LocalDate = " + localDate);
Date date1 = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("Date = " + date1);
localDate = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("LocalDate = " + localDate);
System.out.println();
// Convert java.time.LocalDateTime to java.util.Date and back to
// java.time.LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("LocalDateTime = " + localDateTime);
Date date2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("Date = " + date2);
localDateTime = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("LocalDateTime = " + localDateTime);
}
}
You can cast LocalDate to Date 您可以将LocalDate转换为Date
Date current = LocalDate.from(Instant.ofEpochMilli(new java.util.Date().getTime())
.atZone(ZoneId.systemDefault()));
LocalDate ld = new java.sql.Date( new java.util.Date().getTime() ).toLocalDate();
If you have : 如果你有 :
public class Person {
private LocalDate birthdate;
}
You may use a converter and use the annotation @Convert(converter = LocalDateConverter.class)
on the field 您可以使用转换器,并在字段上使用
@Convert(converter = LocalDateConverter.class)
批注
@Converter(autoApply=true)
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate d){
return d !=null ? Date.valueOf(d) : null;
}
@Override
public LocalDate convertToEntityAttribute(Date d){
return d !=null ? d.toLocalDate() : null;
}
}
Your constructor call new Person(firstName,lastName,birthdate)
takes java.util.Date
, and not java.time.LocalDate
. 您的构造函数调用
new Person(firstName,lastName,birthdate)
接受java.util.Date
,而不java.time.LocalDate
。 Just change the constructor, and change the data type inside the Person class. 只需更改构造函数,然后更改Person类中的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.