还是hibernate4.2.5的这个问题
在整合完最新版的ssh后我测试用hibernate的getCurrentSession()取得Session后的save(),update()和delete()时,我发现页面上虽然能查出更新后的结果,但数据库里却未进行更新,而且后台未打印出sql语句,三个方法只有save()发出了insert语句,我原本以为是缓存问题,但清空缓存后还是一样,delete()和update()无法发出sql语句,这是为什么,我很奇怪,另外,我在更新和删除前先调用了load()把根据id把数据从数据库中查了出来,但只发出了select语句,随后执行update()和delete()却像没有作用一样,我把load()换成了get()也是一样的效果,求高人指点这到底是神马原因,为何发不出sql语句,万分感谢!另外,我的事务配置也做了,包括设置了aop:config和aop:advice,事务加在了service,但就是不能删除和修改数据库中的数据,也就是不能发出sql语句,但是select和insert却可以。求解释。
这是spring里关于事务的配置:
<!-- 配置事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="transactionAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.tarena.service..*Impl.* (..))" advice-ref="transactionAdvice" />
</aop:config>
</beans>
这是我的DAO层相关代码:
@Repository("dao")
public class DaoImpl <T, PK extends Serializable>
implements DaoI<T,PK>{
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
@Override
public void save(T entity) {
this.getCurrentSession().save(entity);
}
@Override
public void update(T entity) {
this.getCurrentSession().flush();
this.getCurrentSession().update(entity);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findAll(Class<T> entityClass) {
String hql = "from " + entityClass.getName();
return this.getCurrentSession().createQuery(hql).list();
}
@Override
public void delete(Class<T> entityClass, PK... pks) {
for(PK pk:pks){
this.getCurrentSession().flush();
this.getCurrentSession().delete(findEntity(entityClass,pk));
}
}
@Override
public Pager<T> findByPage(Class<T> entityClass, String xql, int pageNo, int pageSize) {
return findByPage(entityClass, xql, null, pageNo, pageSize);
}
@Override
public Pager<T> findByPage(Class<T> entityClass, String xql, Object key, int pageNo, int pageSize) {
return findByPage(entityClass, xql, new Object[] { key }, pageNo,
pageSize);
}
@SuppressWarnings("unchecked")
@Override
public Pager<T> findByPage(Class<T> entityClass, String xql, Object[] keys, int pageNo, int pageSize) {
Pager<T> pager = new Pager<T>();
Query query = getCurrentSession().createQuery(xql);
if (keys != null && keys.length > 0) {
for (int i = 0; i < keys.length; i++) {
query.setParameter(i, keys);
}
}
query.setFirstResult(pageNo).setMaxResults(pageSize);
List<T> pageList = query.list();
int totalCount = getTotalNum(xql, keys);
pager.setPageList(pageList);
pager.setTotalCount(totalCount);
return pager;
}
private int getTotalNum(String xql, Object[] keys) {
int totalNum = 0;
int index = xql.indexOf("from");
String from = xql.substring(index).trim();
String hql = "select count(*) " + from;
Query query = getCurrentSession().createQuery(hql);
if (keys != null && keys.length > 0) {
for (int i = 0; i < keys.length; i++) {
query.setParameter(i, keys);
}
}
totalNum = ((Long) query.uniqueResult()).intValue();
return totalNum;
}
@SuppressWarnings("unchecked")
@Override
public T findEntity(Class<T> entityClass, PK pk) {
return (T)this.getCurrentSession().load(entityClass, pk);
}
@SuppressWarnings("unchecked")
public T findEntity(String hql,PK pk){
return (T)this.getCurrentSession().createQuery(hql)
.setParameter(0, pk).uniqueResult();
}
}
中间我加了一个base层:
public abstract class AbstractBaseDao <T, PK extends Serializable> implements
BaseDao<T, PK>{
private DaoI<T,PK> dao;
@Resource(name="dao")
public void setDao(DaoI<T,PK> dao) {
this.dao = dao;
}
@SuppressWarnings("unchecked")
private Class<T> entityClass = GetEntityClassUtil
.getEntityClass(getClass());
@Override
public void save(T entity) {
dao.save(entity);
}
@Override
public void update(T entity) {
dao.update(entity);
}
@Override
public List<T> findAll() {
return dao.findAll(entityClass);
}
@Override
public void delete(PK... pks) {
dao.delete(entityClass, pks);
}
@Override
public Pager<T> findByPage(String xql, int pageNo, int pageSize) {
return dao.findByPage(entityClass, xql, pageNo, pageSize);
}
@Override
public Pager<T> findByPage(String xql, Object key, int pageNo, int pageSize) {
return dao.findByPage(entityClass, xql, key, pageNo, pageSize);
}
@Override
public Pager<T> findByPage(String xql, Object[] keys, int pageNo, int pageSize) {
return dao.findByPage(entityClass, xql, keys, pageNo, pageSize);
}
@Override
public T findEntity(PK pk) {
return dao.findEntity(entityClass, pk);
}
@Override
public T findEntity(String hql, PK pk) {
return dao.findEntity(hql, pk);
}
}
base层使用到得工具类:
public class GetEntityClassUtil {
@SuppressWarnings("rawtypes")
public static Class getEntityClass(Class c) {
Type type = c.getGenericSuperclass();
if (type instanceof ParameterizedType) {
Type[] param = ((ParameterizedType) type).getActualTypeArguments();
return (Class) param[0];
} else {
return Object.class;
}
}
}
这是我的service层相关代码:
@Service("userService")
public class UserServiceImpl extends AbstractBaseDao<User,Integer> implements UserServiceI {
}
我的action类里相关代码这样写:
@ParentPackage("user_module")
@Namespace("/user")
@Action(results={@Result(name="findAll",location="/index.jsp"),
@Result(name="success",location="/user/success.jsp"),
@Result(name="findByPage",location="/user/findAll.jsp"),
@Result(name="showUpdate",location="/user/showUpdate.jsp")},value="userAction")
public class UserAction extends BaseAction{
private static final long serialVersionUID = -6145564175757598422L;
private UserServiceI userService;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Resource(name="userService")
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
public String showUsers(){
List<User> users = userService.findAll();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("users", users);
return "findAll";
}
public String deleteUser() {
userService.delete(new Integer[]{ id });
return "success";
}
public String addUser(){
userService.save(user);
return ActionSupport.SUCCESS;
}
public String showUpdate(){
user = userService.findEntity("from User u where u.id=?", id);
return "showUpdate";
}
public String updateUser(){
user.setId(id);
userService.update(user);
return "success";
}
public String findByPage() {
int pageNo = 0;
String str_pageNo = getParameter("pager.offset");
if (str_pageNo != null) {
pageNo = Integer.parseInt(str_pageNo);
}
Pager<User> pager = userService.findByPage("from User", pageNo, 3);
setAttribute("pager", pager);
return "findByPage";
}
}
但就是update和delete方法发出不了sql语句,这是为什么 ssh整合 hibernate4 --------------------编程问答-------------------- 不应该这么诡异,楼主再好好检查一下代码 --------------------编程问答-------------------- 确实这么诡异,但是经过我又一轮测试后,发现这个问题只出现在4.2.3这个hibernate中,换成其他的4.x版本的hibernate用现在的代码就可以了,这算不算是一个bug --------------------编程问答-------------------- LZ的代码风格和我的很像很像 --------------------编程问答-------------------- 哦哟~像就好~来同志~抱一个~
补充:Java , Java EE