首页 > 代码库 > Hibernate 中update hql语句
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(); } } }}
这样就成功了;
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。