Lets review basic many-to-many relationship between tables and build common search queries in MySQL and Hibernate HQL. We will take this site's database schema which has article-tag many-to-many relationship as an example and will try to build search queries to find articles by specific tags.
Here is our database schema that implements many-to-many relationship using intermediate "article_tag" table:
Corresponding Hibernate mappings:
<hibernate-mapping> <class name=ca.sergiy.model.Tag" table="tag"> <cache usage="read-write"/> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" column="name"/> </class></hibernate-mapping><hibernate-mapping> <class name="ca.sergiy.model.Article" table="article"> <cache usage="read-write" /> <id name="id" column="id" type="long"> <generator class="native" /> </id> <property name="title" column="title" /> <set name="tags" table="article_tag" lazy="false"> <key column="articleid" /> <many-to-many class="ca.sergiy.model.Tag" column="tagid" /> </set> </class></hibernate-mapping>
#1. Find all articles that are tagged with any of tag1, tag2, ..., tagn
MySQL query to select all articles that have "Java" or "Hibernate" among their assigned tags:
SELECT DISTINCT a.*FROM `article` a INNER JOIN article_tag at ON at.articleid = a.id INNER JOIN tag t ON t.id = at.tagidWHERE t.name IN ("Java", "Hibernate")
Corresponding Hibernate HQL:
String[] tags = {"Java", "Hibernate"};String hql = "select distinct a from Article a " + "join a.tags t " + "where t.name in (:tags)";Query query = session.createQuery(hql);query.setParameterList("tags", tags);List<Article> articles = query.list();
This query will also work for a single tag (find all articles tagged with "Java")
#2. Find all articles that have no tags assigned
MySQL query:
SELECT a.*FROM `article` a LEFT JOIN article_tag at ON at.articleid = a.idGROUP BY a.idHAVING Count(at.tagid) = 0
Corresponding Hibernate HQL:
String hql = "select a from Article a " + "left join a.tags t " + "group by a " + "having count(t)=0";Query query = session.createQuery(hql);List<Article> articles = query.list();
Note that this query uses LEFT JOIN.
#3. Find all articles that are tagged with at least tag1, tag2, ..., tagn
MySQL query to select all articles that have at least both "Java" and "Hibernate" among their assigned tags:
SELECT a.*FROM article a INNER JOIN (SELECT at.articleid FROM article_tag at INNER JOIN article a ON a.id = at.articleid INNER JOIN tag t ON t.id = at.tagid WHERE t.name IN ("Java","Hibernate") GROUP BY at.articleid HAVING Count(at.articleid) = 2) aa ON a.id = aa.articleid
Hibernate HQL, looks much cleaner:
String[] tags = {"Java", "Hibernate"};String hql = "select a from Article a " + "join a.tags t " + "where t.name in (:tags) " + "group by a " + "having count(t)=:tag_count";Query query = session.createQuery(hql);query.setParameterList("tags", tags);query.setInteger("tag_count", tags.length);List<Article> articles = query.list();
#4. Find all articles that are tagged with exactly tag1, tag2, ..., tagn
MySQL query to select all articles that are tagged with exactly "Java" and "Hibernate" tags (no other tags assigned):
SELECT a.*FROM article a INNER JOIN (SELECT at.articleid FROM article_tag at WHERE at.articleid IN (SELECT at2.articleid FROM article_tag at2 INNER JOIN article a2 ON a2.id = at2.articleid GROUP BY at2.articleid HAVING Count(at2.articleid) = 2) AND at.tagid IN (SELECT id FROM tag t WHERE t.name IN ("Java","Hibernate")) GROUP BY at.articleid HAVING Count(at.articleid) = 2) aa ON a.id = aa.articleid
Hibernate HQL:
String[] tags = {"Java", "Hibernate"};String hql = "select a from Article a " + "join a.tags t " + "where t.name in (:tags) " + "and a.id in (" + "select a2.id " + "from Article a2 " + "join a2.tags t2 " + "group by a2 " + "having count(t2)=:tag_count) " + "group by a " + "having count(t)=:tag_count";Query query = session.createQuery(hql);query.setParameterList("tags", tags);query.setInteger("tag_count", tags.length);List<Article> articles = query.list();
Basically it is query #3 with extra condition applied: total number of tags should be n.
分享到:
相关推荐
how-to-write-a-great-research-paper-wiley-keys-to-success.pdf
How to write and publish a scientific paper ContentsChapter 1 What Is Scientific Writing? Chapter 2 Origins of Scientific Writing Chapter 3 What Is a Scientific Paper? Chapter 4 How to Prepare the ...
How to Define and Use Formatted Search in SAP business one
Bioinformatics-with-Python-Cookbook-Learn-how-to-use-modern-Python-bioinformatics-libraries-and-applications-to-do-cutting-edge-research-in-computational-biology.pdf
主要为关于论文写作的建议和方法,内部包含有三个文档,分别为:《How to Write Good Reviews for CVPR》、《Teaching Graduate Students How to Write Clearly》以及《How write a Paper》三个文档,十分具有借鉴...
A special feature of this edition is a new appendix on NoSQL and relational theory.Could you write an SQL query to find employees who have worked at least once in every programming department in the ...
This book provides practical, proven techniques for making writing for ... All levels of business and technical personnel will find this easy-to-read guide invaluable and immediately useful every day.
(How to Write a (Lisp) Interpreter (in Python))和(An ((Even Better) Lisp) Interpreter (in Python))的翻译,对解释器实现原理和函数式编程敢兴趣的可以下载看看!
These new features in MariaDB and MySQL help you to write queries without having to wade through a quagmire of brittle self-joins and other crazy techniques from the past. Your queries will generate ...
在小学英语语法中,"how many" 和 "how much" 是两个非常重要的概念,它们用于询问数量,但针对的对象不同。 1. **how many** 主要用来修饰**可数名词的复数形式**。它的基本句型是:"How many + 复数名词 + 一般...
How to Write & Publish a Scientific Paper 6th Edition
教你如何书写与发表科技论文 很实用哦 How to Write & Publish a Scientific Paper 英文版
译林版英语四年级上册Unit-3-How-many-教案.docx
how to draw circle - circle intersection and this alse i did not write it but i found it in my lap top