4
Is there any way to map properties like java.time.LocalDate
and/or java.time.Instant
through JPA and/or Hibernate?
4
Is there any way to map properties like java.time.LocalDate
and/or java.time.Instant
through JPA and/or Hibernate?
2
As far as I know (and have researched) it is not yet possible to map, the new date classes that have been added in Java 8. At a glance HERE.
It is worth taking a look at the specific implementations of Hibernate or Eclipselink to get some news.
Other than that, I still don’t see why java.time.*
instead of the old java.util.Date
, you can use the JodaTime
to make life easier, or even make a conversion to the type you need, since at the end of the day you have a long
corresponding to milisecs
2
Use the JPA Converters 2.1. With them you can map the types you want.
Works like a JPA Converter.
It would be something like:
@Converter
public class CoverConverter implements AttributeConverter<Cover, String> {
@Override
public String convertToDatabaseColumn(Cover attribute) {
switch (attribute) {
case DUST_JACKET:
return "D";
case HARDCOVER:
return "H";
case PAPERBACK:
return "P";
default:
throw new IllegalArgumentException("Unknown" + attribute);
}
}
@Override
public Cover convertToEntityAttribute(String dbData) {
switch (dbData) {
case "D":
return DUST_JACKET;
case "H":
return HARDCOVER;
case "P":
return PAPERBACK;
default:
throw new IllegalArgumentException("Unknown" + dbData);
}
}
}
http://www.thoughts-on-java.org/2013/10/jpa-21-how-to-implement-type-converter.html http://www.nurkiewicz.com/2013/06/mapping-enums-done-right-with-convert.html
Just be careful not to confuse it with the native Iberian Converter. The problem with using the native Iberian converter is that you will not be able to port your project to another implementation.
0
Convertrs can be used with the java.sql. * second package that link.
@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements
AttributeConverter<java.time.LocalDate, java.sql.Date> {
@Override
public java.sql.Date convertToDatabaseColumn(LocalDate entityValue) {
return java.sql.Date.valueOf(entityValue);
}
@Override
public LocalDate convertToEntityAttribute(java.sql.Date databaseValue) {
return databaseValue.toLocalDate();
}
}
Browser other questions tagged java hibernate api jpa
You are not signed in. Login or sign up in order to post.