- 浏览: 82491 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
yangfuchao418:
楼主你这个实在太simple了。你可以用htmlunit。
commons-httpclient 的使用(模拟浏览器) -
fengfeng925:
linliangyi2007 写道sunson468 写道有那 ...
用反射注解实现的ORM(一看就明白的那种) -
pclfs1983:
楼上说的很有道理
用反射注解实现的ORM(一看就明白的那种) -
linliangyi2007:
楼主的工作还是很值得实践一次的,尤其是学习研究阶段。写过和看过 ...
用反射注解实现的ORM(一看就明白的那种) -
linliangyi2007:
sunson468 写道有那么多开源的成熟的架构,除非非常有必 ...
用反射注解实现的ORM(一看就明白的那种)
例子: Entity-> package com.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public 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; } } package com.hibernate.model; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; @Entity @NamedQueries( { @NamedQuery(name="topic.selectCertainTopic",query="from Topic t where t.id = :id") } ) public class Topic { private int id; private String title; private Category category; private Date createDate; private List<Msg> msgs = new ArrayList<Msg>(); @OneToMany(mappedBy="topic") public List<Msg> getMsgs() { return msgs; } public void setMsgs(List<Msg> msgs) { this.msgs = msgs; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @ManyToOne(fetch=FetchType.LAZY) public Category getCategory() { return category; } @Id @GeneratedValue public int getId() { return id; } public String getTitle() { return title; } public void setCategory(Category category) { this.category = category; } public void setId(int id) { this.id = id; } public void setTitle(String title) { this.title = title; } } package com.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Msg { private int id; private String cont; private 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; } @ManyToOne public Topic getTopic() { return topic; } public void setTopic(Topic topic) { this.topic = topic; } } package com.hibernate.model; public class MsgInfo { private int id; private String topicName; private String cont; private String categoryName; public MsgInfo(int id, String cont, String topicName, String categoryName) { super(); this.id = id; this.cont = cont; this.topicName = topicName; this.categoryName = categoryName; } public String getCategoryName() { return categoryName; } public String getCont() { return cont; } public int getId() { return id; } public String getTopicName() { return topicName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public void setCont(String cont) { this.cont = cont; } public void setId(int id) { this.id = id; } public void setTopicName(String topicName) { this.topicName = topicName; } } 测试-> package com.hibernate.test; 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; import com.hibernate.model.Category; import com.hibernate.model.Msg; import com.hibernate.model.MsgInfo; import com.hibernate.model.Topic; public class HQLTest { private static SessionFactory sf = null; @BeforeClass public static void beforeClass() { // new SchemaExport(new // AnnotationConfiguration().configure()).create(false, true); sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void afterClass() { sf.close(); } @Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create( false, true); } public static void main(String[] args) { new HQLTest().testSchemaExport(); } // 保存 @Test public void testSave() { Session session = sf.getCurrentSession(); 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.setTopic(t); m.setCont("m" + i); session.save(m); } session.getTransaction().commit(); } @Test public void testHQL_01() { Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category"); List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_02() { Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category c where c.name> 'c5'"); List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_03() { Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category c order by c.name desc"); List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_04() { Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select distinct c from Category c order by c.name desc"); List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_05(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category c where c.id > :min and c.id < :max") .setInteger("min", 2) .setInteger("max", 8) ; List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_06(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category c where c.id > ? and c.id < ?") .setParameter(0, 2) .setParameter(1, 8) ; List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getName()); } s.getTransaction().commit(); } //分页 @Test public void testHQL_07(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Category c order by c.name asc"); q.setMaxResults(4); q.setFirstResult(2); List<Category> categorys = (List<Category>) q.list(); for (Category c : categorys) { System.out.println(c.getId() + "-" + c.getName()); } s.getTransaction().commit(); } @Test public void testHQL_08(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select c.id, c.name from Category c order by c.name desc"); List<Object[]> cs = (List<Object[]>)q.list(); for(Object[] o : cs){ System.out.println(o[0] + "--"+ o[1]); } s.getTransaction().commit(); } //设定fetch type 为lazy后将不会有第二条sql语句 @Test public void testHQL_09(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_011(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Msg m where m.topic.category.id = 1"); for(Object o : q.list()){ Msg m = (Msg)o; System.out.println(m.getCont()); } s.getTransaction().commit(); } //DTO VO @Test public void testHQL_012(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select new com.hibernate.model.MsgInfo(m.id,m.cont,m.topic.title,m.topic.category.name) from Msg m") ; for(Object o : q.list()){ MsgInfo m = (MsgInfo)o; System.out.println(m.getCont()); } s.getTransaction().commit(); } @Test public void testHQL_013(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select t.title, c.name from Topic t join t.category c"); for(Object o :q.list()){ Object[] m = (Object[])o; System.out.println(m[0]+"---"+ m[1]); } s.getTransaction().commit(); } @Test public void testHQL_014(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Msg m where m = :m"); Msg m = new Msg(); m.setId(1); q.setParameter("m", m); Msg mResult = (Msg)q.uniqueResult(); System.out.println(mResult.getCont()); s.getTransaction().commit(); } @Test public void testHQL_015(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select count(*) from Msg m"); long count = (Long)q.uniqueResult(); System.out.println(count); s.getTransaction().commit(); } @Test public void testHQL_016(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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]); s.getTransaction().commit(); } @Test public void testHQL_017(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Msg m where m.id between 3 and 5"); for(Object o : q.list()){ Msg m = (Msg)o; System.out.println(m.getCont()); } s.getTransaction().commit(); } @Test public void testHQL_018(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Msg m where m.id in(4,5,6)"); for(Object o : q.list()){ Msg m = (Msg)o; System.out.println(m.getCont()); } s.getTransaction().commit(); } @Test public void testHQL_020(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_019(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Msg m where m.cont is not null "); for(Object o : q.list()){ Msg m = (Msg)o; System.out.println(m.getCont()); } s.getTransaction().commit(); } @Test public void testHQL_021(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_022(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_023(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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[] b = (Object[])o; System.out.println(b[0]+"--"+b[1]+"--"+b[2]+"--"+b[3]+"--"+b[4]+"--"); } s.getTransaction().commit(); } @Test public void testHQL_024(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select abs(t.id), sqrt(t.id),mod(t.id,2) from Topic t"); for(Object o : q.list()){ Object[] b = (Object[])o; System.out.println(b[0]+"--"+b[1]+"--"+b[2]+"--"); } s.getTransaction().commit(); } @Test public void testHQL_025(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select current_date,current_time,current_timestamp,t.id from Topic t"); for(Object o : q.list()){ Object[] b = (Object[])o; System.out.println(b[0]+"--"+b[1]+"--"+b[2]+"--"+b[3]); } s.getTransaction().commit(); } @Test public void testHQL_026(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_027(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select t.title ,count(*) from Topic t group by t.title"); for(Object o : q.list()){ Object[] b = (Object[])o; System.out.println(b[0]+"--"+b[1]); } s.getTransaction().commit(); } @Test public void testHQL_028(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select t.title ,count(*) from Topic t group by t.title having count(*) >=1"); for(Object o : q.list()){ Object[] b = (Object[])o; System.out.println(b[0]+"--"+b[1]); } s.getTransaction().commit(); } @Test public void testHQL_029(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_030(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } @Test public void testHQL_031(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("from Topic t where not exists(select m.id from Msg m where m.topic = t.id )"); for(Object o : q.list()){ Topic t = (Topic)o; System.out.println(t.getTitle()); } s.getTransaction().commit(); } @Test public void testHQL_032(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("update Topic t set t.title = upper(t.title)"); q.executeUpdate(); q = s.createQuery("from Topic"); for(Object o : q.list()){ Topic t = (Topic)o; System.out.println(t.getTitle()); } s.createQuery("update Topic t set t.title = lower(t.title)").executeUpdate(); s.getTransaction().commit(); } @Test public void testHQL_033(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.getNamedQuery("topic.selectCertainTopic"); q.setParameter("id", 5); Topic t = (Topic)q.uniqueResult(); System.out.println(t.getTitle()); s.getTransaction().commit(); } //NativeSQL @Test public void testHQL_034(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.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()); } s.getTransaction().commit(); } }
//QBC********************************************* @Test public void testQBC(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Criteria c = s.createCriteria(Topic.class) .add(Restrictions.gt("id", 2)) .add(Restrictions.lt("id", 8)) .add(Restrictions.like("title", "t_")); for(Object o: c.list()){ Topic t = (Topic)o; System.out.println(t.getTitle()); } s.getTransaction().commit(); } //QBE************************************************ @Test public void testQBE(){ Session s = sf.getCurrentSession(); s.beginTransaction(); Topic tExample = new Topic(); tExample.setTitle("T_"); Example e = Example.create(tExample ).ignoreCase().enableLike(); Criteria c = s.createCriteria(Topic.class) .add(Restrictions.gt("id", 2)) .add(Restrictions.lt("id", 8)) .add(e); for(Object o : c.list()) { Topic t = (Topic)o; System.out.println(t.getId() + "-" + t.getTitle()); } s.getTransaction().commit(); }
- Example.rar (5.1 MB)
- 下载次数: 5
发表评论
-
Hibernate---乐观锁/悲观锁
2010-06-13 00:32 1184背景知识: 1.事务(Transa ... -
Hibernate-------一级缓存/二级缓存/查询缓存
2010-06-12 23:19 991例子: model: @Entity //@Bat ... -
Hibernate - List与Iterator缓存问题
2010-06-12 18:37 17441.默认情况下,list不会利用一级缓存,iterator会利 ... -
Hibernate N+1问题
2010-06-12 17:07 10731.n+1问题:当查询多一方时,默认会把对应一的一端也查出来. ... -
(6)hibernate CURD
2010-06-02 10:38 12201. 获取session,SessionFactory sf ... -
(5)hibernate ID生成策略
2010-06-01 14:22 01.xml方式 <id name="id& ... -
(4)hibernate 用EJB3注解进行映射
2010-05-31 21:35 8621.每一个持久化POJO类都是一个实体bean,这可以通过在类 ... -
(3)hibernate配置
2010-05-31 20:47 7851.hibernate3.3.2日志配置,默认采用slf处理日 ... -
(2)hibernate annotations使用
2010-05-30 23:43 8971.在hibernate3.3.2的hibernate-ann ... -
(1)hibernate3.3.2准备工作
2010-05-30 22:48 11091.加入所需要的包 下载hibernate3.3.2的发行 ...
相关推荐
标题"Hibernate_QBC和Hibernate_QBE"提及了两个关于Hibernate的查询方式,即Query By Criteria(QBC)和Query By Example(QBE)。Hibernate是Java领域中一个广泛使用的对象关系映射(ORM)框架,它允许开发者以面向...
3. **限制**:QBE通常适用于简单的查询,对于复杂的查询,如子查询、联接查询等,可能需要使用HQL或QBC。 在实际开发中,QBC更适合于复杂的、需要动态构建的查询,而QBE则更适合于快速实现简单的查询需求。两者都是...
本教程将深入探讨Hibernate中的查询机制,包括HQL(Hibernate Query Language)和QBC(Query by Criteria)。通过实例代码和详细解释,我们将了解如何高效地在数据库中检索数据。 ### Hibernate查询语言(HQL) ...
15. **QBC(Query By Criteria)和QBE(Query By Example)** - 这两种查询方式提供了基于对象和示例的查询接口,简化了复杂查询的编写。 在学习Hibernate时,掌握核心概念、熟练配置环境、理解ORM映射和CRUD操作...
在 Hibernate 中进行查询是其核心功能之一,本文将详细介绍 Hibernate 的几种查询方式,包括 HQL (Hibernate Query Language) 查询、QBC (Query By Criteria) 查询以及 QBE (Query By Example) 查询,并结合示例代码...
- **Hibernate Query Language (HQL)** 是一种面向对象的查询语言,类似于SQL,但更加灵活和强大。 - **QBC**: Query By Criteria,基于准则查询,适用于构建复杂的查询条件。 - **QBE**: Query By Example,通过...
1. **HQL(Hibernate Query Language)**:类似于SQL,但面向对象,支持条件查询、分页、连接查询和嵌套查询。 2. **QBC(Query By Criteria)**:基于对象的查询,通过Criteria API构建查询条件。 3. **QBE(Query ...
- **HQL**(Hibernate Query Language):一种面向对象的查询语言,类似于SQL,但更接近于Java对象的语法结构。 - **QBC**(Query By Criteria):通过Criteria API来构建查询条件。 - **QBE**(Query By Example...
本文将深入解析五种主要的Hibernate查询方法:QBE(Query By Example)、QBC(Query By Criteria)、HQL(Hibernate Query Language)、本地SQL查询以及QID(Query by Identifier)。 1. QBE(Query By Example) ...
它支持多种查询方式,包括面向对象的查询语言(HQL)、条件查询(Query By Criteria, QBC)、示例查询(Query By Example, QBE)以及原生SQL查询等。 #### 二、面向对象查询语言 (HQL) HQL是一种类似SQL的语言,但...
5. `hibernate_2600_QBC`:Criteria查询的例子,展示了如何使用Criteria API来构建动态查询。 四、应用场景 1. 大型企业级应用:在大型系统中,由于数据库操作频繁,使用Hibernate可以提高开发效率,减少出错概率。...
7. **查询语言**: Hibernate支持多种查询语言,包括原生SQL、HQL(Hibernate Query Language)、EJB QL、QBC(Query By Example)和QBE(Query By Example)。HQL更接近面向对象的查询,而EJB QL是Java EE的一部分,...
Criteria 查询机制主要包括 QBE(Query By Example)、QBC(Query By Criteria)和 HQL(Hibernate Query Language)三种方式。 QBE(Query By Example) QBE 是一种基于示例的查询机制,它可以根据一个示例对象来...
在Java的持久化框架Hibernate中,Criteria API提供了一种动态构造SQL查询的方式,它允许开发者在运行时构建灵活的查询,而无需预先编写HQL(Hibernate Query Language)语句。Criteria API提供了丰富的功能,包括...
本文主要探讨Hibernate中的查询语言,包括面向对象的查询语言HQL,QueryBy Criteria (QBC),Query By Example (QBE),以及原生SQL的使用。 1. **大小写敏感性** HQL查询语句对Java类和属性名称的大小写敏感,而...
Hibernate Query Language(HQL)是Hibernate的面向对象的查询语言,类似于SQL,但操作对象和属性而非表和列。以下是一个HQL示例: ```java String hql = "select s.name, avg(s.age) from Student s group by s....
查询对象,HQL Criteria 1.hibernate提供的更面向对象的一种查询方式。 准备工作: 1.java中的POJO对象存在 2.数据库,表得存在 3.hibernate的配置文件(hibernate.cfg.xml)得存在 4.POJO.hbm.xml文件存在 5....