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?
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.
Advantage of HQL database independent. supports polymorphic queries. easy to learn for Java Programmer.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With