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