The classes
class Bean {
private List<Radicacion> getReferencias() {
List<Radicacion> refs = DAO.findAll(Radicacion.class);
return refs;
}
private Radicacion getRadicacion() {
Radicacion r = DAO.findById(Radicacion.class, new Integer(33) );
return r;
}
private void guardarDependencias() {
Iterator<Radicacion> itRad = getReferencias().iterator();
while (itRad.hasNext()){
Radicacion ref = itRad.next();
getRadicacion().getRadicacions().add(ref);
ref.setReferencia(getRadicacion());
}
}
}
////////
class Radicacion {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "referencia")
public List<Radicacion> getRadicacions() {
return this.radicacions;
}
public void setRadicacions(List<Radicacion> radicacions) {
this.radicacions = radicacions;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "referencia_asociada")
public Radicacion getReferencia() {
return this.referencia;
}
public void setReferencia(Radicacion radicacion) {
this.referencia = referencia;
}
}
the problem
i need that all Radicacion objects contained provided by the getReferencias() method end up in the getRadicacion().getReferencias() collection.
whenever i call guardarDependencias (save dependencies in english) nothing happens. The original getRadicacion.getReferencias() remains unchanged.
things to ponder
if i insert a DAOBase.merge(ref); in the iteration, it does not produce any sql output. It makes me think that ref is already loaded as persistent, so why hibernate fails to update its status when i set the link to getRadicacion()?
if I insert System.out.println( HibernateUtil.getCurrentSession().contains(ref)); in the iterator, it prints out true.
If i create other asociations,persistence works fine. The problem might have something to do with the self reference.
Thanks for your help