Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL and one-to-many queries

Tags:

hibernate

hql

I have Hibernate domain objects that looks like this:

   class Player {
      List<Item> inventory;
   }

   class Item {
      List<Enchantment> enchantments;
   }

   class Enchantment {
      boolean isSuperiorEnchantment;
   }

I need to construct an HQL query that returns to me a list of all players that have at least one item with an enchantment on it that has the isSuperiorEnchantment flag set. I can't for the life of me figure out a way to express this in HQL.

Any ideas?

like image 334
Brandon Yarbrough Avatar asked Jul 22 '09 19:07

Brandon Yarbrough


People also ask

Does HQL support all operations?

Need of HQLProvides full support for relational operations. It is possible to represent SQL Queries in the form of objects in HQL which uses classes and properties instead of tables and columns. Return results as objects.

Does HQL support polymorphic queries?

Advantage of HQL database independent. supports polymorphic queries. easy to learn for Java Programmer.

Are subqueries supported in HQL?

Subqueries. For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.

Which is better HQL or SQL?

Differences between SQL and HQL: SQL is based on a relational database model whereas HQL is a combination of object-oriented programming with relational database concepts. SQL manipulates data stored in tables and modifies its rows and columns. HQL is concerned about objects and its properties.


1 Answers

Assuming the appropriate mappings on all of the above, the query you're looking for is:

select p
from Player as p
  left join p.inventory as i
  left join i.enchantments as e
where e.isSuperiorEnchantment = 1
like image 118
ChssPly76 Avatar answered Sep 18 '22 12:09

ChssPly76