Hibernate 原生SQL操作

package cn.sasa.test;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.sasa.domain.account;
import cn.sasa.utils.HibernateUtils;

public class TestSQL {
    @org.junit.Test
    public void test() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction tran = session.beginTransaction();
        
        String sql="select * from account where id>:id";
        var query = session.createSQLQuery(sql);
//        List<Object[]> list =  query.list();
//        for(Object[] item : list){
//            System.out.println(Arrays.toString(item));
//        }
        query.setParameter("id", 2);
        query.addEntity(account.class);
        List<account> aclist = query.list();
        System.out.println(aclist);
        
        tran.commit();
        session.close();
    }
    
    
}

 

posted @ 2019-10-11 17:01  SasaL  Views(1092)  Comments(0Edit  收藏  举报