Hi,
I have a problem with a recursive bi directional relation :
- class A contains a collection of A (@OneToMany)
- class A contain a reference on the parent class A (@ManyToOne)
- in the same session :
* I persist an object A1
* I persist an other objet A2 with A1 as parent
* I make a find on the A1 id => PB : the A1.collection is not updated... it is empty (session cache version)
* It is OK if I make a refresh(A1) after the find
So my questions are :
- Am i supposed to do a refresh on A1 to force the database access and have the good version of my A1 entity (with the updated collection) ?
- Is there something wrong with the my ORM configuration ?
- Is it a hibernate bug ?
Hibernate version: 3.2.2.GA
Mapping documents:
Code:
@Entity
@Table(name = "T_SIGNALISATION")
public class Signalisation extends EntiteJpa {
private static final long serialVersionUID = -6564628427466074308L;
@Id
@GeneratedValue(generator = "Signa")
@SequenceGenerator(name = "Signa", sequenceName = "SIGNA_SEQ", allocationSize = 1)
@Column(name = "ID", nullable = false, precision = 22, scale = 0)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SIG_MERE_ID")
private Signalisation signalisationMere;
@Column(name = "LIBELLE", nullable = true)
private String libelle;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "signalisationMere")
private List<Signalisation> signalisationsFilles = new ArrayList<Signalisation>(0);
public List<Signalisation> getSignalisationsFilles() {
return signalisationsFilles;
}
...
Code between sessionFactory.openSession() and session.close():Code:
// SIGNALISATION CREATION
Signalisation sig = new Signalisation();
sig.setLibelle("TestLibelle1");
signalisationDao.persist(sig);
signalisationDao.flush();
// SIGNALISATION CREATION
Signalisation sigFille = new Signalisation();
sigFille.setLibelle("TestLibelleFille1");
sigFille.setSignalisationMere(sig);
signalisationDao.persist(sigFille);
signalisationDao.flush();
sig = signalisationDao.find(sig.getId());
//signalisationDao.refresh(sig); // test pass with this line (force the database access)
List<Signalisation> l = sig.getSignalisationsFilles();
assertNotNull(l);
assertEquals("Liste des sig filles non complete", 1, l.size());
assertEquals("Mauvaise Sig fille", l.get(0).getId(), sigFille.getId());
Best regards,
Rudy