-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Cascade doesn't work on child entity while doing persist()
PostPosted: Sun Jan 27, 2008 6:04 pm 
Newbie

Joined: Sun Jan 27, 2008 5:35 pm
Posts: 3
Hi,

I have a problem with cascading merging of child entities using the EntityManager. my entity hierarchy ir pretty simple - Hostel is parent which has a one-to-one with Address which in turn has a many-to-one with City. both of these relationships are marked with cascade.ALL

in this scenario City is always a detached entity

when the persist method executes it cascades down to the City entity, sees that it is detached and throws an exception.

what am I doing wrong? why isn't the cascade working the way I think it should. how do I manage this scenario (although I tried a bunch of other ways, none of them worked)?

Hibernate version: 3.2.5.ga + EntityManager 3.3.1 GA + Annotations 3.3.0 GA

Mapping documents:

Code:
@Entity
@Table(name="TO_HOSTEL")
public class Hostel implements Comparable, Serializable {
   
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "HOSTEL_ID")
   private Long id;
   
   @Column(name = "SERVICE")
   private String service;
   @Column(name = "CODE")
   private Integer code;
   @Column(name = "NAME")
   private String name;
   @Column(name = "DESCRIPTION")
   private String description;

   [b]@OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name="ADDRESS")[/b]

   private Address address;
   @Column(name = "MIN_RATE")
   private BigDecimal minRate;
   ...

@Entity
@Table(name="TO_ADDRESS")
public class Address implements Serializable {
   
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "ADDRESS_ID")
   private Long id;
   
   @Column(name = "STREET_ADDRESS")
   private String streetAddress;
   [b]@ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="CITY")
   private City city;[/b]
   @Column(name = "POSTAL_CODE")
   private String postalCode;
   @Column(name = "PROVINCE_CODE")
   private String provinceCode;
   @Column(name = "PROVINCE_NAME")
   private String province;
        ...


@Entity
@Table(name="TO_CITY")
public class City implements Serializable {
   
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "CITY_ID")
   private Long id;
   
   @Column(name = "CITY_NAME")
   private String name;
   @Column(name = "DEFAULT_NAME")
   private String defaultName;
   


Code between sessionFactory.openSession() and session.close():

Code:
EntityManager em = HibernateUtil.getEntityManager();
      EntityTransaction et = em.getTransaction();
      boolean transactionWasActive = et.isActive();
      
      if(!transactionWasActive)
         et.begin();
      
      try {
         
         em.persist(hostel);
         //em.merge(hostel.getAddress().getCity());
         em.flush();
         
      } finally {
         if(!transactionWasActive)
            et.commit();
      }


Full stack trace of any exception that occurs:

javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:51)
at lt.travelonline.dao.HostelDAO.create(HostelDAO.java:40)
at lt.travelonline.hostel.hostelclub.HostelClubService.search(HostelClubService.java:924)
at lt.travelonline.hostel.HostelService.findHostels(HostelService.java:87)
at lt.travelonline.hostel.presentation.HostelSearch.searchHostels(HostelSearch.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.el.parser.AstValue.invoke(Unknown Source)
at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:613)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 6:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think the behavior is as expected. You cannot use persist on a detached entity. You have to use merge.

http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/#d0e880


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 2:38 pm 
Newbie

Joined: Sun Jan 27, 2008 5:35 pm
Posts: 3
Yes, thanks, I am aware of this technique, it's just that I hoped that specifying cascadeType.ALL would solve this for me, but it didn't. What does this cascade option do really?

If merging two entities separately is the solution does that mean that if I have a complex entity with many relationships that may have detached children of their own, I need to traverse that graph and manually merge every detached entity there is in the graph? I must say this makes things very ugly.


Top
 Profile  
 
 Post subject: reply
PostPosted: Mon Jan 28, 2008 2:39 pm 
Newbie

Joined: Sun Jan 27, 2008 5:35 pm
Posts: 3
Yes, thanks, I am aware of this technique, it's just that I hoped that specifying cascadeType.ALL would solve this for me, but it didn't. What does this cascade option do really?

If merging two entities separately is the solution does that mean that if I have a complex entity with many relationships that may have detached children of their own, I need to traverse that graph and manually merge every detached entity there is in the graph? I must say this makes things very ugly.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

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.