问题描述
我试图使用usertype在JPA中使用Hibernate持久化Java 8中的java.time.LocalDateTime。
I'm trying to persist java.time.LocalDateTime in Java 8 in JPA with Hibernate using usertype.
我有一个注释如下的属性。 p>
I've a property annotated as following.
@Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime")
private java.time.LocalDateTime createdDateTime;
我已经添加了usertype的依赖关系,如下所示。
And I've added dependencies for usertype as following.
compile 'org.jadira.usertype:usertype.extended:3.2.0.GA'
compile 'org.threeten:threetenbp:0.9'
但是,我收到以下例外情况:
However, I'm getting following exceptions
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:119)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 20 more
Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:218)
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:204)
at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:103)
at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:130)
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:327)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
at org.hibernate.mapping.Property.isValid(Property.java:241)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
... 28 more
Caused by: java.lang.TypeNotPresentException: Type org.threeten.bp.LocalDateTime not present
at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.generics.repository.ClassRepository.getSuperclass(ClassRepository.java:86)
at java.lang.Class.getGenericSuperclass(Class.java:764)
at org.jadira.usertype.spi.utils.reflection.TypeHelper.getTypeArguments(TypeHelper.java:73)
at org.jadira.usertype.spi.shared.AbstractSingleColumnUserType.<init>(AbstractSingleColumnUserType.java:41)
at org.jadira.usertype.spi.shared.AbstractParameterizedUserType.<init>(AbstractParameterizedUserType.java:24)
at org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime.<init>(PersistentLocalDateTime.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:213)
... 39 more
Caused by: java.lang.ClassNotFoundException: org.threeten.bp.LocalDateTime
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
... 56 more
堆栈跟踪暗示 ClassNotFound
但我添加了依赖关系'org.threeten:threetenbp:0.9'
The stacktrace implies ClassNotFound
but I've added dependency 'org.threeten:threetenbp:0.9'
我的问题
如何坚持 java.time.L使用usertype的JPA与JPA中的ocalDateTime
?
推荐答案
它不工作,因为你使用错误类型。您应该使用 org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime
而不是 org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime
用于JSR-310类。
您不需要 org.threeten:threetenbp:0.9
依赖项。
It does not work because you are using the wrong type. You should use org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime
instead of org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime
for the JSR-310 classes.
You don't need the org.threeten:threetenbp:0.9
dependency.
这篇关于如何使用usertype在JPA中使用Hibernate持久化JSR 310 java.time.LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!