码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate 中update hql语句

时间:2014-06-25 13:58:02      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   color   

今天在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();
            }
        }
    }
}

这样就成功了;

Hibernate 中update hql语句,布布扣,bubuko.com

Hibernate 中update hql语句

标签:style   class   blog   code   java   color   

原文地址:http://www.cnblogs.com/ronaldHU/p/3806681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!