Hibernate之QBC--》用语法函数表示sql语句【个人感觉此技术相当之垃圾,反其正道而行】(二十七)
2013-12-30 14:09
399 查看
1
package com.bjsxt.hibernate;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FetchMode;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class HibernateQLTest {
private static SessionFactory sf;
@BeforeClass
public static void beforeClass() {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
session.save(c);
}
for(int i=0; i<10; i++) {
Category c = new Category();
c.setId(1);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(t);
}
for(int i=0; i<10; i++) {
Topic t = new Topic();
t.setId(1);
Msg m = new Msg();
m.setCont("m" + i);
m.setTopic(t);
session.save(m);
}
session.getTransaction().commit();
session.close();
}
//is empty and is not empty
@Test
public void testQBC() {
Session session = sf.openSession();
session.beginTransaction();
//criterion 标准/准则/约束
Criteria c = session.createCriteria(Topic.class) //from Topic
.add(Restrictions.gt("id", 2)) //greater than = id > 2
.add(Restrictions.lt("id", 8)) //little than = id < 8
.add(Restrictions.like("title", "t_"))
.createCriteria("category")
.add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=5
;
//DetachedCriterea
for(Object o : c.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
public static void main(String[] args) {
beforeClass();
new HibernateQLTest().testQBC();
}
}
package com.bjsxt.hibernate;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FetchMode;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class HibernateQLTest {
private static SessionFactory sf;
@BeforeClass
public static void beforeClass() {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
session.save(c);
}
for(int i=0; i<10; i++) {
Category c = new Category();
c.setId(1);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(t);
}
for(int i=0; i<10; i++) {
Topic t = new Topic();
t.setId(1);
Msg m = new Msg();
m.setCont("m" + i);
m.setTopic(t);
session.save(m);
}
session.getTransaction().commit();
session.close();
}
//is empty and is not empty
@Test
public void testQBC() {
Session session = sf.openSession();
session.beginTransaction();
//criterion 标准/准则/约束
Criteria c = session.createCriteria(Topic.class) //from Topic
.add(Restrictions.gt("id", 2)) //greater than = id > 2
.add(Restrictions.lt("id", 8)) //little than = id < 8
.add(Restrictions.like("title", "t_"))
.createCriteria("category")
.add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=5
;
//DetachedCriterea
for(Object o : c.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
public static void main(String[] args) {
beforeClass();
new HibernateQLTest().testQBC();
}
}
相关文章推荐
- Hibernate之QBE--》用语法函数表示sql语句【个人感觉此技术相当之垃圾,反其正道而行】(二十八)
- PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标) (转)
- 个人收集的一些常用的sql查询语句与函数
- PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)
- 从SQL Server向Oracle迁移的技术实现方案(七)常用SQL语法与函数
- oracle官方文档之SQL语句语法的图形表示法
- SQL 常用语句以及函数(个人收藏)
- sql 语句 编写一个函数用来精确计算某个人的当前年龄(精确到一位小数),比如:20.3岁
- Hibernate中的三种查询语句(hql,sql,qbc)
- SQL2008--SQL语句-存储过程-触发器-事务处理-基本语法-函数
- 在hibernate中,对数据库的增删改操作以及HQL查询与QBC查询的语句的编写语法
- SQL中exists函数用法,转为Hibernate的QBC查询
- PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)
- MySQL 最基本的SQL语法/语句
- Hibernate使用sql语句实现多表关联查询
- sql语句case when使用和date_formate()的使用,DATEDIFF(字段1,字段2)获取天数,常用的MYSQL的函数
- 使用Hibernate 操作原生sql语句建表
- SQL语句的基本语法
- sql语句中decode()函数的嵌套(oracle)
- 四、oracle基本sql语句和函数详解