- 浏览: 247889 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (205)
- jQuery (27)
- Flash AS3.0 (0)
- Html5+CSS3 (12)
- C# .Net (19)
- Xml XPath XSLT (5)
- Sql (3)
- JavaScript (20)
- struts2 (23)
- hibernate (24)
- spring (11)
- j2ee (16)
- jsp/java (11)
- 设计模式 (5)
- json (3)
- Java.IO (7)
- Java.Util (7)
- android (8)
- WebService (10)
- MyEclipse SVN (3)
- servlet (1)
- Exception (3)
- 自我学习 (2)
- UML (2)
- java泛型 (1)
- Lucene (7)
- HtmlParser (2)
- 概念理解 (3)
- 正则表达式 (1)
- EMail (1)
最新评论
-
hanmiao:
没用,干巴巴的壹堆代码,没明白到底区别在哪里。
List Set Map 区别! -
e421083458:
偶来过了!
C#单向链表的实现
Hibernate 查询语言
NativeSQL(不执行跨平台)
Hql(hibernate HQL)
EjBAL(JPQL1.0)是HQL的子集
QBC(Query BY Cretira)
QBE(Query By Example)
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();
}
@Test
public void testHQL_01() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_02() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.name > 'c5'");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_03() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_04() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select distinct c from Category c order by c.name desc");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_05() {
Session session = sf.openSession();
session.beginTransaction();
/*Query q = session.createQuery("from Category c where c.id > :min and c.id < :max");
//q.setParameter("min", 2);
//q.setParameter("max",;
q.setInteger("min", 2);
q.setInteger("max",;*/
Query q = session.createQuery("from Category c where c.id > :min and c.id < :max")
.setInteger("min", 2)
.setInteger("max",;
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_06() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.id > ? and c.id < ?");
q.setParameter(0, 2)
.setParameter(1,;
// q.setParameter(1,;
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
//分页
@Test
public void testHQL_07() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
q.setMaxResults(4);
q.setFirstResult(2);
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_08() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select c.id, c.name from Category c order by c.name desc");
List<Object[]> categories = (List<Object[]>)q.list();
for(Object[] o : categories) {
System.out.println(o[0] + "-" + o[1]);
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_09() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = (List<Topic>)q.list();
for(Topic t : topics) {
System.out.println(t.getTitle());
//System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_10() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = (List<Topic>)q.list();
for(Topic t : topics) {
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_11() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.topic.category.id = 1");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//了解即可
//VO Value Object
//DTO data transfer object
@Test
public void testHQL_12() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select new com.bjsxt.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg");
for(Object o : q.list()) {
MsgInfo m = (MsgInfo)o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//动手测试left right join
//为什么不能直接写Category名,而必须写t.category
//因为有可能存在多个成员变量(同一个类),需要指明用哪一个成员变量的连接条件来做连接
@Test
public void testHQL_13() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, c.name from Topic t join t.category c "); //join Category c
for(Object o : q.list()) {
Object[] m = (Object[])o;
System.out.println(m[0] + "-" + m[1]);
}
session.getTransaction().commit();
session.close();
}
//学习使用uniqueResult
@Test
public void testHQL_14() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m = :MsgToSearch "); //不重要
Msg m = new Msg();
m.setId(1);
q.setParameter("MsgToSearch", m);
Msg mResult = (Msg)q.uniqueResult();
System.out.println(mResult.getCont());
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_15() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select count(*) from Msg m");
long count = (Long)q.uniqueResult();
System.out.println(count);
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_16() {
Session session = sf.openSession();
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[0] + "-" + o[1] + "-" + o[2] + "-" + o[3]);
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_17() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.id between 3 and 5");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_18() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.id in (3,4, 5)");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
//is null 与 is not null
@Test
public void testHQL_19() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.cont is not null");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
//is empty and is not empty
@Test
public void testHQL_20() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.msgs is empty");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_21() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.title like '%5'");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_22() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.title like '_5'");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//不重要
@Test
public void testHQL_23() {
Session session = sf.openSession();
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[] arr = (Object[])o;
System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] + "-" + arr[3] + "-" + arr[4] + "-");
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_24() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select abs(t.id)," +
"sqrt(t.id)," +
"mod(t.id, 2)" +
" from Topic t ");
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] );
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_25() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select current_date, current_time, current_timestamp, t.id from Topic t");
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + " | " + arr[1] + " | " + arr[2] + " | " + arr[3]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_26() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.createDate < :date");
q.setParameter("date", new Date());
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_27() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title") ;
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "|" + arr[1]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_28() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title having count(*) >= 1") ;
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "|" + arr[1]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_29() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.id < (select avg(t.id) from Topic t)") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_30() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id, 2)= 0) ") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//用in 可以实现exists的功能
//但是exists执行效率高
@Test
public void testHQL_31() {
Session session = sf.openSession();
session.beginTransaction();// t.id not in (1)
Query q = session.createQuery("from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)") ;
// Query q = session.createQuery("from Topic t where exists (select m.id from Msg m where m.topic.id=t.id)") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//update and delete
//规范并没有说明是不是要更新persistent object,所以如果要使用,建议在单独的trasaction中执行
@Test
public void testHQL_32() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("update Topic t set t.title = upper(t.title)") ;
q.executeUpdate();
q = session.createQuery("from Topic");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.createQuery("update Topic t set t.title = lower(t.title)")
.executeUpdate();
session.getTransaction().commit();
session.close();
}
//不重要
@Test
public void testHQL_33() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.getNamedQuery("topic.selectCertainTopic");
q.setParameter("id", 5);
Topic t = (Topic)q.uniqueResult();
System.out.println(t.getTitle());
session.getTransaction().commit();
session.close();
}
//Native(了解)
@Test
public void testHQL_34() {
Session session = sf.openSession();
session.beginTransaction();
SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_35() {
//尚未实现JPA命名的NativeSQL
}
public static void main(String[] args) {
beforeClass();
}
}
NativeSQL(不执行跨平台)
Hql(hibernate HQL)
EjBAL(JPQL1.0)是HQL的子集
QBC(Query BY Cretira)
QBE(Query By Example)
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();
}
@Test
public void testHQL_01() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_02() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.name > 'c5'");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_03() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_04() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select distinct c from Category c order by c.name desc");
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_05() {
Session session = sf.openSession();
session.beginTransaction();
/*Query q = session.createQuery("from Category c where c.id > :min and c.id < :max");
//q.setParameter("min", 2);
//q.setParameter("max",;
q.setInteger("min", 2);
q.setInteger("max",;*/
Query q = session.createQuery("from Category c where c.id > :min and c.id < :max")
.setInteger("min", 2)
.setInteger("max",;
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_06() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c where c.id > ? and c.id < ?");
q.setParameter(0, 2)
.setParameter(1,;
// q.setParameter(1,;
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
//分页
@Test
public void testHQL_07() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Category c order by c.name desc");
q.setMaxResults(4);
q.setFirstResult(2);
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getId() + "-" + c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_08() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select c.id, c.name from Category c order by c.name desc");
List<Object[]> categories = (List<Object[]>)q.list();
for(Object[] o : categories) {
System.out.println(o[0] + "-" + o[1]);
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_09() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = (List<Topic>)q.list();
for(Topic t : topics) {
System.out.println(t.getTitle());
//System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close();
}
//设定fetch type 为lazy后将不会有第二条sql语句
@Test
public void testHQL_10() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.category.id = 1");
List<Topic> topics = (List<Topic>)q.list();
for(Topic t : topics) {
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_11() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.topic.category.id = 1");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//了解即可
//VO Value Object
//DTO data transfer object
@Test
public void testHQL_12() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select new com.bjsxt.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg");
for(Object o : q.list()) {
MsgInfo m = (MsgInfo)o;
System.out.println(m.getCont());
}
session.getTransaction().commit();
session.close();
}
//动手测试left right join
//为什么不能直接写Category名,而必须写t.category
//因为有可能存在多个成员变量(同一个类),需要指明用哪一个成员变量的连接条件来做连接
@Test
public void testHQL_13() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, c.name from Topic t join t.category c "); //join Category c
for(Object o : q.list()) {
Object[] m = (Object[])o;
System.out.println(m[0] + "-" + m[1]);
}
session.getTransaction().commit();
session.close();
}
//学习使用uniqueResult
@Test
public void testHQL_14() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m = :MsgToSearch "); //不重要
Msg m = new Msg();
m.setId(1);
q.setParameter("MsgToSearch", m);
Msg mResult = (Msg)q.uniqueResult();
System.out.println(mResult.getCont());
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_15() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select count(*) from Msg m");
long count = (Long)q.uniqueResult();
System.out.println(count);
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_16() {
Session session = sf.openSession();
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[0] + "-" + o[1] + "-" + o[2] + "-" + o[3]);
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_17() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.id between 3 and 5");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_18() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.id in (3,4, 5)");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
//is null 与 is not null
@Test
public void testHQL_19() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Msg m where m.cont is not null");
for(Object o : q.list()) {
Msg m = (Msg)o;
System.out.println(m.getId() + "-" + m.getCont());
}
session.getTransaction().commit();
session.close();
}
//is empty and is not empty
@Test
public void testHQL_20() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.msgs is empty");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_21() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.title like '%5'");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_22() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.title like '_5'");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getId() + "-" + t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//不重要
@Test
public void testHQL_23() {
Session session = sf.openSession();
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[] arr = (Object[])o;
System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] + "-" + arr[3] + "-" + arr[4] + "-");
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_24() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select abs(t.id)," +
"sqrt(t.id)," +
"mod(t.id, 2)" +
" from Topic t ");
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] );
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_25() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select current_date, current_time, current_timestamp, t.id from Topic t");
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + " | " + arr[1] + " | " + arr[2] + " | " + arr[3]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_26() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.createDate < :date");
q.setParameter("date", new Date());
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_27() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title") ;
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "|" + arr[1]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_28() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title having count(*) >= 1") ;
for(Object o : q.list()) {
Object[] arr = (Object[])o;
System.out.println(arr[0] + "|" + arr[1]);
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_29() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.id < (select avg(t.id) from Topic t)") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_30() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id, 2)= 0) ") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//用in 可以实现exists的功能
//但是exists执行效率高
@Test
public void testHQL_31() {
Session session = sf.openSession();
session.beginTransaction();// t.id not in (1)
Query q = session.createQuery("from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)") ;
// Query q = session.createQuery("from Topic t where exists (select m.id from Msg m where m.topic.id=t.id)") ;
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.getTransaction().commit();
session.close();
}
//update and delete
//规范并没有说明是不是要更新persistent object,所以如果要使用,建议在单独的trasaction中执行
@Test
public void testHQL_32() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.createQuery("update Topic t set t.title = upper(t.title)") ;
q.executeUpdate();
q = session.createQuery("from Topic");
for(Object o : q.list()) {
Topic t = (Topic)o;
System.out.println(t.getTitle());
}
session.createQuery("update Topic t set t.title = lower(t.title)")
.executeUpdate();
session.getTransaction().commit();
session.close();
}
//不重要
@Test
public void testHQL_33() {
Session session = sf.openSession();
session.beginTransaction();
Query q = session.getNamedQuery("topic.selectCertainTopic");
q.setParameter("id", 5);
Topic t = (Topic)q.uniqueResult();
System.out.println(t.getTitle());
session.getTransaction().commit();
session.close();
}
//Native(了解)
@Test
public void testHQL_34() {
Session session = sf.openSession();
session.beginTransaction();
SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
List<Category> categories = (List<Category>)q.list();
for(Category c : categories) {
System.out.println(c.getName());
}
session.getTransaction().commit();
session.close();
}
@Test
public void testHQL_35() {
//尚未实现JPA命名的NativeSQL
}
public static void main(String[] args) {
beforeClass();
}
}
发表评论
-
java的枚举的定义和使用
2012-01-12 23:29 1198public emun EmunName { w ... -
常用SQL语句
2011-12-31 11:33 648MySql分组查询语句: from Share as s ... -
hibernate对映射xml的一对多的双向关联详解
2011-12-18 00:15 878<?xml version="1.0" ... -
getHibernateTemplate().execute(new HibernateCallback())方法
2011-12-14 09:43 4471. HibernateCallback是一个 ... -
Java企业级开发SSH三大框架的Jar包
2011-12-06 18:39 946123 -
struts spring hibernate配置文件
2011-10-24 21:24 952ApplicationContext ctx = new Cl ... -
Annotation的关联关系
2011-10-19 23:22 690一对一、一对多、多对多、组合映射、主键关系 的单向和双向 ... -
hibernate 名词解释
2011-10-18 23:20 753JPA是一个标准(接口),hibernate只是一个实现 先有 ... -
Junit测试文件
2011-10-18 22:19 828package com.bjsxt.hibernate; ... -
log4j.properties
2011-10-18 22:00 733### direct log messages to stdo ... -
Hibernate级联操作
2011-09-23 00:15 878package com.secn.user; import ... -
Hibernate关系映射
2011-09-22 22:14 652关系映射: 对象之间的关系 一对一(单向和双向) Annont ... -
Hibenrate一对多和多对多
2011-09-20 23:18 337... -
Hibernate联合主键
2011-09-20 22:35 787package com.bjsxt.hibernate; i ... -
Hibernate一对一的关联关系
2011-09-20 22:30 672... -
Hbernate一对一单向关系(Annotation)
2011-09-20 21:54 696package com.bjsxt.hibernate.mod ... -
Load()和Get()的区别和update用法
2011-09-20 20:03 969Load(Teacher.Class,1); Teacher. ... -
Configuration_SessionFactory_GetCurrentSession_OpenSession以及Hibernate中的三种状态
2011-09-19 21:55 761SessionFactory sf = new Configu ... -
常用的hiberante中xml的ID生成策略
2011-09-19 21:09 1151hiberante的xml生成策略: 策略一般是指:1. 可以 ... -
hibernate Annotation版本持久化实体类
2011-09-18 01:16 733package com.bjsxt.hibernate; ...
相关推荐
### Hibernate HQL 关联查询详解 #### 一、引言 在进行数据库操作时,关联查询是必不可少的一部分,尤其在处理复杂的数据结构时更是如此。Hibernate作为一种流行的Java持久层框架,提供了强大的对象关系映射(ORM...
《Hibernate HQL查询语法详解》 Hibernate,作为Java领域中广泛应用的对象关系映射(ORM)框架,极大地简化了数据库操作。其内置的HQL(Hibernate Query Language)是一种面向对象的查询语言,它允许开发者以类和...
Hibernate HQL基础练习小列子+数据库
此压缩包中的`HibernateHQL.chm`文件很可能是Hibernate HQL的详细教程或参考手册,包含了HQL的语法、用法示例、最佳实践以及可能遇到的问题解决方案。它可以帮助开发者深入理解如何在实际项目中高效地使用HQL进行...
根据提供的文件信息,我们可以深入探讨Hibernate HQL(Hibernate Query Language)的相关知识点,特别是关于其查询功能、连接操作、条件过滤以及聚合函数的应用等。 ### Hibernate HQL简介 Hibernate HQL是一种...
【标题】:“Hibernate HQL 结合 velocity” 在Java开发中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库操作,使得开发者能够用Java对象来处理数据库中的数据。而Velocity则是一个模板引擎,它...
本程序“HibernateHql综合测试小程序”是针对Hibernate中的HQL(Hibernate Query Language)进行的一系列功能测试,涵盖了二十多种不同的HQL语句,旨在帮助开发者深入理解和熟练运用Hibernate的查询能力。...
Hibernate HQL,全称为Hibernate Query Language,是Hibernate框架中用于对象关系映射(ORM)的查询语言。HQL的设计目标是提供一种面向对象的查询方式,使得开发者能够以类和对象的角度来操作数据库,而不是直接使用...
### Hibernate HQL 基本操作详解 #### 一、HQL简介 HQL,即Hibernate Query Language,是Hibernate提供的一种强大的查询语言。虽然它的语法结构看起来与SQL类似,但其实质上是一种面向对象的语言,能够理解并处理...
HQL(Hibernate Query Language)是Hibernate提供的一个SQL的面向对象的查询语言,它使得开发者可以避免直接编写SQL语句,从而更好地进行数据库操作。本文将深入探讨“hibernate hql where语句拼接工具类”,并结合...
"HibernateHQL实现前台翻页"这一主题就是关于如何利用Hibernate的HQL(Hibernate Query Language)来实现数据库查询的分页功能,并结合Servlet(通常与MVC模式中的Controller层对应)和MySQL数据库来构建一个完整的...
Hibernate HQL 查询语句总结 Hibernate HQL 查询语句是 Hibernate 框架中的一种查询语言,它提供了更加丰富的和灵活的查询特性,具有类似标准 SQL 语句的查询方式,同时也提供了更加面向对象的封装。以下是 ...
### Hibernate HQL 查询详解 #### 一、HQL(Hibernate Query Language)简介 HQL(Hibernate Query Language)是一种面向对象的查询语言,它允许开发者使用类及其属性来编写查询语句,而不是传统的SQL语句。这使得...
【Hibernate HQL 深度历险】是一个关于掌握Hibernate查询语言高级特性的技术主题。Hibernate Query Language(HQL)是Hibernate框架提供的面向对象的查询语言,它与SQL类似但针对对象模型,允许开发者直接查询对象而...
Hibernate HQL,全称为Hibernate Query Language,是Hibernate框架中用于操作数据库的一种面向对象的查询语言。它允许开发者用面向对象的方式来编写查询语句,极大地提高了开发效率和代码的可读性。以下是对HQL的...
对hibernate的hql进行了详尽的讲解
### Hibernate HQL 基本查询详解 #### 一、HQL 概述 HQL (Hibernate Query Language) 是一种面向对象的查询语言,由 Hibernate 官方提供,用于简化数据库查询过程。与传统的 JDBC 方式相比,使用 HQL 进行数据库...
在Java的持久化框架Hibernate中,HQL(Hibernate Query Language)是用于操作对象关系映射(ORM)的SQL方言,提供了更加面向对象的查询方式。本篇将详细讲解HQL中的命名参数以及如何通过Query接口实现分页查询,这...
### Hibernate HQL 查询详解 #### 一、HQL 概述 Hibernate 提供了一种非常强大的查询语言——HQL(Hibernate Query Language),它在语法上与 SQL 类似但功能更为强大,因为它支持面向对象的特性,例如继承、多态性...
本文档主要讨论了使用Hibernate的HQL(Hibernate Query Language)进行级联查询时需要注意的一些事项。 1. 数据库模型 - `Employee`表:包含`id`(主键)、`empname`(员工姓名)、`empage`(员工年龄)、`context...