Spring JPA:使用更新的子实体更新实体

Posted

技术标签:

【中文标题】Spring JPA:使用更新的子实体更新实体【英文标题】:Spring JPA : Update entity with updated child entities 【发布时间】:2019-02-09 01:34:28 【问题描述】:

使用的数据库:带有 Spring jpa 的 Postgres

我有一个主要实体,其结构如下:

Parent (CascadeType.All for childs)
-->Child 1 (Join column with parent here)
-->Child 2 (Join column with parent here)
-->Child 3 (Join column with parent here)
-->Child 4 (Join column with parent here)
-->Child 5 (Join column with parent here)

当我第一次在 db 中保存父实体时(子 2..5 为空),它保存成功。然后我更新 Child 2..5 并使用相同的对象进行保存操作,它给了我错误,它超过了 Postgres 的列大小为 1665。(这是因为孩子有嵌套的孩子)

我尝试通过删除 CascadeType.ALL 及其存储库对象来更新 Child 2 ..5,但它说 “对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例”。

请告知如何解决此问题。谢谢

public class Parent 

    @OneToOne(cascade = CascadeType.ALL)
    private Child1 child1;

    @OneToOne(cascade = CascadeType.ALL)
    private Child1 child2;

    @OneToOne(cascade = CascadeType.ALL)
    private Child1 child3;

    @OneToOne(cascade = CascadeType.ALL)
    private Child1 child4;

    @OneToOne(cascade = CascadeType.ALL)
    private Child1 child5;

    //getters setters


public class Child1 
    @OneToOne
    @JoinColumn(name = FKNAME)
    private Parent parent1;

    @OneToOne(cascade = CascadeType.ALL)
    private GrandChild1 child1;

    //getters setters   


public class GrandChild1 
    @OneToOne
    @JoinColumn(name = FKNAME)
    private Child1 child1;

    //getters setters   


parentRepository.save(parent);

例外:

2018-09-04 08:12:07 WARN  SqlExceptionHelper:127 [1111] - SQL Error: 0, SQLState: 54000
2018-09-04 08:12:07 ERROR SqlExceptionHelper:129 [1111] - ERROR: target lists can have at most 1664 entries
org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:503)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy136.saveAndFlush(Unknown Source)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.hibernate.exception.GenericJDBCException: could not extract ResultSet

【问题讨论】:

显示你的代码,你是如何保存父对象的? @AngadBansode :我正在使用 parentRepository.save(parent-obj) 保存到数据库中。 父子关系,我的意思是@OneToMany或其他 @AngadBansode :我已经更新了代码。 @JoinColumn 没用。删除它。 【参考方案1】:

Hibernate 尝试通过连接相关表并在 select 子句中包含所需的列来获取急切的 1-1 关系。

由于您有无限级联的 1-1 关系,这会在有限的计算机上造成问题。

通过将链中的至少一个元素声明为惰性来打破关系链,并且问题应该消失。

请注意(至少在遥远的过去)它并不总是很容易将其拉出来,但有一个 SO 问题应该会有所帮助:Making a OneToOne-relation lazy

【讨论】:

以上是关于Spring JPA:使用更新的子实体更新实体的主要内容,如果未能解决你的问题,请参考以下文章

Spring Data JPA 中使用Update Query更新实体类问题

如何在 Spring Data 中漂亮地更新 JPA 实体?

即使 JPA 实体不脏,我是不是可以强制 spring-data 更新可审计字段?

为啥使用 Spring Data JPA 更新实体时@Transactional 隔离级别不起作用?

当我不想更新实体时,Spring Data JPA 会更新它

Spring Data Jpa:保存方法仅返回选择,但不执行插入