Courses courses = null;
Session session = null;
Transaction trans = null; try
{
session = factory.openSession();
trans = session.beginTransaction();
courses = (Student) session.get(courses.class, id);
trans.commit();
} catch (RuntimeException e)
{
trans.rollback();
e.printStackTrace();
throw e;
} finally
{
session.close();
}
System.out.println(course.toString()); //最好覆盖toString
if(course.getChapterses()==null)
{
System.out.println("what a fuck body too!");
}
Set set=(Set) course.getChapterses();
System.out.println(set.size());no session or session was closed
你的session关闭太早了(原因我不知,你知)
Session session = null;
Transaction trans = null; try
{
session = factory.openSession();
trans = session.beginTransaction();
courses = (Student) session.get(courses.class, id);
trans.commit();
} catch (RuntimeException e)
{
trans.rollback();
e.printStackTrace();
throw e;
} finally
{
session.close();
}
System.out.println(course.toString()); //最好覆盖toString
if(course.getChapterses()==null)
{
System.out.println("what a fuck body too!");
}
Set set=(Set) course.getChapterses();
System.out.println(set.size());no session or session was closed
你的session关闭太早了(原因我不知,你知)
如果你的DAO还是使用实体层的级联来进行查询的话。很费劲。首先之一就是不灵活。我推荐的方法查看下列2个帖子中我的观点。以前的帖子里面是体现多对多的关系。相对于多对多,你的多对一更为简单能够实现。http://topic.csdn.net/u/20071227/13/8be476e9-507d-4ae0-9156-dfd29b92abf0.html
http://topic.csdn.net/u/20071228/23/a331e107-75dd-458d-b211-b7b5863eedd4.html当然,如果一定要将关联关系放入实体层来体现的话。你也一定不要通过实体的关系来查找。性能很低。可以直接用hql:
select model.chapterses from Courses as model where model.id = : id(我大概看了下你的实体配置,理解意思即可)