Hibernate Envers - 无法将DefaultRevisionEntity强制转换为javassist.util.proxy.Proxy

时间:2015-12-11 20:47:37

标签: java hibernate jpa java-8 hibernate-envers

我的配置:

  1. AS:Weblogic 12 c
  2. Java 8
  3. Hibernate Core 5.0.5.Final
  4. Hibernate Envers 5.0.5.Final
  5. 我正在使用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),包括javassist。

    所以我把它添加到我耳中的weblogic-application.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 个答案:

答案 0 :(得分:1)

我首先要确保你在类路径上有正确版本的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;
  }
}