I have many questions to ask about hibernate sessions, because I'm having continuous issues with them.
I'm using Spring 3.1.1 and Hibernate 4.1.3.
first here is the class diagram that I'm working on.
code classes:
public class Equipement {
@ManyToOne(fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
public getOffice(){
return office ;
}
public class Office {
@ManyToOne(fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
public getService(){
return service ;
}
public class Service {
@ManyToOne(fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
public getDepartement(){
return departement ;
}
Here is how I load all my equipement.
public class HibernateEquipementDao{
@SuppressWarnings("unchecked")
public List<Equipement> getAll() {
return sessionFactory.getCurrentSession().createQuery("from Equipement").list() ;
}
}
Suppose I load all my equipements in a List<Equipement> allEquipements
and want for example to populate my view with only equipements of some service or some departements.
List<Equipement> aListOfEquipements = new ArrayList<Equipement>() ;
for(Equipement equipement : allEquipements)
if(equipement.getOffice().getService().getName().equals("name"))
aListOfEquipements.add(equipement) ;
Can I really do this whenever I want ? the session called Current is always active ? sometimes I get that exception. org.hibernate.LazyInitializationException: could not initialize proxy - no Session
that makes me ask where is the Current session that my EquipementDao use ?
Is there a way to open a session when I want to get the Departement of an Equipement ?
Or there is another way to proceed ? how manage sessions and how to load all my Equipements and access the other entities with no problems ?