即使级联为ALL

时间:2019-01-09 11:41:32

标签: java hibernate jpa

我在一个AuthorizationPosition EE项目中有两个实体ProductAttributesWildfly。两者之间的映射如下:

授权位置:

  @javax.persistence.ManyToOne(fetch =  javax.persistence.FetchType.LAZY , optional=false, cascade = {javax.persistence.CascadeType.ALL})
  @javax.persistence.JoinColumn(name = "PRODUCT_ATTRIBUTES_ID", referencedColumnName="PRODUCT_ATTRIBUTES_ID")
  protected org.example.persistence.entity.ProductAttributes productAttributes;

产品属性:

  @javax.persistence.OneToMany(
        targetEntity=org.example.persistence.entity.AuthorizationPosition.class,
        mappedBy="productAttributes",
        fetch=javax.persistence.FetchType.LAZY
                    , cascade = javax.persistence.CascadeType.ALL
                 , orphanRemoval = true         )
  protected java.util.Set<org.example.persistence.entity.AuthorizationPosition> authorizationPositionsByProductAttributes;

我在两个实体中都设置了所有关系,并在em.persist()上调用ProductAttributes(作为较大结构的一部分),这是出现奇怪错误的地方(在末尾有完整的堆栈跟踪问题):

java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : org.example.persistence.entity.AuthorizationPosition.productAttributes -> org.example.persistence.entity.ProductAttributes

我是否缺少某些东西,或者休眠(hibernate-core-5.3.1.Final)确实试图以其他方式持续存在?尽管CascadeType.ALL出现在所有关系映射中,这应该不是问题吗?

完整的堆栈跟踪:

java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : org.example.persistence.entity.AuthorizationPosition.productAttributes -> org.example.persistence.entity.ProductAttributes
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:146) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:814) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:785) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java:580) [wildfly-jpa-13.0.0.Final.jar:13.0.0.Final]
    at org.example.control.ImportServoceImpl.import(ImportServoceImpl.java:109) [service-SNAPSHOT.jar:]
    at org.example.rest.control.ImportContainerService.importDocument(ImportContainerService.java:65) [classes:]
    at org.example.rest.control.ImportContainerService$Proxy$_$$_WeldSubclass.importDocument$$super(Unknown Source) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_191]
    at org.jboss.weld.interceptor.proxy.TerminalAroundInvokeInvocationContext.proceedInternal(TerminalAroundInvokeInvocationContext.java:51) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:78) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.hibernate.validator.cdi.internal.interceptor.ValidationInterceptor.validateMethodInvocation(ValidationInterceptor.java:79) [hibernate-validator-cdi-6.0.10.Final.jar:6.0.10.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_191]
    at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:73) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeAroundInvoke(InterceptorMethodHandler.java:84) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:72) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:56) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:79) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:68) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.example.rest.control.ImportContainerService$Proxy$_$$_WeldSubclass.importDocument(Unknown Source) [classes:]
    at org.example.rest.boundary.ContainerRestService.import(ContainerRestService.java:181) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_191]
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509)
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:92) [wildfly-weld-ejb-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.interceptorChainCompleted(WeldInvocationContextImpl.java:107) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.proceed(WeldInvocationContextImpl.java:126) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.hibernate.validator.cdi.internal.interceptor.ValidationInterceptor.validateMethodInvocation(ValidationInterceptor.java:79) [hibernate-validator-cdi-6.0.10.Final.jar:6.0.10.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_191]
    at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:73) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.invokeNext(WeldInvocationContextImpl.java:92) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.interceptor.proxy.WeldInvocationContextImpl.proceed(WeldInvocationContextImpl.java:124) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.weld.bean.InterceptorImpl.intercept(InterceptorImpl.java:105) [weld-core-impl-3.0.4.Final.jar:3.0.4.Final]
    at org.jboss.as.weld.ejb.DelegatingInterceptorInvocationContext.proceed(DelegatingInterceptorInvocationContext.java:82) [wildfly-weld-ejb-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.as.weld.interceptors.EjbComponentInterceptorSupport.delegateInterception(EjbComponentInterceptorSupport.java:60)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:76)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:88)
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:101)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43) [wildfly-ejb3-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47) [wildfly-jpa-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) [wildfly-ee-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:40)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51) [wildfly-ejb3-13.0.0.Final.jar:13.0.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:237) [wildfly-ejb3-13.0.0.Final.jar:13.0.0.Final]
    ... 104 more
