Home Jpa Eclipsejpaexamples JPA Named Parameter List

Related Tutorials


 
 

Share on Google+Share on Google+

JPA Named Parameter List

Advertisement
In this section, you will learn how to develop a JPA application using JPA Named Parameter List.

JPA Named Parameter List

     

In this section, you will learn how to develop a JPA application using JPA Named Parameter List. This is similar as named parameter but here we use the an array list as named parameter. 

Named Parameter List:

List snames=new ArrayList();
snames.add("Ravi");
snames.add("Vinod");
snames.add("suman11");
Query query=em.createQuery("SELECT st FROM Student st
WHERE st.sname IN (:snameList)");
query.setParameter("snameList", snames); 

To develop JPA Named Parameter application, you need the following artifacts:

  1. Database Table: student
  2. Model Class: Student.java
  3. Main Class: JPANamedParameterList.java

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, 
PRIMARY KEY (`id`) 
)

Model Class: Student.java

/**

*/
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
*
*/
@Entity
@Table(name="student")

public class Student {

@Id
@GeneratedValue
private int id;

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

/**
* @param id the id to set
*/
public void setId(int id) {
this.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;
}

@Column(name="sroll",nullable=false)
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;
}

@Column(name="scourse",length=10,nullable=false)
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: JPANamedParameterList.java

/**
* 
*/
package roseindia;

import java.util.ArrayList;
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 JPANamedParameterList {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
List snames=new ArrayList();
snames.add("Ravi");
snames.add("Vinod");
snames.add("suman11");
Query query=em.createQuery("SELECT st FROM Student st WHERE st.sname IN (:snameList)");
query.setParameter("snameList", snames);
List stList=query.getResultList();
Iterator stIterator=stList.iterator();
while(stIterator.hasNext()){
Student st=(Student)stIterator.next();
System.out.print("id:"+st.getId());
System.out.print(" sname:"+st.getSname());
System.out.print(" sroll:"+st.getSroll());
System.out.print(" scourse:"+st.getScourse());
System.out.println();
}
entr.commit();
}
finally{
em.close();
}
}
}

Output:

log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select student0_.id as id0_, student0_.scourse
as scourse0_, student0_.sname as sname0_, student0_.sroll
as sroll0_ from student student0_ where student0_.sname in (? , ? , ?)

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

id:2 sname:Ravi sroll:102 scourse:M.Tech.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: June 16, 2009

Related Tutorials

Discuss: JPA Named Parameter List   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Anil
September 26, 2012
JPA named paramlist

IN did not work. I used select data from Table t where t.value(:list) i have passed list as ArrayList<String> which does not have any commas any alternative way for NamedParameter List. I used setParamList in hibernate.But such option is not available with JPA.
DMCA.com