调用原始JPA方法抛出WrongClassException

时间:2014-02-07 19:56:04

标签: java spring hibernate jpa spring-roo

 public static UserDetail UserDetail.findUserDetail(Long id) {
        if (id == null) return null;
        return entityManager().find(UserDetail.class, id);
    }

我们正在使用spring Roo。以上是Roo生成的finder方法。部分堆栈跟踪如下:

Caused by: org.hibernate.WrongClassException: Object with id: 1501237 was not of the specified subclass: com.***.***.user.UserDetail (Discriminator: FacebookUserDetail)

有没有人遇到这个例外?

修改 这个问题和以下问题与同一问题有关。

Java class file truncated

我有两个项目。我的一个项目(比如project2)依赖于另一个项目(project2)。这两个项目都是maven项目,project1列在project2的依赖项中。当我编译project2时,应将project1中的所有类文件复制到project2(我想象)。但是,我看到project1中某个类文件的文件大小与project2中同一个类的类文件的文件大小不同。如果我反编译文件,我会得到以下结果。

从project1:

中反编译了FacebookUserDetail.class
package com.***.domain.user.external;

import com.***.domain.user.UserDetailType;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.TypedQuery;
import org.aspectj.lang.JoinPoint;
import org.aspectj.runtime.internal.CFlowCounter;
import org.aspectj.runtime.reflect.Factory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.aspectj.AbstractDependencyInjectionAspect;
import org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect;
import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl;

@Configurable
@Entity
public class FacebookUserDetail extends ExternalUserDetail
{
  public FacebookUserDetail()
  {
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_1, this, this); if ((!AnnotationBeanConfigurerAspect.ajc$if$bb0((Configurable)getClass().getAnnotation(Configurable.class))) && (AbstractDependencyInjectionAspect.ajc$if$6f1(localJoinPoint))) AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);
  }

  public static FacebookUserDetail findFacebookUserDetailByFacebookId(String facebookId)
  {
    String str = facebookId; JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_0, null, null, str); if ((AnnotationDrivenStaticEntityMockingControl.ajc$cflowCounter$1.isValid()) && (AnnotationDrivenStaticEntityMockingControl.hasAspect())) return (FacebookUserDetail)findFacebookUserDetailByFacebookId_aroundBody1$advice(str, localJoinPoint, AnnotationDrivenStaticEntityMockingControl.aspectOf(), null, ajc$tjp_0, localJoinPoint); return findFacebookUserDetailByFacebookId_aroundBody0(str, localJoinPoint);
  }

  public UserDetailType getExternalUserDetailType()
  {
    return UserDetailType.FACEBOOK;
  }

  static
  {
    ajc$preClinit(); } 
  public static long countFacebookUserDetails() { return FacebookUserDetail_Roo_Entity.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_Entity$com_nim_domain_user_external_FacebookUserDetail$countFacebookUserDetails(); } 
  public static List<FacebookUserDetail> findAllFacebookUserDetails() { return FacebookUserDetail_Roo_Entity.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_Entity$com_nim_domain_user_external_FacebookUserDetail$findAllFacebookUserDetails(); } 
  public static FacebookUserDetail findFacebookUserDetail(Long paramLong) { return FacebookUserDetail_Roo_Entity.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_Entity$com_nim_domain_user_external_FacebookUserDetail$findFacebookUserDetail(paramLong); } 
  public static List<FacebookUserDetail> findFacebookUserDetailEntries(int paramInt1, int paramInt2) { return FacebookUserDetail_Roo_Entity.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_Entity$com_nim_domain_user_external_FacebookUserDetail$findFacebookUserDetailEntries(paramInt1, paramInt2); } 
  public static TypedQuery<FacebookUserDetail> findFacebookUserDetailsByUserIdEquals(String paramString) { return FacebookUserDetail_Roo_Finder.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_Finder$com_nim_domain_user_external_FacebookUserDetail$findFacebookUserDetailsByUserIdEquals(paramString); } 
  public String toString() { return FacebookUserDetail_Roo_ToString.ajc$interMethod$com_nim_domain_user_external_FacebookUserDetail_Roo_ToString$com_nim_domain_user_external_FacebookUserDetail$toString(this); }

}

从project2

中反编译了FacebookUserDetail.class
package com.***.domain.user.external;

import com.***.domain.user.UserDetailType;
import org.aspectj.lang.JoinPoint;
import org.aspectj.runtime.internal.CFlowCounter;
import org.aspectj.runtime.reflect.Factory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.aspectj.AbstractDependencyInjectionAspect;
import org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect;
import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl;

public class FacebookUserDetail extends ExternalUserDetail
{
  public FacebookUserDetail()
  {
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_1, this, this); if ((!AnnotationBeanConfigurerAspect.ajc$if$bb0((Configurable)getClass().getAnnotation(Configurable.class))) && (AbstractDependencyInjectionAspect.ajc$if$6f1(localJoinPoint))) AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);
  }

  public static FacebookUserDetail findFacebookUserDetailByFacebookId(String facebookId)
  {
    String str = facebookId; JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_0, null, null, str); if ((AnnotationDrivenStaticEntityMockingControl.ajc$cflowCounter$1.isValid()) && (AnnotationDrivenStaticEntityMockingControl.hasAspect())) return (FacebookUserDetail)findFacebookUserDetailByFacebookId_aroundBody1$advice(str, localJoinPoint, AnnotationDrivenStaticEntityMockingControl.aspectOf(), null, ajc$tjp_0, localJoinPoint); return findFacebookUserDetailByFacebookId_aroundBody0(str, localJoinPoint);
  }

  public UserDetailType getExternalUserDetailType()
  {
    return UserDetailType.FACEBOOK;
  }

  static
  {
    ajc$preClinit();
  }
}

我的问题是:在project2中截断类文件的可能原因是什么?

3 个答案:

答案 0 :(得分:1)

据我所知,从错误中您可以看到以下情况: 您请求具有该ID的UserDetail类型的实体(其DTYPE / discriminator列值应等于FacebookUserDetail或其他扩展UserDetail),但在您的数据库中,DTYPE是另一个。您必须更正您的数据库。

或者也可能是FacebookUserDetail未被识别为同一层次结构的DTYPE。尝试调试一下,例如,如果搜索具有相同ID的FacebookUserDetail实例,则测试返回的内容。

答案 1 :(得分:0)

看起来您的超级类和子类在数据库中没有为请求的记录共享相同的ID 1501237

答案 2 :(得分:0)

很明显,你有遗传问题,请看http://en.wikibooks.org/wiki/Java_Persistence/Inheritance