I am going to try to explain clearly my problem.
Context: I am doing a web app with : glassfish v3, EclipseLink. My problem is about the low performances of a bidirectionnal oneToMany relationship.
I have one class Event and one class photo. One Event can have one or more photos, and reciprocally, one can belong to 0 or only one event.
Before: I did mapping by myself. So when I needed to load all Photo objects for a given Event, I simply used a query : select * from Photo p where p.event_id_fk = SOMETHING. It gave me for one query all needed Photo objects.
Now: I used JPA annotation @OneToMany as follow :
@Entity
@Table( name = "photo" )
public class Photo{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column( name = "ID" )
private long idPhoto;
@ManyToOne
@JoinColumn( name = "EVENT_ID" )
private Event event;
... other fields ...
And
@Entity
@Table( name = "event" )
public class Event {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column( name = "ID" )
private Long idEvent;
@OneToMany( mappedBy = "event", orphanRemoval = true, fetch = FetchType.LAZY )
private List<Photo> photos = new ArrayList<Photo>();
... other fiels ...
The problem is that when I select the List photos from Event object (just doing event.getPhotos() ), I can read the JPA generated query :
Précis: SELECT ...ALL FIELDS... FROM photo_account WHERE (PHOTO_ID = ?)
bind => [996]
Précis: SELECT ...ALL FIELDS... FROM photo_account WHERE (PHOTO_ID = ?)
bind => [997]
Précis: SELECT ...ALL FIELDS... FROM photo_account WHERE (PHOTO_ID = ?)
bind => [998]
Précis: SELECT ...ALL FIELDS... FROM photo_account WHERE (PHOTO_ID = ?)
bind => [999]
(...)
The result is one query for one photo. In practice, this selection is pretty slow.
Therefore, I would like to know if it exists a way to load all objects by only one query? (custom query, parameters to configure...) and/or a best way to map/design the relationship between these objects? and more generally if I am doing something bad?
Thank you in advance for any help.