Spring JPA引发“未知实体”异常

时间:2016-12-05 04:19:50

标签: java-8 spring-data-jpa

Jdk 1.8.0_82,Springboot 1.3.8和JPA。

我想插入由现有对象值构成的新行。

我打电话

List<LogSchemaFieldModel> newFields = Lists.newArrayList();
for(LogSchemaFieldModel f : fields){
newFields.add(new LogSchemaFieldModel(){{
                        setFieldName( f.getFieldName() );
                        setFieldType( f.getFieldType() );
                        setFieldOpt( Field.Mode.NULLABLE.toString() );
                        setDescription( f.getDescription() );
                        setSampleValue( f.getSampleValue() );
                        setCommon(true);
                        setRequired(null);
                    }});
}
repo.save(newFields);

但它会抛出此异常。

org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: com......service.LogDefineService$6; nested exception is java.lang.IllegalArgumentException: Unknown entity: com......service.LogDefineService$6
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) ~[spring-orm-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) ~[spring-data-jpa-1.9.5.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.8.RELEASE.jar:4.2.8.RELEASE]
    at com.sun.proxy.$Proxy119.save(Unknown Source) ~[na:na]
    at com.my...

1 个答案:

答案 0 :(得分:3)

最后,我找到了解决方案。

删除双括号初始化。

List<LogSchemaFieldModel> newFields = Lists.newArrayList();
for(LogSchemaFieldModel f : fields){
LogSchemaFieldModel nf = new LogSchemaFieldModel();
                nf.setFieldName( f.getFieldName() );
                nf.setFieldType( f.getFieldType() );
                nf.setFieldOpt( Field.Mode.NULLABLE.toString() );
                nf.setDescription( f.getDescription() );
                nf.setSampleValue( f.getSampleValue() );
                nf.setCommon(true);
                nf.setRequired(null);
newFields.add(nf);
}
repo.save(newFields);

但我不知道为什么会这样。 是java8双支撑初始化bug吗?