目录:
- 应用场景
- 实例演示
[一]、应用场景
在实体类中注解配置实现了one-to-many、many-to-one双向关联后(如何配置详见:http://www.micmiu.com/j2ee/hibernate/hibernate-one-to-many-bidirectional-curd/),那么如何运用HQL的方式实现关联查询one 端或者many端的数据?下面我将一一详细讲解,还是以Author 和Contact 作为1:N的实例来演示。
[二]、实例演示
1.初始数据
用dbunit初始数据如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<demo_t_author id="1" username="michael" title="攻城师" sex_type="male" description="IT技术"/> <demo_t_author id="2" username="hazel" title="随记" sex_type="female" description="记录生活的点滴"/> <demo_t_author id="3" username="ctosun" title="酱油师" sex_type="male" description="打酱油"/> <demo_t_contact id="11" author_id="1" type="blog" details="http://www.micmiu.com/author/michael" other=""/> <demo_t_contact id="12" author_id="1" type="weibo" details="http://weibo.com/ctosun" other=""/> <demo_t_contact id="13" author_id="1" type="twitter" details="http://twitter.com/suncto" other=""/> <demo_t_contact id="14" author_id="1" type="mail" details="sjsky007@gmial.com" other=""/> <demo_t_contact id="15" author_id="1" type="mobile" details="180xxxxxxxx" other=""/> <demo_t_contact id="21" author_id="2" type="blog" details="http://www.micmiu.com/author/hazel" other=""/> <demo_t_contact id="22" author_id="2" type="mail" details="test@micmiu.com" other=""/> <demo_t_contact id="31" author_id="3" type="blog" details="http://www.ctosun.com" other=""/> |
2.单元测试(重点是注意HQL的写法)
测试类:OneToManyBidirectionalHQLTest.java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package com.micmiu.hibernate; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.junit.Assert; import org.junit.Test; import com.micmiu.hibernate.demo.entity.Author; import com.micmiu.hibernate.demo.entity.Contact; /** * * 测试HQL关联查询 注解配置双向一对多关系的实体 * * @author <a href="http://www.micmiu.com">Michael</a> * @see <a href="http://www.micmiu.com">http://www.micmiu.com</a> * @time Create on 2013-6-7 上午11:38:49 * @version 1.0 */ public class OneToManyBidirectionalHQLTest extends AbstractHibernateBaseTest { @Override @Test public void testMethod() { testHQL4One(); testHQL4Many(); } /** * 测试HQL 查询1:N的1端数据 */ @SuppressWarnings("unchecked") public void testHQL4One() { System.out.println(">>>> 测试查询 one 端数据"); Session session = sessionFactory.openSession(); session.beginTransaction(); String hql = "select a from Author a join a.contacts c where a.username='michael' " + "and a.title='攻城师' and c.type ='blog' and c.details like '%micmiu.com%' "; Query query = session.createQuery(hql); List<Author> list = query.list(); System.out.println(">>>> one-to-many 中 one size:" + list.size()); for (Author a : list) { System.out.println(a); Assert.assertEquals("michael", a.getUsername()); } Assert.assertEquals(1, list.size()); session.getTransaction().commit(); session.close(); } /** * 测试HQL 查询1:N的N端数据 */ @SuppressWarnings("unchecked") public void testHQL4Many() { System.out.println(">>>> 测试查询 many 端数据"); Session session = sessionFactory.openSession(); session.beginTransaction(); String hql = "select c from Contact c join c.author a where a.username='michael' " + "and a.title='攻城师' and c.type ='blog' and c.details like '%micmiu.com%' "; Query query = session.createQuery(hql); List<Contact> list = query.list(); System.out.println(">>>> one-to-many 中 many size:" + list.size()); for (Contact c : list) { System.out.println(c); Assert.assertTrue(c.getDetails().indexOf("micmiu.com") > 0); } Assert.assertEquals(1, list.size()); session.getTransaction().commit(); session.close(); } } |
AbstractHibernateBaseTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.micmiu.hibernate; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; /** * 单元测试的抽象类 * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-6-6 下午7:31:02 * @version 1.0 */ public abstract class AbstractHibernateBaseTest { protected static SessionFactory sessionFactory; @BeforeClass public static void beforeClass() { Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } @AfterClass public static void afterClass() { sessionFactory.close(); } @Test public abstract void testMethod(); } |
运行测试通过,日志如下:
>>>> 测试查询 one 端数据
Hibernate:
select
author0_.ID as ID2_,
author0_.DESCRIPTION as DESCRIPT2_2_,
author0_.SEX_TYPE as SEX3_2_,
author0_.TITLE as TITLE2_,
author0_.USERNAME as USERNAME2_
from
DEMO_T_AUTHOR author0_
inner join
DEMO_T_CONTACT contacts1_
on author0_.ID=contacts1_.AUTHOR_ID
where
author0_.USERNAME=’michael’
and author0_.TITLE=’攻城师’
and contacts1_.TYPE=’blog’
and (
contacts1_.DETAILS like ‘%micmiu.com%’
)
>>>> one-to-many 中 one size:1
Author [id=1, username=michael, title=攻城师, sexType=male, description=IT技术]
>>>> 测试查询 many 端数据
Hibernate:
select
contact0_.ID as ID3_,
contact0_.AUTHOR_ID as AUTHOR5_3_,
contact0_.DETAILS as DETAILS3_,
contact0_.OTHER as OTHER3_,
contact0_.TYPE as TYPE3_
from
DEMO_T_CONTACT contact0_
inner join
DEMO_T_AUTHOR author1_
on contact0_.AUTHOR_ID=author1_.ID
where
author1_.USERNAME=’michael’
and author1_.TITLE=’攻城师’
and contact0_.TYPE=’blog’
and (
contact0_.DETAILS like ‘%micmiu.com%’
)
Hibernate:
select
author0_.ID as ID2_0_,
author0_.DESCRIPTION as DESCRIPT2_2_0_,
author0_.SEX_TYPE as SEX3_2_0_,
author0_.TITLE as TITLE2_0_,
author0_.USERNAME as USERNAME2_0_
from
DEMO_T_AUTHOR author0_
where
author0_.ID=?
>>>> one-to-many 中 many size:1
Contact [id=11, type=blog, details=http://www.micmiu.com/author/michael, other=null]
本文介绍到此结束@Michael Sun.
0 条评论。