Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate fails to persist even when cascade is ALL

I have two entities AuthorizationPosition and ProductAttributes in a Wildfly EE project. The mapping between the two is the following:

AuthorizationPosition:

  @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;

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;

I set all the relation in both of the entities and call em.persist() on ProductAttributes (as part of a bigger structure) this is where the strange error appears (full stack trace at the end of the question):

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

Am I missing something or Hibernate (hibernate-core-5.3.1.Final) is really trying to persist in an other way around? Should not this be a problem though as CascadeType.ALL is present on all of the relation mappings?

Full stacktrace:

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

Persistence unit properties:

   <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:

I set the relation on both of the entities. I use similar code to the following (some parts are removed due to clarity):

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

When I check it in debug, the values are set correctly.

like image 420
Hash Avatar asked Jan 09 '19 11:01

Hash


1 Answers

I am kind of surprised. We tried all of the suggestions:

  • @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?

But nothing seemed to work some caused null constraint validation problems (ORA-01400) others were behaving exactly the same.

While testing performance we disabled validation completely (for a comparison test) in persistence.xml:

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

And with and accidental missclick on the wrong file to upload - to my surprise - the structure persisted fine without having any errors or constraint violations...

TL.DR.: it is a hibernate bug in the validator which prevented us to persist. In case the validation-mode was set to NONE the structure got persisted. We check the validation information now previous to the em.persist(..) call and evaluate the results accordingly.

like image 85
Hash Avatar answered Oct 22 '22 00:10

Hash