问题
Java 8 gives the java.time API.
I am using a grails application, I want to use java.time for properties of my domain classes.
I need to map for instance the LocalDateTime to the DATE/DATETIME on GORM which is built on top of Hibernate.
How can I persist my mappings?
I see a solution, that I can use jadira, and establish static mappings for my attributs.
However is there another way?
Please any help is appreciated.
回答1:
You can use the user type
mapping. See the docs. That should remove the need to explicitly map all instances individually.
To go further, I would take the grails-joda-time
plugin sources and try to make a jave.time
version. And even try to make (and submit a pull request) a version that would support both joda-time
and java.time
.
回答2:
I haven't used gorm, but if we were to speak hibernate only (and I guess that it should be applicable), you would be able to follow two path.
One, if you are using or if you're able to upgrade to hibernate 5, than you can include the hibernate-java8
Support for using Java8 specific data-types such as any of the JSR 310 Date/Time types in domain model.
Otherwise, you would have to write your own type as explained here, and use it e.g. like
@Type(type="your.custom.LocalDateTimeUserType")
private LocalDateTime date;
Finally, gorm
is implementing the JPA
specification, you can implement a converter
来源:https://stackoverflow.com/questions/35507996/grails-mapping-localdatetime-of-java-8-domain-objects-to-gorm