-->
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.  [ 3 posts ] 
Author Message
 Post subject: delete a self referenced entity
PostPosted: Tue Nov 04, 2008 2:57 am 
Newbie

Joined: Fri Sep 26, 2008 4:45 am
Posts: 17
Hi,

I've the following entity :

Code:
package ch.btc.datec.yamina.model;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity

public class Localisation implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID")
   private long id;

   @Column(name = "lieuDit", length = 40, nullable = true)
   private String lieuDit;

   @Column(name = "coordX")
   private int coordonneeX;

   @Column(name = "coordY")
   private int coordonneeY;

   @Column(name = "coordProvenance", length = 40, nullable = true)
   private String provenanceCoordonnees;

   @Column(name = "numSecteur", length = 40, nullable = true)
   private String noSecteur;

   @Column(name = "numParcelle", length = 40, nullable = true)
   private String noParcelle;

   @Column(name = "numArticle", length = 40, nullable = true)
   private String noDetache;

   @Column(name = "SURFACE_PARCELLE", length = 40, nullable = true)
   private String surfaceParcelle;

   @OneToOne (cascade = CascadeType.ALL,fetch = FetchType.EAGER)
   private Adresse adresse; // TODO OneToOne (don't share an address between more object)

   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   private Set<Localisation> parcellesDetachees;

   /**
    *
    */
   public Localisation() {
      parcellesDetachees = new HashSet<Localisation>();
      if (adresse == null) {
         this.adresse = new Adresse();
      }
      else {
         this.adresse = adresse;
      }
   }

   /**
    * @param adresse
    * @param coordonneeX
    * @param coordonneeY
    * @param id
    * @param lieuDit
    * @param noDetache
    * @param noParcelle
    * @param noSecteur
    * @param parcellesDetachees
    * @param provenanceCoordonnees
    * @param surfaceParcelle
    */
   public Localisation(Adresse adresse, int coordonneeX, int coordonneeY, long id,
         String lieuDit, String noDetache, String noParcelle,
         String noSecteur, Set<Localisation> parcellesDetachees,
         String provenanceCoordonnees, String surfaceParcelle) {
      this.adresse = adresse;
      this.coordonneeX = coordonneeX;
      this.coordonneeY = coordonneeY;
      this.id = id;
      this.lieuDit = lieuDit;
      this.noDetache = noDetache;
      this.noParcelle = noParcelle;
      this.noSecteur = noSecteur;
      this.parcellesDetachees = parcellesDetachees;
      this.provenanceCoordonnees = provenanceCoordonnees;
      this.surfaceParcelle = surfaceParcelle;
   }

   /**
    * @return the id
    */
   public long getId() {
      return id;
   }

   /**
    * @param id
    *            the id to set
    */
   public void setId(long id) {
      this.id = id;
   }

   /**
    * @return the lieuDit
    */
   public String getLieuDit() {
      return lieuDit;
   }

   /**
    * @param lieuDit
    *            the lieuDit to set
    */
   public void setLieuDit(String lieuDit) {
      this.lieuDit = lieuDit;
   }

   /**
    * @return the coordonneeX
    */
   public int getCoordonneeX() {
      System.out.println("GET COORDONNEE X " + coordonneeX);
      return coordonneeX;
   }

   /**
    * @param coordonneeX
    *            the coordonneeX to set
    */
   public void setCoordonneeX(int coordonneeX) {
      System.out.println("SET COORDONNEE X " + coordonneeX);
      this.coordonneeX = coordonneeX;
   }

   /**
    * @return the coordonneeY
    */
   public int getCoordonneeY() {
      System.out.println("GET COORDONNEE Y " + coordonneeY);
      return coordonneeY;
   }

   /**
    * @param coordonneeY
    *            the coordonneeY to set
    */
   public void setCoordonneeY(int coordonneeY) {
      System.out.println("SET COORDONNEE Y " + coordonneeY);
      this.coordonneeY = coordonneeY;
   }

   /**
    * @return the provenanceCoordonnees
    */
   public String getProvenanceCoordonnees() {
      return provenanceCoordonnees;
   }

   /**
    * @param provenanceCoordonnees
    *            the provenanceCoordonnees to set
    */
   public void setProvenanceCoordonnees(String provenanceCoordonnees) {
      this.provenanceCoordonnees = provenanceCoordonnees;
   }

   /**
    * @return the noSecteur
    */
   public String getNoSecteur() {
      return noSecteur;
   }

   /**
    * @param noSecteur
    *            the noSecteur to set
    */
   public void setNoSecteur(String noSecteur) {
      this.noSecteur = noSecteur;
   }

   /**
    * @return the noParcelle
    */
   public String getNoParcelle() {
      return noParcelle;
   }

   /**
    * @param noParcelle
    *            the noParcelle to set
    */
   public void setNoParcelle(String noParcelle) {
      this.noParcelle = noParcelle;
   }

   /**
    * @return the noDetache
    */
   public String getNoDetache() {
      return noDetache;
   }

   /**
    * @param noDetache
    *            the noDetache to set
    */
   public void setNoDetache(String noDetache) {
      this.noDetache = noDetache;
   }

   /**
    * @return the surfaceParcelle
    */
   public String getSurfaceParcelle() {
      return surfaceParcelle;
   }

   /**
    * @param surfaceParcelle
    *            the surfaceParcelle to set
    */
   public void setSurfaceParcelle(String surfaceParcelle) {
      this.surfaceParcelle = surfaceParcelle;
   }

   /**
    * @return the adresse
    */
   public Adresse getAdresse() {
      return adresse;
   }

   /**
    * @param adresse
    *            the adresse to set
    */
   public void setAdresse(Adresse adresse) {
      this.adresse = adresse;
   }

   /**
    * @return
    * @see java.util.List#size()
    */
   public int size() {
      return parcellesDetachees.size();
   }


