第一个问题:dao中方法没有getSession().flush();delete()或者saveOrUpdate()方法无效dao中的方法
public class BaseDao<T, PK extends Serializable>  {
@Autowired
protected SessionFactory sessionFactory;
protected Class<T> clazz; public BaseDao() {
this.clazz = ReflectionUtils.getSuperClassGenricType(getClass());
} /**
 * 取得当前session
 * 
 * @return session
 */
public Session getSession() {
return sessionFactory.getCurrentSession();
} /**
 * 保存或新增实体
 * 
 * @param entity
 *            实体
 */
public void saveOrUpdate(final T entity) {
 getSession().saveOrUpdate(entity);
} /**
 * 删除实体.
 * 
 * @param entity
 *            要删除的实体
 */
public void delete(final T entity) {
getSession().delete(entity);
}action中调用
Back b = backService.get(710L);
b.setUserName("sfwefdfsf");
dao.saveOrUpdate(b);
我想把这个数据在数据库中取出来,然后修改姓名,在存进去,可是数据库没有任何变化,后台sql语句也不打印如过把dao中发放改成
public void saveOrUpdate(final T entity) {
getSession().flush();
getSession().saveOrUpdate(entity);
}这样就可以,为什么呢,我看别人的代码都没有getSession().flush();啊,肯定不是事务问题,因为我执行session.save方法时是没有错误的,我的web.xml配置相关文件如下:<filter>
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
<filter-class>org.wdj.framework.filter.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>excludeSuffixs</param-name>
<param-value>js,css,jpg,gif</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第二个问题:dao中方法
public T get(final PK id) {
System.out.println("get");
return (T) getSession().load(clazz, id);
}在执行这个方法的时候为什么后台会打印出二条SQL语句?可是这个方法却只执行一次。第三个问题:hibernate中有query,Criteria查询,如果只用一种查询可以吗,一般大家都用那种,谢谢

解决方案 »

  1.   

    1.好久没用hibernate,第一个不知道
    2.打印二条什么SQL,还是二条"get"?
    3.当然可以,看情况,觉得哪种合适用那种
      

  2.   

    1·估计你的getSession()方法都没有任何事务的提交
    2·你确定你只执行了一次这个方法?两条sql语句都是一样的吗?会不会其他的操作产生了两条sql语句
    3·一般人都喜欢用query,因为他接近于sql,但是java是推荐用criteria的,因为criteria更加具有面向对象的思想