Hi,
I am a newbie in hibernate. Following code is from Netbeans introducing how to use hibernate in visual web,
http://www.netbeans.org/kb/61/web/hibernate-vwp.htmlMy first question is when should I close the session. It did not mention in the online doc.
-----------------------------------------------------------------------------------
private void buildPersonOptions() {
List<Person> personList = null;
try{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Person");
personList = (List<Person>) q.list();
} catch(Exception e) {
e.printStackTrace();
}
personOptions = new Option[personList.size()];
int i=0;
for(Person person : personList) {
Option opt = new Option(person.getPersonId(), person.getName());
personOptions[i++] = opt;
}
}
Second question, I have create a visual web project using hibernate. Following is the part of the code in SessionBean1.java
protected void updateCurrentEmp(){
List<CurrentEmpPrb> currentEmpList = null;
List<Prb> availablePrbList = null;
int prbStatus = 0;
try{
Transaction tx = sessionTest.beginTransaction();
Query q = sessionTest.createQuery("from CurrentEmpPrb e where e.lanId = ?");
String id = ((LanId) getBean("LanId")).getId();
q.setString(0,id);
currentEmpList = (List<CurrentEmpPrb>)q.list();
tx.commit();
//session.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
if (currentEmpList.size()!=0){
currentEmpOptions = new Option[currentEmpList.size()];
int i=0;
for(CurrentEmpPrb currEmployee : currentEmpList){
Option opt = new Option(currEmployee.getCompany_EmployeeId (),currEmployee.getCompany_EmployeeId());
currentEmpOptions[i++] = opt;
}
currentEmp = currentEmpList.get(0);
if (currentEmp.getEmp().getIsManager().longValue()==1){
setCurrentEmpCategory("mngrView");
}else{
currentEmp.getEmp().getEmpPrbDetl().getEmpId();
.......
}
else{
.......
}
}
I have noticed that I can not put 'sessionTest.close()' in try{} section, otherwise I will get a 'could not initialize proxy - no Session' exception because of lazy = 'true'. In this case, when should I close the session and how to achieve that?
Sorry for my poor english.
Thanks in advance!