Share on Google+Share on Google+

JPA Sub-Queries

What is JPA Sub queries and how to use it you application?

JPA Sub-Queries


In this section, you will see how to use sub-queries in your JPA application. Sub-queries allows you for querying the vast number of data components in a single query.

JPA Sub-queries

Query query=em.createQuery
("SELECT st FROM Student st WHERE st.sroll=
(SELECT st.sroll FROM Student st WHERE st.sroll>100 AND st.sroll<102)"

For JPA Grouping application, you need the following artifacts:

  1. Database Table: student
  2. Model Class:
  3. Main Class:

Database Table: student

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(100) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) NOT NULL, 

Model Class:


package roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

* @author Administrator

public class Student {

private int id;

* @return the id
public int getId() {
return id;

* @param id the id to set
public void setId(int id) { = id;

@Column(name="sname", length=100,nullable=false)
private String sname;

* @return the sname
public String getSname() {
return sname;

* @param sname the sname to set
public void setSname(String sname) {
this.sname = sname;

private int sroll;

* @return the sroll
public int getSroll() {
return sroll;

* @param sroll the sroll to set
public void setSroll(int sroll) {
this.sroll = sroll;

private String scourse;

* @return the scourse
public String getScourse() {
return scourse;

* @param scourse the scourse to set
public void setScourse(String scourse) {
this.scourse = scourse;


Main Class:

package roseindia;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

* @author Administrator
public class JPASubQueries {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
Query query=em.createQuery("SELECT st FROM Student st WHERE st.sroll=(SELECT st.sroll FROM Student st WHERE st.sroll>100 AND st.sroll<102)");
List stList=query.getResultList();
Iterator stIterator=stList.iterator();
Student st=(Student);
System.out.print(" sname:"+st.getSname());
System.out.print(" sroll:"+st.getSroll());
System.out.print(" scourse:"+st.getScourse());


log4j:WARN No appenders could be found for logger

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id0_, student0_.scourse as
scourse0_, student0_.sname as sname0_, student0_.sroll as
sroll0_ from student student0_ where student0_.sroll=
(select student1_.sroll from student student1_ where
student1_.sroll>100 and student1_.sroll<102)

id:1 sname:Vinod sroll:101 scourse:MCA


Posted on: March 22, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JPA Sub-Queries  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Alejandro Cruz
February 14, 2012
How to use the next subquery injpa??

Hello. I´m a have a problem with the next subquery, I didn´t use it in my jpa aplication. SELECT wgen_a.idAsignacion, wgen_ud.nombre as UsuarioOrigen, wgen_ua.nombre as UsuarioAsignado FROM Asignacion wgen_a ,UsuarioDetalle wgen_ud ,(SELECT u FROM UsuarioDetalle u) wgen_ua WHERE wgen_a.idAsignacion = 1 and wgen_a.idUsuario = wgen_ud.idUsuario and wgen_a.idUsuario = wgen_ua.idUsuario ; Can you help me, please??? Thanks a lot