Hibernate 中update hql语句
今天在MySQL中用hibernate测试update语句发现以下问题:
update语句竟然不去作用;
表机构如下:
create table student
(sid int primary key ,
sname varchar(45) not null,
ssex char(2) not null,
sdept varchar(10) not null,
sage int ,
saddress varchar(45)
);
update语句如下:
String[] params = new String[] { "20", "成龙" };
HibernateUtil.executeUpdate(
"update Student s set s.sage=? where s.sname=?",params);
package com.huml.util; import java.util.ArrayList; import java.util.Arrays; import org.hibernate.Transaction; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sf; private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); static { sf = new Configuration().configure().buildSessionFactory(); } private HibernateUtil() { } public static Session openSession() { return sf.openSession(); } public static Session getcurrentSession() { Session session = threadLocal.get(); if (session == null) { session = sf.openSession(); threadLocal.set(session); } return session; } public static void save(Object obj) { Session session = openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(obj); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } } @SuppressWarnings("unchecked") public static ArrayList executeQuery(String hql, String... params) { Session session = openSession(); Transaction tx = null; ArrayList list = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); } } list = (ArrayList) query.list(); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } return list; } public static void executeUpdate(String hql, String... params) { Session session = openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); // System.out.println("query influenced: "+params[i]); } } System.out.println("query influenced: " + query.getQueryString()); int n = query.executeUpdate(); System.out.println("query influence: " + n); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } } }
原因是MySQL中不能自动把String类型转换成Integer,在Oracle中可以修改代码如下:
Object[] params = new Object[] { 20, "成龙" };
HibernateUtil.executeUpdate(
"update Student s set s.sage=? where s.sname=?",params);
package com.huml.util; import java.util.ArrayList; import java.util.Arrays; import org.hibernate.Transaction; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sf; private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); static { sf = new Configuration().configure().buildSessionFactory(); } private HibernateUtil() { } public static Session openSession() { return sf.openSession(); } public static Session getcurrentSession() { Session session = threadLocal.get(); if (session == null) { session = sf.openSession(); threadLocal.set(session); } return session; } public static void save(Object obj) { Session session = openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(obj); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } } @SuppressWarnings("unchecked") public static ArrayList executeQuery(String hql, String... params) { Session session = openSession(); Transaction tx = null; ArrayList list = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); } } list = (ArrayList) query.list(); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } return list; } public static void executeUpdate(String hql, Object... params) { Session session = openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { query.setParameter(i, params[i]); // System.out.println("query influenced: "+params[i]); } } System.out.println("query influenced: " + query.getQueryString()); int n = query.executeUpdate(); System.out.println("query influence: " + n); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e.getMessage()); } finally { if (session != null && session.isOpen()) { session.close(); } } } }
这样就成功了;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。