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
--------------------------------------------------------------
Hi
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.
@Entity
public class Offer implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
//bidirectional
//Appointments that are in the offer
@ManyToMany(fetch=FetchType.EAGER)
@IndexColumn(name="INDEX_COL")
private List<Appointment> appointments = new ArrayList<Appointment>();
...}
@Entity
public class Appointment implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String description;
//unidirektional
@ManyToOne
@JoinColumn(name="instructor")
private CmtUser instructor;
...
//bidirektional
@ManyToMany(fetch=FetchType.EAGER,mappedBy="appointments")
@IndexColumn(name="INDEX_COL")
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) {
try{
Query q = em.createQuery("SELECT a from Offer a " +
"WHERE a.instructor.id = :instructorId ");
q.setParameter("instructorId",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) {
try{
Query q = em.createQuery("SELECT a from Appointment a " +
"WHERE a.instructor.id = :instructorId AND " +
"a.accepted =:false ");
q.setParameter("instructorId",instructorId);
q.setParameter("false",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
Nicki
--------------------------------------------------------------
Reply to this message by going to Community
[
http://community.jboss.org/message/564238#564238]
Start a new discussion in EJB 3.0 at Community
[
http://community.jboss.org/choose-container!input.jspa?contentType=1&...]