Caused by: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : org.example.persistence.entity.AuthorizationPosition.productAttributes -> org.example.persistence.entity.ProductAttributes
    at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:122) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:436) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:648) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) [hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    ... 168 more

持久性单元属性:

   <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="hibernate.connection.isolation" value="READ_COMMITTED"/>
        <property name="hibernate.listeners.envers.autoRegister" value="false"/>
        <!--<property name="hibernate.globally_quoted_identifiers" value="true"/>-->
        <!-- note: no DDL validation, because it kicks in before Liquibase gets a chance to update the schema
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        -->

        <!-- hibernate version integrated in Wildfly states:
        Legacy Hibernate behavior was to ignore the @DiscriminatorColumn.
        However, as part of issue HHH-6911 we now apply the explicit @DiscriminatorColumn.
        If you would prefer the legacy behavior, enable the `hibernate.discriminator.ignore_explicit_for_joined`
        setting (hibernate.discriminator.ignore_explicit_for_joined=true) -->
        <property name="hibernate.discriminator.ignore_explicit_for_joined" value="true"/>

        <property name="hibernate.enhancer.enableDirtyTracking" value="true"/>
        <property name="hibernate.enhancer.enableLazyInitialization" value="false"/>
        <property name="hibernate.enhancer.enableAssociationManagement" value="false"/>
        <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>

        <property name="hibernate.show_sql" value="${hibernate.showsql}"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments" value="true"/>

        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.cache.use_query_cache" value="true"/>
        <property name="hibernate.cache.use_minimal_puts" value="true"/>
        <property name="hibernate.cache.use_structured_entries" value="true"/>

        <property name="hibernate.default_batch_fetch_size" value="64"/>
        <property name="hibernate.jdbc.batch_size" value="100"/>
        <property name="hibernate.jdbc.fetch_size" value="100"/>
        <property name="hibernate.jdbc.batch_versioned_data" value="false"/>
        <property name="hibernate.order_inserts" value="true"/>
        <property name="hibernate.order_updates" value="true"/>

        <property name="hibernate.generate_statistics" value="false"/>

        <property name="hibernate.connection.useUnicode" value="true"/>
        <property name="hibernate.connection.characterEncoding" value="UTF-8"/>
        <property name="hibernate.jdbc.time_zone" value="UTC"/>
    </properties>

Edit1:

我在两个实体上都设置了关系。我使用与以下类似的代码(为清晰起见,删除了某些部分):

public void addAuthorizationPositionByProductAttributes(@NotNull AuthorizationPosition authorizationPositionByProductAttributes) {
   if(this.authorizationPositionsByProductAttributes.add(authorizationPositionByProductAttributes)){
      authorizationPositionByProductAttributes.setProductAttributes(this);
   }
}

当我在调试中检查它时,这些值已正确设置。

2 个答案:

答案 0 :(得分:1)

我有点惊讶。我们尝试了所有建议:

  • @mrtna remove Cascade.ALL from the ProductAttributes -> AuthorizationPosition
  • @JonathanJohx remove orphanRemoval = true , referencedColumnName="PRODUCT_ATTRIBUTES_ID") optional=false,
  • @TiMr remove optional and add nullable=false to the join column and change lazy to Eager
  • @alekz same AuthorizationPosition entity is added to two ProductAttributes?

但是似乎没有任何效果,有些导致空约束验证问题(ORA-01400),而另一些行为完全相同。

虽然测试性能我们在persistence.xml中完全禁用了验证(用于比较测试):

<validation-mode>NONE</validation-mode>

由于意外地错误地上传了错误的文件-令我惊讶的是-该结构保持了良好的状态,没有任何错误或约束违规...

TL.DR。: 。它是验证器中的休眠错误,阻止了我们继续操作。如果validation-mode设置为NONE,则该结构得以保留。我们现在检查em.persist(..)调用之前的验证信息,并据此评估结果。

答案 1 :(得分:-1)

您是否尝试从AuthorizationPosition中删除optional=false

有时候,Hibernate无法将事情摆在“正确”的顺序上。

Hibernate有时需要在外键中存储NULL,即使有避免它的理论方法也是如此。在这种情况下,您必须删除非空约束。 (我从外键中删除了所有非空约束...)

您可以尝试从AuthorizationPosition中删除级联,这可能是订购问题的原因。但是我想它不会改变任何东西。