Yes, thank you for your answer, I have now understood this.
My question is now a little bit different. I don't want to use a filter in my view (because I think this implies the use of hibernate classes, and I want a portable code for my application.
So I have tried a first solution consisting in map my entities to beans in the view tier. This can appear to be strange, but I will integrate JSF later, so it can maybe be an interesting solution.
And i have found an other solution too, consisting in use EJB-QL to force the load of lazy relations, when needed. Sure, the disadvantage is that when the list is loaded like this, it is definitive for the current user session, but finally, it is interesting too.
So I am actually on an new problem, and I would be happy to have any answer. I am actually trying to use EJB-QL for load the contacts of a person. This is a ManyToMany relation on the class person (A person has contacts, and these contacts are themselves Persons).
So this is the important part of my entity for this relation to be understood:
Code:
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="PERSON")
@NamedQueries({....
@NamedQuery(name="person.lazyLoadContacts",
query="SELECT contacts FROM Person contacts JOIN contacts.persons AS p WHERE p.id LIKE :id")
})
public class Person implements Serializable {
....
....
/* ManyToMany relation attributes */
private Collection<Person> contacts;
private Collection<Person> persons;
public Person(){ }
public Person(String name){
this.name= name;
}
/********************************/
@ManyToMany
@JoinTable(
name="PERSON_CONTACT",
joinColumns=@JoinColumn(name="ID_PERSON", referencedColumnName="ID"),
inverseJoinColumns=@JoinColumn(name="ID_CONTACT", referencedColumnName="ID")
)
public Collection<Person> getContacts(){
return this.contacts;
}
public void setContacts(Collection contacts){
this.contacts= contacts;
}
@ManyToMany(mappedBy="contacts")
public Collection<Person> getPersons(){
return this.persons;
}
public void setPersons(Collection<Person> persons){
this.persons= persons;
}
}
I know my EJB QL is wrong but I don't know why (I have an idea, but I don't find the solution). So, do you see how to correct this?
I would also be happy to have your opinion about my ideas about the management of the lazy relations.
One more time, thanks for your answers. I really helps.