-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToMany relationship and orphanRemoval attribute
PostPosted: Tue Jun 07, 2011 5:14 pm 
Newbie

Joined: Tue Jun 07, 2011 4:53 pm
Posts: 2
Hi friends, I need your help... :)

I developing an application in spring mvc and hibernate3. The configuration of hibernate classes are the next...

Code:
@Entity
@Table(name="TDE_DECLARACION")
@SequenceGenerator(name="SEQ_DECLARACION", sequenceName="DECLARACION_ID")
public class Declaracion{
   
   private int codigo;
   private int tipo;
   private String domicilio;

   private List<PatInmobiliario> patInmobiliario;
   
   public Declaracion(){
   }
   
   @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_DECLARACION")
   @Column (name="DE_CODIGO")
   public int getCodigo() {
      return codigo;
   }
   public void setCodigo(int codigo) {
      this.codigo = codigo;
   }
   
   @Column (name="DE_TIPO", nullable = false)
   public int getTipo() {
      return tipo;
   }
   public void setTipo(int tipo) {
      this.tipo = tipo;
   }
   
   @Column (name="DE_DOMICILIO")
   public String getDomicilio() {
      return domicilio;
   }
   public void setDomicilio(String domicilio) {
      this.domicilio = domicilio;
   }
   
   @OneToMany(cascade={CascadeType.ALL}, mappedBy="clave.declaracion", orphanRemoval=true)
   public List<PatInmobiliario> getPatInmobiliario() {
      return patInmobiliario;
   }

   public void setPatInmobiliario(List<PatInmobiliario> patInmobiliario) {
      this.patInmobiliario = patInmobiliario;
   }

   @Override
   public String toString() {
      return "Declaracion [codigo=" + codigo + "]";
   }
   
   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + codigo;
      result = prime * result + tipo;
      result = prime * result
            + ((domicilio == null) ? 0 : domicilio.hashCode());
      result = prime * result
            + ((patInmobiliario == null) ? 0 : patInmobiliario.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Declaracion other = (Declaracion) obj;
      if (codigo != other.codigo)
         return false;
      if (domicilio == null) {
         if (other.domicilio != null)
            return false;
      } else if (!domicilio.equals(other.domicilio))
         return false;
      if (tipo != other.tipo)
         return false;
      if (patInmobiliario == null) {
         if (other.patInmobiliario != null)
            return false;
      } else if (!patInmobiliario.equals(other.patInmobiliario))
         return false;
      return true;
   }

Code:
@Entity
@Table(name="TPI_PATINMOBILIARIO")
public class PatInmobiliario {
   
   private PatInmobiliario_PK clave;
   private String descripcion;
   
   @EmbeddedId
   public PatInmobiliario_PK getClave(){
      return clave;
   }
   
   public void setClave(PatInmobiliario_PK patInmobiliario_PK){
      this.clave = patInmobiliario_PK;
   }
   
   @Column(name="PI_DESCRIPCION")
   public String getDescripcion() {
      return descripcion;
   }
   public void setDescripcion(String descripcion) {
      this.descripcion = descripcion;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((clave == null) ? 0 : clave.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      PatInmobiliario other = (PatInmobiliario) obj;
      if (clave == null) {
         if (other.clave != null)
            return false;
      } else if (!clave.equals(other.clave))
         return false;
      return true;
   }
}

Code:
@Embeddable
public class PatInmobiliario_PK implements Serializable{
   
   private int codigo;
   private int declaracion;
   
   @Column(name="PI_CODIGO")
   public int getCodigo() {
      return codigo;
   }
   public void setCodigo(int codigo) {
      this.codigo = codigo;
   }
   
   @Column(name="PI_DE_CODIGO")
   public int getDeclaracion() {
      return declaracion;
   }
   public void setDeclaracion(int declaracion) {
      this.declaracion = declaracion;
   }
   
   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + codigo;
      result = prime * result + declaracion;
      return result;
   }
   
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      PatInmobiliario_PK other = (PatInmobiliario_PK) obj;
      if (codigo != other.codigo)
         return false;
      if (declaracion != other.declaracion)
         return false;
      return true;
   }
}


JAVA CODE

Declaracion1
|--> PatInmobiliario1
|--> PatInmobiliario2

Declaracion1.getPatInmobiliario().remove(PatInmobiliario1);

If I have an object of class Declaracion with two childs of class PatInmobiliario, and I remove one of these and persist the data in database with
getHibernateTemplate().saveOrUpdate(declaracion);
The changes I make in the parent class (Declaracion) was updated but the child of PatInmobiliario didn't deleted.

I hope that my doubt has been clear.

Sorry for my english :S


Top
 Profile  
 
 Post subject: Re: OneToMany relationship and orphanRemoval attribute
PostPosted: Wed Jun 08, 2011 12:06 pm 
Newbie

Joined: Tue Jun 07, 2011 4:53 pm
Posts: 2
I have no solution for this issue, I implement a method that delete the child class manually.

Code:
   public PatInmobiliario getPatInmobiliario(PatInmobiliario_PK codigo) {
      PatInmobiliario patInmobiliario = (PatInmobiliario)getHibernateTemplate().get(PatInmobiliario.class, codigo);
      return patInmobiliario;
   }
   
   public boolean removePatInmobiliario(PatInmobiliario_PK patInmobiliario) {
      getHibernateTemplate().delete(getPatInmobiliario(patInmobiliario));
      getSession().flush();
      return true;
   }


This patch solve the problem But the right way would be done automatically :(


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