hibernate-hql-18

public class HQLThreeTest {
    public static SessionFactory sf = null;

    @BeforeClass
    public static void beforeClass(){
        Configuration cfg = new AnnotationConfiguration();
        sf = cfg.configure().buildSessionFactory();
    }

    //  类中的某字段对应的集合是否为空
    //  注意设置  @one2many
    @Test
    public void testHQL_20(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("  from Topic t where t.msgs is empty ");

        List<Topic> topics = (List<Topic>)q.list();
        for(Topic t:topics){
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
    }

    @Test
    public void testHQL_21(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("  from Topic t where t.title like '%5' ");

        List<Topic> topics = (List<Topic>)q.list();
        for(Topic t:topics){
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
    }

    // hql中还可以使用一些函数  不重要
    @Test
    public void testHQL_22(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery(" select  lower(t.title)," +
                "upper(t.title)," +
                "trim(t.title)," +
                "concat(t.title,'***')," +
                "length(t.title)" +
                "  from Topic t  ");

        for(Object o:q.list()){
            Object[] t = (Object[])o;
            System.out.println(t[0]+"-"+t[1]);
        }
        session.getTransaction().commit();
    }

    @Test
    public void testHQL_25(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery(" select current_date ,current_time ,current_timestamp ,t.id from Topic t ");

        for(Object o:q.list()){
            Object[] t = (Object[])o;
            System.out.println(t[0]+"-"+t[1]+"-"+t[2]+"-"+t[3]);
        }
        session.getTransaction().commit();
    }

    @Test
    public void testHQL_31(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery(" from Topic t where not exists (select m.id from Msg m where t.id = m.topic.id)");

        List<Topic> topics = (List<Topic>)q.list();
        for(Topic t:topics){
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
    }

    // update
    @Test
    public void testHQL_32(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery(" update Topic t set t.title = upper(t.title) ");
        q.executeUpdate();

        q = session.createQuery(" from Topic ");
        List<Topic> topics = (List<Topic>)q.list();
        for(Topic t:topics){
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
    }

    //  支持命名查询(不重要  好处是方便sql统一管理)



    // native query(了解)
    @Test
    public void testHQL_34(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        SQLQuery q = session.createSQLQuery("  select * from category limit 2,4 ").addEntity(Category.class);

        List<Topic> topics = (List<Topic>)q.list();
        for(Topic t:topics){
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
    }




    @Test
    public void testSchemaExport(){
        //  true  是否将建表语句输出到控制台   true  是否在数据库中建表
        new SchemaExport(new AnnotationConfiguration().configure()).create(true,true);
    }


    @AfterClass
    public static void AfterClass(){
        sf.close();

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值