How to return Collections and other Types in EJB QL ?
In this tutorial we will learn how to create new Type safe instances of an Objects simply using the EJB QL in your Query.
By default, the select clause picks which objects and properties to return in the query result set. Check this example:
select mate
from Cat as cat
inner join cat.mate as mate
The above query will select mates of other Cats.
Queries can return properties of any value type including properties of component type:
select cat.name from DomesticCat cat
where cat.name like 'fri%'
select cust.name.firstName from Customer as cust
Queries can return multiple objects and/or properties as an array of type Object[]:
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offsp
Or as a java.uti.List:
select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
Or – assuming that the class Family has an appropriate constructor – as an actual typesafe Java object:
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
You can assign aliases to selected expressions using as:
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat
This is most useful when used together with select new map:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat
Recommended Articles
EJB to EJB Invocations Between Two WildFly Servers: A Comprehensive Guide
Learn how to perform EJB to EJB invocations between two WildFly server instances, including configuration for standalone and remote clients. #WildFly #JavaEE #CloudNative
Call an EJB from another application in WildFly using EJB Lookup
Learn how to invoke EJBs from a remote EJB Client using WildFly's EJB lookup feature. This tutorial covers the steps for creating an EJB server project, setting up security, and configuring dependencies.
EJB to EJB Communication in a Cluster with WildFly 31
Learn how to set up EJB communication between a cluster of WildFly servers and an EJB client, enabling dynamic view of the cluster topology.
Exposing an Enterprise Java Bean as a Singleton: A Simple Approach
Learn how to use the @Singleton annotation to expose an Enterprise Java Bean as a single instance, ensuring thread-safe and efficient management of your application's resources.