Spring Data JPA OneToMany和ManyToOne给了我JpaSystemException

时间:2014-03-31 12:30:42

标签: spring jpa

我有两张桌子:PARKKPILEVEL1和PARKKPILEVEL2 PARKKPILEVEL2包含一个FK - PARKKPILEVEL1_ID到PARKKPILEVEL1

CREATE TABLE `PARKKPILEVEL1` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(64) NOT NULL,
  `DESCRIPTION` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

CREATE TABLE `PARKKPILEVEL2` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(64) NOT NULL,
  `DESCRIPTION` varchar(256) DEFAULT NULL,
  `PARKKPILEVEL1_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `PARKKPILEVEL1_idx` (`PARKKPILEVEL1_ID`),
  CONSTRAINT `PARKKPILEVEL1` FOREIGN KEY (`PARKKPILEVEL1_ID`) REFERENCES `PARKKPILEVEL1` (`ID`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

以下是PARKKPILEVEL1和PARKKPILEVEL2的域类

@Entity
@Table(name = "PARKKPILEVEL1")
public class ParkKPILevel1 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String description;
    @OneToMany(mappedBy = "parkKPILevel1", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<ParkKPILevel2> parkKPILevel2s = new ArrayList<ParkKPILevel2>();
}

@Entity
@Table(name = "PARKKPILEVEL2")
public class ParkKPILevel2 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String description;
    @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
    private ParkKPILevel1 parkKPILevel1;
}

我创建了OneToMany和ManyToOne关系

但是当我尝试查找服务中的所有PARKKPILEVEL1行时,发生以下异常:

org.springframework.orm.jpa.JpaSystemException: org.apache.openjpa.util.IntId cannot be cast to com.company.domain.ParkKPILevel1; nested exception is <openjpa-2.2.1-r422266:1396819 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: org.apache.openjpa.util.IntId cannot be cast to com.company.domain.ParkKPILevel1
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:321)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:404)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy34.findAll(Unknown Source)
    at com.company.service.impl.ParkKPIServiceImpl.findParkKPILevel1(ParkKPIServiceImpl.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy37.findParkKPILevel1(Unknown Source)
    at com.company.service.TestParkKPIService.findParkKPILevel1(TestParkKPIService.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: org.apache.openjpa.util.IntId cannot be cast to com.company.domain.ParkKPILevel1
    at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:1019)
    at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:918)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1036)
    at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
    at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2381)
    at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.loadElement(RelationToManyInverseKeyFieldStrategy.java:90)
    at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.loadElement(RelationCollectionInverseKeyFieldStrategy.java:76)
    at org.apache.openjpa.jdbc.meta.strats.StoreCollectionFieldStrategy.processEagerParallelResult(StoreCollectionFieldStrategy.java:313)
    at org.apache.openjpa.jdbc.meta.strats.StoreCollectionFieldStrategy.loadEagerParallel(StoreCollectionFieldStrategy.java:247)
    at org.apache.openjpa.jdbc.meta.FieldMapping.loadEagerParallel(FieldMapping.java:916)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1109)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1062)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:409)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:304)
    at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)
    at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
    at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1038)
    at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:996)
    at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:918)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1036)
    at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
    at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2381)
    at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:274)
    at org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:60)
    at org.apache.openjpa.kernel.QueryImpl$PackingResultObjectProvider.getResultObject(QueryImpl.java:2075)
    at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
    at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1251)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
    at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
    at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:286)
    at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:277)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    ... 44 more
Caused by: java.lang.ClassCastException: org.apache.openjpa.util.IntId cannot be cast to com.company.domain.ParkKPILevel1
    at com.company.domain.ParkKPILevel2.pcReplaceField(ParkKPILevel2.java)
    at org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:3223)
    at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(StateManagerImpl.java:2648)
    at org.apache.openjpa.kernel.StateManagerImpl.storeObject(StateManagerImpl.java:2638)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setMappedBy(JDBCStoreManager.java:476)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:402)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:304)
    at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)
    at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
    at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1038)
    at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:996)
    ... 87 more

1 个答案:

答案 0 :(得分:1)

据我所知,您使用的是OPEN_JPA,他们有与您的问题相关的未解决的错误。如果您的@OneToMany具有抓取类型FetchType.EAGER,那么您将看到

  

PersistenceException:LongId无法强制转换为MyEntity ...

以下是他们在Jira中的错误的链接: https://issues.apache.org/jira/browse/OPENJPA-2505

作为解决方法,他们建议避免“急切”并改为使用“加入提取”