查询方式:
1. NativeSQL (Mysql, oracle, sql server等)>HQL>EJBQL
2. QL应该与导航关系结合, 共同为查询提供服务
category->topic: one to more:
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Category { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
Topic->msg: one to more:
package com.bjsxt.hibernate;import java.util.Date;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;@Entitypublic class Topic { private int id; private String title; private Category category; //private Category category2; private Date createDate; public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @ManyToOne(fetch=FetchType.LAZY) public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
msg.java;
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;@Entitypublic class Msg { private int id; private String cont; private Topic topic; @ManyToOne public Topic getTopic() { return topic; } public void setTopic(Topic topic) { this.topic = topic; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCont() { return cont; } public void setCont(String cont) { this.cont = cont; } }
hibernate.cfg.xml:
com.mysql.jdbc.Driver jdbc:mysql://localhost/hibernate root linda0213 org.hibernate.dialect.MySQLDialect 1 thread org.hibernate.cache.NoCacheProvider true true
test文件:
package com.bjsxt.hibernate;import java.util.Date;import java.util.List;import org.hibernate.Query; import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;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(); } @Test public void testHQL_01() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from Category"); Listcategories = (List )q.list(); for(Category c : categories) { System.out.println(c.getId()+" "+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 categories = (List )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 categories = (List )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 categories = (List )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", 8); q.setInteger("min", 2); q.setInteger("max", 8);*/ Query q = session.createQuery("from Category c where c.id > :min and c.id < :max") .setInteger("min", 2) .setInteger("max", 8); List categories = (List )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, 8);// q.setParameter(1, 8); List categories = (List )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 categories = (List )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