私は以下のアプローチを使用して、休止状態での遅延初期化問題を解決しています。それがうまくいくかどうか教えてください。私はいくつかの理由から強制的に私の永続層に私の転写を実装する必要があります。休止状態の遅延ソリューション。そうですか?
public class CourseDAO {
Session session = null;
public CourseDAO() {
session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public Course findByID(int cid) {
Course crc = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery(
"from Course as course where course.cid = "+cid+" "
);
crc = (Course) q.uniqueResult();
//note that i am not commiting my transcation here.
//Because If i do that i will not be able to do lazy fetch
}
catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
throw new DataAccessLayerException(e);
}
finally {
return crc;
}
}
}
とフィルタで、私はfollingコード
session = HibernateUtil.getSessionFactory().getCurrentSession();
if(session.isOpen())
session.getTransaction().commit();
を使用していますが、このアプローチの権利ですか?何か問題がありますか?
http://community.jboss.org/wiki/OpenSessionInView – Bozho