具有泛型和多级继承的JPA。设置单表策略。 org.hibernate.MappingException:无法获得构造函数 [英] JPA with generics and multilevel inheritance. Set Single Table strategy. org.hibernate.MappingException: Could not get constructor
问题描述
我的实体有问题。
一切正常,直到我改变了类继承:
GenericModel - >保险 - >医生
GenericModel - >保险 - >护士
到:
GenericModel - >保险 - >责任>医生
GenericModel - >保险 - >责任>护士
GenericModel - >保险 - >合同
我做了什么:
责任
相关元素外化为新的责任
类。
合同
类,它继承自 Insurance
class。
Insurance
class中使用了与 Liability
有关的一些功能/元素。
以下是具有新结构的实体:
@MappedSuperclass
public abstract class GenericModel< T extends GenericModel> {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)public Long id;
@Transient受保护的类< T> entityClass;
public GenericModel(){
类obtainClass = getClass();
类型genericSuperclass = null; (;;){
genericSuperclass = obtainClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType){
break;
}
obtainClass = obtainClass.getSuperclass();
}
ParameterizedType genericSuperclass_ =(ParameterizedType)genericSuperclass;
entityClass =((Class)((Class)genericSuperclass_.getActualTypeArguments()[0]));
@Entity
@DiscriminatorColumn(name =type)
@DiscriminatorValue(value =A)
@Table(name =mi__Insurance)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
公共类Insurance< T延伸保险>扩展GenericModel< T> {
public Insurance(){
super();
this.status = Status.STARTED.getId();
this.statusDescription = Status.getDescriptionBy(this.status);
this.statusColor = Status.getColorBy(status);
this.statusAction = Status.getActionBy(status);
this.progress = 0;
this.policyHolder_Answer = null;
this.policyHolder = 1;
this.filledParts = 1;
this.totalParts = 2;
@Entity
@DiscriminatorValue(value =C)
公共类合同延伸Insurance< Contract> {
public Contract(){
super();
this.setColor(危险);
this.setIcon();
this.setFormUrl();
@Entity
@DiscriminatorValue(value =L)
公共类责任延伸Insurance< Liability> {
public Liability(){
super();
this.legal_Answer = null;
this.freely = 0;
this.mandatory = 0;
this.hiv = 0;
this.legal = 0;
$ b $ / code $ / pre
@Entity
@DiscriminatorValue(value =D)
public class Doctor延伸责任{
public Doctor() {
super();
this.setType(Type.DOCTOR);
this.setColor(success);
this.setIcon();
this.setFormUrl();
this.hiv_Answer = null;
code
一切看起来都不错,但不幸的是我收到了一个错误:
play.api.UnexpectedException:意外的异常[PersistenceException:[ PersistenceUnit:defaultPersistenceUnit]无法构建EntityManagerFactory]
导致:javax.persistence.PersistenceException:[PersistenceUnit:defaultPersistenceUnit]无法构建EntityManagerFactory
导致:org.hibernate.MappingException:无法获取org的构造函数org.hibernate.InstantiationException:.hibernate.persister.entity.SingleTableEntityPersister
。通过致无法实例测试objectmyInsurance.models.Insurance
产生的原因:java.lang.reflect.InvocationTargetException:空
由于:java.lang.ClassCastException:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法转换为java.lang.Class
这是完整的堆栈跟踪:
! @ 6mped6a12 - 内部服务器错误,用于(GET)[/] - >
play.api.UnexpectedException:意外的异常[的PersistenceException:[PersistenceUnit:defaultPersistenceUnit]无法建立的EntityManagerFactory]在play.core.ReloadableApplication $$ anonfun $得到$ 1 $$ anonfun $
申请$ 1 $$ anonfun $ 1.apply(ApplicationProvider.scala:148)〜[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication $$ anonfun $ get $ 1 $$ anonfun $ apply $ 1 $$ anonfun $ 1.apply(ApplicationProvider.scala:112)〜[play_2.10.jar:2.2.6]
at scala.Option.map(Option.scala:145)〜[scala-library.jar:na]
在play.core.ReloadableApplication $$ anonfun $ get $ 1 $$ anonfun $ apply $ 1.apply(ApplicationProvider.scala:112)〜[play_2.10.jar:2.2.6]
在玩。 core.ReloadableApplication $$ anonfun $ get $ 1 $$ anonfun $ apply $ 1.apply(ApplicationProvider.scala:110)〜[play_2.10.jar:2.2.6]
at scala.util.Success.flatMap(Try .scala:200)〜[scala-library.jar:na]
在play.core.ReloadableApplication $$ anonfun $ get $ 1.apply(ApplicationProvider.scala:11 0)〜[play_2.10.jar:2.2.6]
在play.core.ReloadableApplication $$ anonfun $ get $ 1.apply(ApplicationProvider.scala:102)〜[play_2.10.jar:2.2.6 ]
at scala.concurrent.impl.Future $ PromiseCompletingRunnable.liftedTree1 $ 1(Future.scala:24)〜[scala-library.jar:na]
at scala.concurrent.impl.Future $ PromiseCompletingRunnable。运行(Future.scala:24)〜[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask $ AdaptedRunnableAction.exec(ForkJoinTask.java:1361)〜[scala-library.jar:na ]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)〜[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool .java:1339)〜[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)〜[scala-library.jar:na]
在scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)〜[scala-library.jar:na]
引起:javax.persistence.PersistenceException:[PersistenceUnit:defaultPersistenceUnit]无法在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)〜[hibernate-entitymanager-4.2.0.CR1.jar]上构建EntityManagerFactory
:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)〜[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)〜[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
在javax.persistence.Persistence。 createEntityManagerFactory(Persistence.java:63)〜[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)〜[hibernate-jpa -2.0-api.jar:1.0.1.Final]
在play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35)〜[play-java-jpa_2.10.jar:2.2.6]
在play.api.Play $$ anon fun $ start $ 1 $$ anonfun $ apply $ mcV $ sp $ 1.apply(Play.scala:88)〜[play_2.10.jar:2.2.6]
at play.api.Play $$ anonfun $ start $ 1 $$ anonfun $ apply $ mcV $ sp $ 1.apply(Play.scala:88)〜[play_2.10.jar:2.2.6]
at scala.collection.immutable.List.foreach(List.scala :318)〜[scala-library.jar:na]
at play.api.Play $$ anonfun $ start $ 1.apply $ mcV $ sp(Play.scala:88)〜[play_2.10.jar: 2.2.6]
at play.api.Play $$ anonfun $ start $ 1.apply(Play.scala:88)〜[play_2.10.jar:2.2.6]
at play.api。 Play $$ anonfun $ start $ 1.apply(Play.scala:88)〜[play_2.10.jar:2.2.6]
at play.utils.Threads $ .withContextClassLoader(Threads.scala:18)〜[ play_2.10.jar:2.2.6]
在play.api.Play $ .start(Play.scala:87)〜[play_2.10.jar:2.2.6]
在play.core .ReloadableApplication $$ anonfun $ get $ 1 $$ anonfun $ apply $ 1 $$ anonfun $ 1.apply(ApplicationProvider.scala:139)〜[play_2.10.jar:2.2.6]
... 14个常用框架省略
引起:org.hibernate.MappingException:无法获取构造为在org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)org.hibernate.persister.entity.SingleTableEntityPersister
〜[冬眠核-4.2.0.CR1.jar:4.2。 0.CR1]
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
在org.hibernate.internal.SessionFactoryImpl。< init>(SessionFactoryImpl.java:385)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate。 cfg.Configuration.buildSessionFactory(Configuration.java:1742)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.EntityManagerFactoryImpl。< init>( EntityManagerFactoryImpl.java:94)〜[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)〜[hibernate -entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
... 28 org.hibernate.InstantiationException:在org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:49)不能实例测试objectmyInsurance.models.Insurance
〜[通过公共帧省略了
引起的冬眠芯-4.2.0.CR1.jar:4.2.0.CR1]在org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68)
〜[冬眠核-4.2.0 .CR1.jar:4.2.0.CR1]
在org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:75)〜[hibernate-core-4.2.0.CR1.jar:4.2.0。 (EntityMetamodel.java:145)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1] $ b [or] $ b at org.hibernate.persister.entity.AbstractEntityPersister。< init>(AbstractEntityPersister.java:507)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org .hibernate.persister.entity.SingleTableEntityPersister<初始化>(SingleTableEntityPe rsister.java:146)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)〜[na:1.7.0_80]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)〜[na:1.7.0_80]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[na: 1.7.0_80]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)〜[na:1.7.0_80]
at org.hibernate.persister.internal.PersisterFactoryImpl.create( PersisterFactoryImpl.java:163)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
... 33个常见帧被忽略
由java.lang.reflect引起。 InvocationTargetException:null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)〜[na:1.7.0_80]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)〜[na: 1.7.0_80]
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[NA:1.7.0_80]在java.lang.reflect.Constructor.newInstance(Constructor.java:526)
〜[NA:1.7。 0_80]
在org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:46)〜[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
。 .. 43共同框架省略
引起:java.lang.ClassCastException:sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法在泛型.models.GenericModel转换为java.lang.Class
。< init>(GenericModel.java:51)〜[na:na]
at myInsurance.models.Insurance。< init>(Insurance.java:151)〜[na:na]
... 48个常用框架省略
a href =https://stackoverflow.com/q/31557468/2794446>这里是我之前发表的关于继承泛型的文章,它引发了我的这个问题。
也许我的构造函数有一些问题。我不知道,也许我应该在某处声明参数类型的构造函数?
请帮助我。
EDIT-工作解决方案
@MappedSuperclass
公共抽象类GenericModel< T扩展GenericModel> ; {
/ **
*适用于嵌套/内部类和类型化参数类
* /
public GenericModel(){
类obtainClass = getClass();
类型genericSuperclass = null; (;;){
genericSuperclass = obtainClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType){
break;
}
obtainClass = obtainClass.getSuperclass();
}
ParameterizedType genericSuperclass_ =(ParameterizedType)genericSuperclass;
try {
entityClass =((Class)((Class)genericSuperclass_.getActualTypeArguments()[0]));
} catch(ClassCastException e){
entityClass = guessEntityClassFromTypeParametersClassTypedArgument();
code
$ b $ hr
/ **
*用于扩展GenericModel< ExtendingClass< TypeArgument>>
*如果传递给GenericModel TypeParameter,它不是实际的类,而是TypedParameter的类
* @return
* /
public Class< T> guessEntityClassFromTypeParametersClassTypedArgument(){
return ClassUtil.getActualTypeBinding(getClass(),GenericModel.class,0);
package org.fastnate.data.util;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
/ **
*助手检查课程。
*
* @author Tobias Liefke
* /
public final class ClassUtil {
private static< I>类型getActualTypeArgument(final Class< ;? extends I> instanceClass,final Class< I> superClass,
final int argumentIndex){
final List< Type> parents = new ArrayList<>();
parents.add(instanceClass.getGenericSuperclass());
parents.addAll(Arrays.asList(instanceClass.getGenericInterfaces())); (最终类型parentType:父母){
final Class<?>
parentClass = parentType instanceof ParameterizedType? (Class<>)((ParameterizedType)parentType)
.getRawType():(Class<> parentType;
//首先检查我们是否找到超类或接口
if(superClass.equals(parentClass)){
//我们找到了请求的超类 - 使用绑定$(b $ b)return((ParameterizedType)parentType).getActualTypeArguments()[argumentIndex];
} else if(parentClass!= null&& superClass.isAssignableFrom(parentClass)){
// Else step up
final Type type = getActualTypeArgument(parentClass.asSubclass(superClass), superClass,argumentIndex);
if(类型instanceof Class){
返回类型;
} else if(type instanceof TypeVariable){
return((ParameterizedType)parentType).getActualTypeArguments()[Arrays.asList(
parentClass.getTypeParameters())。indexOf(type)];
}
}
}
返回null;
}
/ **
*解析在超类中指定并在子类中绑定的泛型类型的实际绑定。
*
* @param instanceClass
*实现类
* @param superClass
*指定泛型类型变量的超类或接口
* @ param argumentIndex
*超类定义中类型变量的索引(0 =第一个类型变量)
* @返回instanceClass中变量的绑定类
* /
公共静态< T,I>类< T> getActualTypeBinding(final Class< ;? extends I> instanceClass,
final Class< I> superClass,final int argumentIndex){
Type type = getActualTypeArgument(instanceClass,superClass,argumentIndex); (类型instanceof TypeVariable<>){
type =((TypeVariable<>)类型).getBounds()[0];
}
if(type instanceof ParameterizedType){
type =((ParameterizedType)type).getRawType(); (类型instanceof Class<>){
return(Class< T>)类型的
}
;
}
抛出new NoSuchElementException(+ instanceClass中找不到+ argumentIndex +。+ superClass
+参数的绑定。
private ClassUtil(){
//助手类
}
}
解决方案这与JPA无关。
如果你看一下stacktrace:
.lang.ClassCastException:
sun.reflect.generics.reflectiveObjects.TypeVariableImpl无法在generic.models.GenericModel上转换为java.lang.Class
。< init>(GenericModel.java:51)〜 [na:na]
它引用了这一行:
$ b $(< pre> entityClass =((Class)((Class)genericSuperclass_.getActualTypeArguments()[0]));
Insurance
的实际类型参数不是一个显式类,但是一个类型变量。你不能将它转换成类。
如果你想找到你的类的参数的实际绑定 - 我已经为加藤法尼: ClassUtil.getActualTypeBinding :
ClassUtil.getActualTypeBinding(getClass(),GenericModel.class,0);
I have problem with my entity.
Everything works fine until I've changed classes inheritance from:
GenericModel -> Insurance -> Doctor
GenericModel -> Insurance -> Nurse
to:
GenericModel -> Insurance -> Liability > Doctor
GenericModel -> Insurance -> Liability > Nurse
GenericModel -> Insurance -> Contract
What have I done:
- I've externalise some
Liability
releated elements to new Liability
class.
- I've added
Contract
class, which inheritade from Insurance
class.
- I've took some functionality/elements related to
Liability
from Insurance
class.
Here are my entities with new structure:
@MappedSuperclass
public abstract class GenericModel<T extends GenericModel> {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long id;
@Transient protected Class<T> entityClass;
public GenericModel() {
Class obtainedClass = getClass();
Type genericSuperclass = null;
for(;;) {
genericSuperclass = obtainedClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType) {
break;
}
obtainedClass = obtainedClass.getSuperclass();
}
ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
}
}
@Entity
@DiscriminatorColumn(name="type")
@DiscriminatorValue(value="A")
@Table(name="mi__Insurance")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Insurance<T extends Insurance> extends GenericModel<T> {
public Insurance() {
super();
this.status = Status.STARTED.getId();
this.statusDescription = Status.getDescriptionBy(this.status);
this.statusColor = Status.getColorBy(status);
this.statusAction = Status.getActionBy(status);
this.progress = 0;
this.policyHolder_Answer = null;
this.policyHolder = 1;
this.filledParts = 1;
this.totalParts = 2;
}
}
@Entity
@DiscriminatorValue(value="C")
public class Contract extends Insurance<Contract> {
public Contract() {
super();
this.setColor("danger");
this.setIcon();
this.setFormUrl();
}
}
@Entity
@DiscriminatorValue(value="L")
public class Liability extends Insurance<Liability> {
public Liability() {
super();
this.legal_Answer = null;
this.freely = 0;
this.mandatory = 0;
this.hiv = 0;
this.legal = 0;
}
}
@Entity
@DiscriminatorValue(value="D")
public class Doctor extends Liability {
public Doctor() {
super();
this.setType(Type.DOCTOR);
this.setColor("success");
this.setIcon();
this.setFormUrl();
this.hiv_Answer = null;
}
}
Everything looks ok for me, but unfortunately I am getting an errors:
play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Caused by: org.hibernate.InstantiationException: could not instantiate test objectmyInsurance.models.Insurance
Caused by: java.lang.reflect.InvocationTargetException: null
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
Here is full stack trace:
! @6mped6a12 - Internal server error, for (GET) [/] ->
play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.6]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.6]
at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:102) ~[play_2.10.jar:2.2.6]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:na]
at play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.api.Play$$anonfun$start$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.6]
at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.2.6]
at play.api.Play$.start(Play.scala:87) ~[play_2.10.jar:2.2.6]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:139) ~[play_2.10.jar:2.2.6]
... 14 common frames omitted
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:385) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
... 28 common frames omitted
Caused by: org.hibernate.InstantiationException: could not instantiate test objectmyInsurance.models.Insurance
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:49) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:75) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:145) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:507) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:146) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_80]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_80]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_80]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_80]
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
... 33 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_80]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_80]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_80]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_80]
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:46) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
... 43 common frames omitted
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at generic.models.GenericModel.<init>(GenericModel.java:51) ~[na:na]
at myInsurance.models.Insurance.<init>(Insurance.java:151) ~[na:na]
... 48 common frames omitted
If it somehow helps, here is my previous post about Inheritance with Generics, which draws me to this issue.
Perhaps there is some issue with my contructors. I dont know, maybe I Should somewhere declare parameter typed constructor?
Please help me.
EDIT- Working solution
@MappedSuperclass
public abstract class GenericModel<T extends GenericModel> {
/**
* Works for nested/inner classes and Typed Parametered Classes
*/
public GenericModel() {
Class obtainedClass = getClass();
Type genericSuperclass = null;
for(;;) {
genericSuperclass = obtainedClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType) {
break;
}
obtainedClass = obtainedClass.getSuperclass();
}
ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
try {
entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
} catch (ClassCastException e) {
entityClass = guessEntityClassFromTypeParametersClassTypedArgument();
}
}
}
/**
* Use in case of extending GenericModel<ExtendingClass<TypeArgument>>
* Use if passed into GenericModel TypeParameter which isn't actual class, but Class with TypedParameter
* @return
*/
public Class<T> guessEntityClassFromTypeParametersClassTypedArgument() {
return ClassUtil.getActualTypeBinding(getClass(), GenericModel.class, 0);
}
package org.fastnate.data.util;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
/**
* Helper for inspection of classes.
*
* @author Tobias Liefke
*/
public final class ClassUtil {
private static <I> Type getActualTypeArgument(final Class<? extends I> instanceClass, final Class<I> superClass,
final int argumentIndex) {
final List<Type> parents = new ArrayList<>();
parents.add(instanceClass.getGenericSuperclass());
parents.addAll(Arrays.asList(instanceClass.getGenericInterfaces()));
for (final Type parentType : parents) {
final Class<?> parentClass = parentType instanceof ParameterizedType ? (Class<?>) ((ParameterizedType) parentType)
.getRawType() : (Class<?>) parentType;
// First check if we found the super class or interface
if (superClass.equals(parentClass)) {
// We found the requested super class - use the binding
return ((ParameterizedType) parentType).getActualTypeArguments()[argumentIndex];
} else if (parentClass != null && superClass.isAssignableFrom(parentClass)) {
// Else step up
final Type type = getActualTypeArgument(parentClass.asSubclass(superClass), superClass, argumentIndex);
if (type instanceof Class) {
return type;
} else if (type instanceof TypeVariable) {
return ((ParameterizedType) parentType).getActualTypeArguments()[Arrays.asList(
parentClass.getTypeParameters()).indexOf(type)];
}
}
}
return null;
}
/**
* Resolves the actual binding of a generic type that was specified in a superclass and bound in a subclass.
*
* @param instanceClass
* the implementing class
* @param superClass
* the superclass or interface which specifies the generic type variable
* @param argumentIndex
* the index of the type variable in the superclass definition (0 = the first type variable)
* @return the bound class for the variable in instanceClass
*/
public static <T, I> Class<T> getActualTypeBinding(final Class<? extends I> instanceClass,
final Class<I> superClass, final int argumentIndex) {
Type type = getActualTypeArgument(instanceClass, superClass, argumentIndex);
if (type instanceof TypeVariable<?>) {
type = ((TypeVariable<?>) type).getBounds()[0];
}
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getRawType();
}
if (type instanceof Class<?>) {
return (Class<T>) type;
}
throw new NoSuchElementException("Can't find binding for the " + argumentIndex + ". argument of " + superClass
+ " in " + instanceClass);
}
private ClassUtil() {
// Helper class
}
}
解决方案 This has nothing to do with JPA.
If you look at the stacktrace:
Caused by: java.lang.ClassCastException:
sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at generic.models.GenericModel.<init>(GenericModel.java:51) ~[na:na]
And it references this line:
entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
The actual type argument of Insurance
ist not an explicit class, but a type variable. Sou you can't cast it to a class.
If you would like to find the actual binding for a parameter of your class - I've created that for Fastnate: ClassUtil.getActualTypeBinding:
ClassUtil.getActualTypeBinding(getClass(), GenericModel.class, 0);
这篇关于具有泛型和多级继承的JPA。设置单表策略。 org.hibernate.MappingException:无法获得构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!