Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Cascading persist with embedded not nullable relation
PostPosted: Wed Mar 21, 2012 5:20 pm 
Newbie

Joined: Wed Mar 21, 2012 4:55 pm
Posts: 1
hello,

I'm confusing about Hibernate behavior and JPA spec.
please tell me below behavior that due to JPA spec or not.

Entity Classes:

Code:
@Entity
public class NewEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Embedded
    private NewEntityEE neee = new NewEntityEE();
}


Code:
@Embeddable
public class NewEntityEE implements Serializable {

    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(nullable = false)
    private NewEntity3 ne3 = new NewEntity3();
}


Code:
@Entity
public class NewEntity3 implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}


the Service class:

Code:
@Stateless
public class NewEJB {
    @PersistenceContext
    private EntityManager em;

    public NewEntity create(){
        NewEntity ne = new NewEntity();
        em.persist(ne);
        return ne;
    }
}


invoke NewEJB#create(), exception occured:

Code:
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
   Unsaved transient entity: ([entity.NewEntity3#<null>])
   Dependent entities: ([[entity.NewEntity#<null>]])
   Non-nullable association(s): ([entity.NewEntity.ne3])
警告: EJB5184:A system exception occurred during an invocation on EJB NewEJB, method: public entity.NewEntity ejb.NewEJB.create()
警告: javax.ejb.EJBException
   at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215)
   at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113)
   at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901)
   at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)
   at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
   at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
   at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
   at $Proxy286.create(Unknown Source)
   at ejb.__EJB31_Generated__NewEJB__Intf____Bean__.create(Unknown Source)
   at servlet.NewServlet.processRequest(NewServlet.java:53)
   at servlet.NewServlet.doGet(NewServlet.java:72)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
   at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
   at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
   at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
   at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
   at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
   at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
   at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
   at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
   at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: entity.NewEntity.ne3 -> entity.NewEntity3
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1357)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1294)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:860)
   at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:269)
   at ejb.NewEJB.create(NewEJB.java:15)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
   at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
   at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
   at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
   at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
   at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
   at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
   at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
   at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
   ... 31 more
Caused by: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: entity.NewEntity.ne3 -> entity.NewEntity3
   at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:135)
   at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:240)
   at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:622)
   at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:759)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:732)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:736)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:854)
   ... 55 more


in addition, when i remove annotation "@JoinColumn(nullable = false)" of NewEntityEE, it worked.

or make relation hierarchy to direct like NewEntity -> NewEntity3
with annotation "@JoinColumn(nullable = false)", it worked.

environment:
- Hibernate 4.1.1
- GlassFish 3.1.2
- Sun JDK 1.7.0_03

thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.