-->
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: Bi-directionnal recursive relation not refreshed in session
PostPosted: Tue Dec 11, 2007 6:50 am 
Newbie

Joined: Mon Jan 08, 2007 10:28 am
Posts: 11
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 7:53 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
You are supposed to keep the references in sync yourself.

if a1 references a2, you have to make sure a2 references a1, before you save it in the database.

If you don't do that it actually is next to random which state (with or without) the referenc is stored in the database


This is one of the many reasons why I recommend to avoid bidirectional references if possible.

regards
Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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.