0

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

demonz demonz
  • 641
  • 1
  • 15
  • 32

1 Answers1

0

What you code suggest that you want to add all Radicacions ( which you get by (DAO.findAll(Radicacion.class) to a Radicacion with id 33. Also Radicacion is self referencing that is it has a reference to itself as well as has a collection of Radicacion (Parent- Child relation)

The first thing you should check is that if the method guardarDependencies is running inside a transaction? I don’t see any transaction management code. If no sql is outputted (and you expect it) during session usage then the first suspicion goes to whether it is run under a transaction.

You can also run Hibernate in DEBUG mode and see what is happening under the hood. it's very helpful to get at the root problem

Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • hi shailendra, you are right, Radicacion is a self referencing class. As for the transaction code, it is a filter that begins a new transaction for every request and does a commit when the request is about to end. – demonz demonz Sep 18 '12 at 14:43
  • See http://stackoverflow.com/questions/3393515/jpa-how-to-have-one-to-many-relation-of-the-same-entity-type and http://stackoverflow.com/questions/5329015/hibernate-annotations-self-referencing-class for proper usage of self referencing relations – Shailendra Sep 18 '12 at 18:21