[jboss-user] [EJB 3.0] - ManyToMany bidirectional - inverse side does not load the complete collection

Nicole Schweighardt do-not-reply at jboss.com
Thu Sep 30 07:27:25 EDT 2010

Nicole Schweighardt [http://community.jboss.org/people/NSchweig] created the discussion

"ManyToMany bidirectional - inverse side does not load the complete collection"

To view the discussion, visit: http://community.jboss.org/message/564238#564238


I read a lot about eager and lazy fetching and so on but for this case I could not find a solution.

I have a relationship between 'appointment' and 'offer'. An appointment has a list with offers, an offer has a list with appointments. Offer is the owner side.

public class Offer implements Serializable{
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    //Appointments that are in the offer
    private List<Appointment> appointments = new ArrayList<Appointment>();

public class Appointment implements Serializable{
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    private String description;
    private CmtUser instructor;
    private List<Offer> offers= new ArrayList<Offer>();

If I load an offer from my database in my sessionbean 'OfferHandler', the list with the appointments is fully loaded. (see picture 1) (I tested it with the debugger to look into the collections in the sessionbean, too, not only with my web layer)

public List<Offer> getOffersForInstructor(Long instructorId) {
            Query q = em.createQuery("SELECT a from Offer a " +
                    "WHERE a.instructor.id = :instructorId ");
            List<Offer> offers =    (List<Offer>) q.getResultList();
            return offers ;
        catch(EntityNotFoundException ex){}
        return null;

But on the other side if I try to load some appointments from my database the collection with the offers only contains the last saved offer! (see picture 2)

public List<Appointment> getFreeAppointments(Long instructorId) {
            Query q = em.createQuery("SELECT a from Appointment a " +
                    "WHERE a.instructor.id = :instructorId AND " +
                    "a.accepted  =:false ");
            List<Appointment> appointments =    (List<Appointment>) q.getResultList();
            return appointments;

In the database there is an jointable with the right entries:
f. example: (the first offer contains the first appointment, the second offer contains both appointments)

offers_id          appointments_id     INDEX_COL
1                    1                  0
2                    1                  0
2                    2                  1

I hope someone can help. I tested a lot to force loading the collection. f.example refreshing the appointments or loading the offers in the appointments with appointment.offers.size().

Thank you a lot

Reply to this message by going to Community

Start a new discussion in EJB 3.0 at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20100930/a0709f8b/attachment.html 

More information about the jboss-user mailing list