当前位置: 首页 > 知识库问答 >
问题:

Hibernate Envers-DefaultRevsionEntity不能强制转换为javassist.util.proxy.Proxy

鲁泰宁
2023-03-14

我的配置:

  1. AS:Weblogic 12 c
  2. Java8
  3. Hibernate Core5.0.5.Final
  4. Hibernate Envers5.0.5.Final

我正在使用Hibernate Envers的基本配置,我可以看到我的审计表被正确填充,并且在插入/更新/删除时也可以看到“revinfo”表。

问题是当我咨询一些实体/修订时,错误发生了。

我的查询:

    @PersistenceContext(unitName = "pu")
    protected EntityManager em;


    @SuppressWarnings("rawtypes")
    public List<Object[]> list(Class classe){
        AuditQuery query = getAuditReader().createQuery()
                .forRevisionsOfEntity(classe, false, true);

//Error occurs here:
        Number revision = (Number) query
                .getSingleResult();

        System.out.println(revision);

        List<Object[]> list = null;
        return list;
    }

    private AuditReader getAuditReader() {
        AuditReader reader = AuditReaderFactory.get(em);
        return reader;
    }

错误:

javax.ejb.EJBException: EJB Exception: : org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: org.hibernate.envers.DefaultRevisionEntity
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:130)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4461)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:336)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:257)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:137)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1022)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:632)
    at org.hibernate.type.EntityType.resolve(EntityType.java:424)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:687)
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:848)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:930)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
    at org.hibernate.loader.Loader.doList(Loader.java:2611)
    at org.hibernate.loader.Loader.doList(Loader.java:2594)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
    at org.hibernate.loader.Loader.list(Loader.java:2418)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
Caused by: java.lang.ClassCastException: org.hibernate.envers.DefaultRevisionEntity_$$_javassist_153 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:123)
    ... 129 more
    at weblogic.ejb.container.internal.EJBRuntimeUtils.throwEJBException(EJBRuntimeUtils.java:88)
    at weblogic.ejb.container.internal.BaseLocalObject.handleSystemException(BaseLocalObject.java:503)
    at weblogic.ejb.container.internal.BaseLocalObject.handleSystemException(BaseLocalObject.java:446)
    at weblogic.ejb.container.internal.BaseLocalObject.postInvoke1(BaseLocalObject.java:251)
    at weblogic.ejb.container.internal.BaseLocalObject.__WL_postInvokeTxRetry(BaseLocalObject.java:204)
    Truncated. see log file for complete stacktrace
Caused By: org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: org.hibernate.envers.DefaultRevisionEntity
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:130)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4461)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:336)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:257)
    Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassCastException: org.hibernate.envers.DefaultRevisionEntity_$$_javassist_153 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:123)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:666)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4461)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:336)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:257)
    Truncated. see log file for complete stacktrace

编辑:。我可以在Weblogic CAT中看到有几个lib冲突(app lib vs weblogic lib),包括jav辅助。

所以我把它添加到weblogic应用程序中。xml在我耳边:

<prefer-application-packages>
    <package-name>antlr.*</package-name>
    <package-name>javassist.*</package-name>
</prefer-application-packages>

现在我得到了一个新的不同的例外,但现在当Weblogic开始时,这个例外就消失了:

weblogic.management.DeploymentException: java.lang.ClassCastException: org.jboss.weld.security.Principal$1127466021$Proxy$_$$_Weld$Proxy$ cannot be cast to javassist.util.proxy.ProxyObject
    at weblogic.application.internal.BaseDeployment.throwAppException(BaseDeployment.java:123)
    at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:260)
    at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:61)
    at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
    at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:80)
    Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassCastException: org.jboss.weld.security.Principal$1127466021$Proxy$_$$_Weld$Proxy$ cannot be cast to javassist.util.proxy.ProxyObject
    at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:249)
    at org.jboss.weld.bean.builtin.ee.AbstractEEBean.<init>(AbstractEEBean.java:43)
    at org.jboss.weld.bean.builtin.ee.PrincipalBean.<init>(PrincipalBean.java:52)
    at org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:193)
    at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:349)
    Truncated. see log file for complete stacktrace

知道发生了什么吗?

共有1个答案

施令秋
2023-03-14

我首先要确保在类路径中有正确的Javassist版本,并且不会碰巧有多个版本。过去,当我有冲突的版本时,我也见过类似的问题。

对于Hibernate 5.0.5,它应该是javassist-3.18.1-GA.jar。

这是我的审计历史存储库实现的示例,它返回按给定实体类型和实体标识符的修订号排序的实体修订列表作为参考。

public class AuditHistoryRepository<T, ID extends Serializable> {

  @PersistenceContext 
  private EntityManager entityManager;
  private Class<T> entityClazz;

  public List<T> getEntityRevisionsById(ID id) {
    List<T> results = new ArrayList<>();
    // read audit history
    AuditReader reader = AuditReaderFactory.get(entityManager);
    AuditQuery query = reader.createQuery()
      .forRevisionsOfEntity(entityClazz, false, true)
      .add(AuditEntity.id().eq(id))
      .addOrder(AuditEntity.revisionNumber().asc());
    // get results
    for(Object row : query.getResultList()) {
      if(row instanceof Object[]) {
        Object[] array = (Object[])row;
        T entity = (T) array[0];
        results.add(entity);
      }
    }
    return results;
  }
}
 类似资料: