gpt4 book ai didi

java - Caused by : java. lang.ClassCastException : java. lang.Class无法转换为java.lang.reflect.ParameterizedType

转载 作者:行者123 更新时间:2023-11-30 08:59:35 25 4
gpt4 key购买 nike

我是 Java 反射的新手。

我查了很多论坛,但我还没有找到可行的解决方案。

错误:

Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

当我试图从数据库中获取对象时出现:

        new Unit().getBy(3L);

方法声明

@MappedSuperclass
public abstract class Generic<T extends Generic> {
@Transient
public Class<T> entityClass;
Generic() {
entityClass = ((Class) ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long id;

public T getBy(Long id) {
return JPA.em().find(entityClass, id);
}

二级扩展

@MappedSuperclass
public abstract class GenericDictionary<T extends Generic<T>> extends Generic<T> {

@Required
public String name;
@Required
public boolean active = true;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean getActive() {
return active;
}

public void setActive(boolean stat) {
this.active = stat;
}

实体类:

@Entity
@Table(name="common__Unit")
public class Unit extends GenericDictionary<Unit> {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="parent_id")
public Unit parent;
}

Controller 中的方法调用:

new Unit().getBy(3L);

这很奇怪,因为我有其他实体,我可以在其中使用该方法而不会出现像这样的任何运行时错误。这是那个实体:

@Entity
@Table(name="common__Clinic")
public class Clinic extends GenericDictionary<Clinic> {
@ManyToMany(cascade = CascadeType.PERSIST)
public List<Unit> unit;

以及其他 Controller 中的方法调用:

    Clinic clinic = new Clinic().getBy(3L);

此代码有效。我看不出该模型和 Controller 之间有任何区别。

谁能帮我指出这里的错误?

---------------------------------------- 编辑 ------ --------------------------------

完整堆栈跟踪

[error] play - Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.HibernateException: Javassist Enhancement failed: models.Unit
[error] application -

! @6kb72hhpo - Internal server error, for (POST) [/SupraADMIN/oddzialy/zapisz] ->

play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.HibernateException: Javassist Enhancement failed: models.Unit]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.4.jar:2.2.4]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.4.jar:2.2.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.4.jar:2.2.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.4.jar:2.2.4]
at scala.Option.map(Option.scala:145) [scala-library.jar:na]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10-2.2.4.jar:2.2.4]
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Javassist Enhancement failed: models.Unit
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:838) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:781) ~[hibernate-entitymanager-4.2.0.CR1.jar:4.2.0.CR1]
at models.Generic.getBy(Generic.java:62) ~[na:na]
at controllers.admin.CMS.UnitsSubmit(CMS.java:477) ~[na:na]
at admin.Routes$$anonfun$routes$1$$anonfun$applyOrElse$13$$anonfun$apply$13.apply(routes_routing.scala:317) ~[na:na]
Caused by: org.hibernate.HibernateException: Javassist Enhancement failed: models.Unit
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:143) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4396) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:332) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:258) ~[hibernate-core-4.2.0.CR1.jar:4.2.0.CR1]
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at models.Generic.<init>(Generic.java:40) ~[na:na]
at models.GenericDictionary.<init>(GenericDictionary.java:13) ~[na:na]
at models.Unit.<init>(Unit.java:30) ~[na:na]
at models.Unit_$$_jvst57e_b5.<init>(Unit_$$_jvst57e_b5.java) ~[na:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_65]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_65]
[error] application - REGUEST: POST /SupraADMIN/oddzialy/zapisz GENERATED ERROR: @6kb72hhpo: Execution exception in /home/user/Aplikacje/Eclipse/SVP/modules/common/app/models/Generic.java:62

最佳答案

这是解决问题的通用模型的构造函数:

@MappedSuperclass
public abstract class Generic<T extends Generic> {
@Transient protected Class<T> entityClass;
public Generic() {
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]));
}

关于java - Caused by : java. lang.ClassCastException : java. lang.Class无法转换为java.lang.reflect.ParameterizedType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27167225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com