public class HQLTwoTest {
public static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
Configuration cfg = new AnnotationConfiguration();
sf = cfg.configure().buildSessionFactory();
}
// 注意uniqeResult的使用
@Test
public void testHQL_15(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery(" select count(*) from Msg m ");
long count = (Long)q.uniqueResult();
System.out.println(count);
session.getTransaction().commit();
}
@Test
public void testHQL_16(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery(" select max(m.id),min(m.id),avg(m.id),sum(m.id) from Msg m");
Object[] o = (Object[])q.uniqueResult();
System.out.println(o[1]+"-"+o[1]+"-"+o[2]);
session.getTransaction().commit();
}
@Test
public void testHQL_17(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery(" from Msg m where m.id between 3 and 5");
List<Msg> msgs = (List<Msg>)q.list();
for(Msg m:msgs){
System.out.println(m.getCont());
}
session.getTransaction().commit();
}
@Test
public void testHQL_18(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery(" from Msg m where m.id in(3,4,5) ");
List<Msg> msgs = (List<Msg>)q.list();
for(Msg m:msgs){
System.out.println(m.getCont());
}
session.getTransaction().commit();
}
@Test
public void testHQL_19(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Query q = session.createQuery(" from Msg m where m.id is not null ");
List<Msg> msgs = (List<Msg>)q.list();
for(Msg m:msgs){
System.out.println(m.getCont());
}
session.getTransaction().commit();
}
@Test
public void testSchemaExport(){
// true 是否将建表语句输出到控制台 true 是否在数据库中建表
new SchemaExport(new AnnotationConfiguration().configure()).create(true,true);
}
@AfterClass
public static void AfterClass(){
sf.close();
}
}
hibernate-HQL-17
最新推荐文章于 2018-10-15 14:43:15 发布