I've read HibernateJAOO.ppt and search here for detached object support in Hibernate, but still unclear about its use. The following code will throw Lazy Exception when run:
Code:
public class TestCode {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sf = DataStore.getHibernateSessionFactory();
session = sf.openSession();
Test test = null;
Set objectiveQuestions = null;
test = (Test) session.get(Test.class, new Long(2));
session.close();
System.err.println("session closed and will open a new one ");
session = sf.openSession();
System.err.println(test.getEntryDate());
System.err.println(test.getTitle());
Set objectiveTestRequirements = test.getTestRequirementsObjective();
for (Iterator iterator = objectiveTestRequirements.iterator(); iterator.hasNext();) {
TestRequirementObjective objectiveTestRequirement = (TestRequirementObjective) iterator.next();
System.err.println(objectiveTestRequirement.getCatalog().getName());
objectiveQuestions = objectiveTestRequirement.getObjectiveQuestions();
}
session.close();
System.err.println("session closed and will open a new one ");
session = sf.openSession();
for (Iterator iterator = objectiveQuestions.iterator(); iterator.hasNext();) {
QuestionObjective q = (QuestionObjective) iterator.next();
System.err.println(q.getEntryDate());
}
session.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
But the following code will work correctly when I replace close/open with disconnect and reconnect:
Code:
public class TestCode {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sf = DataStore.getHibernateSessionFactory();
session = sf.openSession();
Test test = null;
Set objectiveQuestions = null;
test = (Test) session.get(Test.class, new Long(2));
session.disconnect();
System.err.println("session closed and open a new one ");
session.reconnect();
System.err.println(test.getEntryDate());
System.err.println(test.getTitle());
Set objectiveTestRequirements = test.getTestRequirementsObjective();
for (Iterator iterator = objectiveTestRequirements.iterator(); iterator.hasNext();) {
TestRequirementObjective objectiveTestRequirement = (TestRequirementObjective) iterator.next();
System.err.println(objectiveTestRequirement.getCatalog().getName());
objectiveQuestions = objectiveTestRequirement.getObjectiveQuestions();
}
session.disconnect();
System.err.println("session closed and open a new one ");
session.reconnect();
for (Iterator iterator = objectiveQuestions.iterator(); iterator.hasNext();) {
QuestionObjective q = (QuestionObjective) iterator.next();
System.err.println(q.getEntryDate());
}
session.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
}
So does "detached objects support" means I must never close a session (session.close()) ?
Thank you very much !