目录:
- 应用场景
- 实例演示
[一]、应用场景
在实体类中注解配置实现了one-to-many、many-to-one双向关联后(如何配置详见:http://www.micmiu.com/j2ee/hibernate/hibernate-one-to-many-bidirectional-curd/),那么如何运用QBC(Criteria)的方式实现关联查询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.单元测试(重点是注意Criteria的写法)
测试类:OneToManyBidirectionalQBCTest.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 |
package com.micmiu.hibernate; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.CriteriaSpecification; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.junit.Assert; import org.junit.Test; import com.micmiu.hibernate.demo.entity.Author; import com.micmiu.hibernate.demo.entity.Contact; /** * * 测试QBC关联查询 注解配置双向一对多关系的实体 * * @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 OneToManyBidirectionalQBCTest extends AbstractHibernateBaseTest { @Override @Test public void testMethod() { testQBC4One(); testQBC4Many(); } /** * 测试Criteria查询1:N的1端数据 */ @SuppressWarnings("unchecked") public void testQBC4One() { System.out.println(">>>> 测试查询 one 端数据"); Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Author.class, "a"); criteria.createAlias("a.contacts", "c"); criteria.add(Restrictions.eq("a.sexType", "male")).add( Restrictions.eq("c.type", "mail")); criteria.addOrder(Order.asc("a.username")); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); List<Author> list = criteria.list(); System.out.println(">>>> one-to-many 中 one size:" + list.size()); for (Author c : list) { System.out.println(c); } Assert.assertEquals(1, list.size()); session.close(); } /** * 测试Criteria查询1:N的N端数据 */ @SuppressWarnings("unchecked") public void testQBC4Many() { System.out.println(">>>> 测试查询 many 端数据"); Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Contact.class, "c"); criteria.add(Restrictions.eq("c.type", "blog")) .createAlias("author", "a") .add(Restrictions.eq("a.username", "michael")) .add(Restrictions.eq("a.title", "攻城师")) .add(Restrictions.like("c.details", "%micmiu.com%")); List<Contact> list = criteria.list(); System.out.println(">>>> one-to-many 中 many size:" + list.size()); for (Contact c : list) { System.out.println(c); } Assert.assertEquals(1, list.size()); 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
this_.ID as ID2_2_,
this_.DESCRIPTION as DESCRIPT2_2_2_,
this_.SEX_TYPE as SEX3_2_2_,
this_.TITLE as TITLE2_2_,
this_.USERNAME as USERNAME2_2_,
c1_.ID as ID3_0_,
c1_.AUTHOR_ID as AUTHOR5_3_0_,
c1_.DETAILS as DETAILS3_0_,
c1_.OTHER as OTHER3_0_,
c1_.TYPE as TYPE3_0_,
author4_.ID as ID2_1_,
author4_.DESCRIPTION as DESCRIPT2_2_1_,
author4_.SEX_TYPE as SEX3_2_1_,
author4_.TITLE as TITLE2_1_,
author4_.USERNAME as USERNAME2_1_
from
DEMO_T_AUTHOR this_
inner join
DEMO_T_CONTACT c1_
on this_.ID=c1_.AUTHOR_ID
left outer join
DEMO_T_AUTHOR author4_
on c1_.AUTHOR_ID=author4_.ID
where
this_.SEX_TYPE=?
and c1_.TYPE=?
order by
this_.USERNAME asc
>>>> one-to-many 中 one size:1
Author [id=1, username=michael, title=攻城师, sexType=male, description=IT技术]
>>>> 测试查询 many 端数据
Hibernate:
select
this_.ID as ID3_1_,
this_.AUTHOR_ID as AUTHOR5_3_1_,
this_.DETAILS as DETAILS3_1_,
this_.OTHER as OTHER3_1_,
this_.TYPE as TYPE3_1_,
a1_.ID as ID2_0_,
a1_.DESCRIPTION as DESCRIPT2_2_0_,
a1_.SEX_TYPE as SEX3_2_0_,
a1_.TITLE as TITLE2_0_,
a1_.USERNAME as USERNAME2_0_
from
DEMO_T_CONTACT this_
inner join
DEMO_T_AUTHOR a1_
on this_.AUTHOR_ID=a1_.ID
where
this_.TYPE=?
and a1_.USERNAME=?
and a1_.TITLE=?
and this_.DETAILS like ?
>>>> one-to-many 中 many size:1
Contact [id=11, type=blog, details=http://www.micmiu.com/author/michael, other=null]
本文介绍到此结束@Michael Sun.
0 条评论。