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();
}
}
hibernate-hql-18
最新推荐文章于 2022-12-28 12:31:02 发布