service代码://方法上有配置了事务
public Tree createDirectoryTree(long rootDirectoryId) throws ServiceException {
Directory rootDirectory = (Directory)getBaseDAO().load(getDirectoryClass(), rootDirectoryId);
System.out.println(rootDirectory.getName());//这里报no session错误
...
}
DAO代码:public BasePojo load(Class<? extends BasePojo> clazz, long id) throws DataAccessException {
try {
return hibernateTemplate.load(clazz, id);
} catch(Exception e) {
throw new DataAccessException(e.getMessage());
} finally {
hibernateTemplate.clear();//去掉这一句,就没有报no session错误
}
}createDirectoryTree的事务还没结束,session应该是还没关闭的啊
而且上面DAO注释掉hibernateTemplate.clear(),就不会有no session错误,我找不到原因,各位帮帮忙指导下我什么原因
public Tree createDirectoryTree(long rootDirectoryId) throws ServiceException {
Directory rootDirectory = (Directory)getBaseDAO().load(getDirectoryClass(), rootDirectoryId);
System.out.println(rootDirectory.getName());//这里报no session错误
...
}
DAO代码:public BasePojo load(Class<? extends BasePojo> clazz, long id) throws DataAccessException {
try {
return hibernateTemplate.load(clazz, id);
} catch(Exception e) {
throw new DataAccessException(e.getMessage());
} finally {
hibernateTemplate.clear();//去掉这一句,就没有报no session错误
}
}createDirectoryTree的事务还没结束,session应该是还没关闭的啊
而且上面DAO注释掉hibernateTemplate.clear(),就不会有no session错误,我找不到原因,各位帮帮忙指导下我什么原因
昨天,我查了hibernate的缓冲机制的资料,心里有了个模模糊糊的概念,但是还是理不清楚,帮忙讲下原因。
我把load改为get,就不会出错,我知道load是懒加载,get是立即加载,我在load之后,清除缓存,这个时候缓冲里面找不到对象,但是session还没关,不是还可以再加载吗?
还有就是get不也是加载到缓存里面吗,那clear之后为什么还能使用?
能不能告诉我spring中hibernate的session周期,是不是从事务开始,到事务结束时关闭。
那这样的话,每个方法是不是都是用sessionFactory.getCurrentSession()来获取session,然后操作数据库,晚上回去试下,真悲剧
不知道我想的是不是正确的,有没有人指导下,明天结账。