that have a reference to itself :

Code:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)private Set<Localisation> parcellesDetachees;


when I'm attemting to delete a sub entity (an entity inserted into the parcelleDetachees collection I'm having the following exception

Code:
2008-11-03 16:23:10 ERROR - org.hibernate.util.JDBCExceptionReporter:78 - [NDSQL02\DEV]L'instruction DELETE est en conflit avec la contrainte REFERENCE "FKC2E5C5DF6CDFC068". Le conflit s'est produit dans la base de données "datec", table "dbo.Localisation_Localisation", column 'parcellesDetachees_ID'.
2008-11-03 16:23:10 ERROR - org.hibernate.event.def.AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [ch.btc.datec.yamina.model.Localisation#132]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2546)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2702)
   at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:77)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:842)
   at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:838)
   at ch.btc.datec.yamina.dao.ParcelleDaoImpl.delete(ParcelleDaoImpl.java:30)
   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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
   at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:126)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy38.delete(Unknown Source)
   at ch.btc.datec.yamina.web.SessionBean.deleteDetachedLocalisation(SessionBean.java:3528)
   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 org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
   at javax.faces.component.UICommand.broadcast(UICommand.java:89)
   at com.icesoft.faces.component.panelseries.UISeries$RowEvent.broadcast(UISeries.java:546)
   at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:238)
   at com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:284)
   at javax.faces.component.UIData.broadcast(UIData.java:517)
   at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:233)
   at com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:284)
   at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
   at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
   at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
   at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
   at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:17)
   at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:54)
   at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:42)
   at com.icesoft.faces.webapp.http.core.ViewBoundServer.service(ViewBoundServer.java:65)
   at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:44)
   at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer$Matcher.serviceOnMatch(PathDispatcherServer.java:50)
   at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:19)
   at com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet.service(ThreadBlockingAdaptingServlet.java:19)
   at com.icesoft.faces.webapp.http.servlet.EnvironmentAdaptingServlet.service(EnvironmentAdaptingServlet.java:29)
   at com.icesoft.faces.webapp.http.servlet.MainSessionBoundServlet.service(MainSessionBoundServlet.java:139)
   at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:35)
   at com.icesoft.faces.webapp.http.servlet.PathDispatcher$Matcher.serviceOnMatch(PathDispatcher.java:52)
   at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:29)
   at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:79)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
   at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
   at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
   at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
   at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
   at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:528)
   at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:261)
   at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:54)
   at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
   at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
   at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
   at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
   at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
   at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:26)
   at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
   at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3229)
   at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
   at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
   at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2002)
   at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1908)
   at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1362)
   at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
   at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
Caused by: com.inet.tds.be: Msg 547, Level 16, State 0, Line 1, Sqlstate 23000
[NDSQL02\DEV]L'instruction DELETE est en conflit avec la contrainte REFERENCE "FKC2E5C5DF6CDFC068". Le conflit s'est produit dans la base de données "datec", table "dbo.Localisation_Localisation", column 'parcellesDetachees_ID'.
   at com.inet.tds.al.a(Unknown Source)
   at com.inet.tds.n.b(Unknown Source)
   at com.inet.tds.n.a(Unknown Source)
   at com.inet.tds.p.c(Unknown Source)
   at com.inet.tds.p.a(Unknown Source)
   at com.inet.tds.p.b(Unknown Source)
   at com.inet.tds.bg.g(Unknown Source)
   at com.inet.tds.bg.executeUpdate(Unknown Source)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
   at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2525)
   ... 84 more
       


Any idea on what I'm doing wrong ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 8:58 am 
Newbie

Joined: Fri Jul 14, 2006 12:05 pm
Posts: 19
You need to remove the subentity from the parent's parcelleDetachees collection before you can delete it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 9:30 am 
Newbie

Joined: Fri Sep 26, 2008 4:45 am
Posts: 17
Thanks this is how I solve the issue


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.