Caused by: java.lang.ClassCastException: java.lang

2019-05-24 14:37发布

I am new in Java Reflection.

I checked a lot of forums, but I haven't found working solution.

The error:

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

comes when I am trying to get object from db:

        new Unit().getBy(3L);

the method declaration

@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);
    }

second level extension

@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 class:

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

And method call in controller:

new Unit().getBy(3L);

It is quite strange, cause I have other entity where I can use that method without any runtime error like this. Here is that entity:

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

And the method call in other controller:

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

This code works. I dont see any differences between that models and controllers.

Can any one help me pointing my mistakes here?

---------------------------------------- EDIT --------------------------------------

full stack trace

[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

3条回答
SAY GOODBYE
2楼-- · 2019-05-24 15:22

In your Generic class Please User this :

private Class<T> persistentClass;

@SuppressWarnings("unchecked")
public AbstractGenericDAOImpl() {
    this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

}
查看更多
▲ chillily
3楼-- · 2019-05-24 15:31

The getGenericSuperclass approach doesn't work when using a complex type hierarchy. Instead you might try using TypeTools:

Generic() {
    entityClass = (Class<T>)TypeResolver.resolveRawArgument(Generic.class, getClass());
}
查看更多
冷血范
4楼-- · 2019-05-24 15:35

Here is constructor for generic model that solves the issue:

@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]));
    }
查看更多
登录 后发表回答