Lets see how to querying from Hibernate.
For this example I have used a College , Student relation which is a one to many association.
College.java
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class College {
private int collegeId;
private String collegeName;
private List<Student> students;
@Id
@GeneratedValue
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
@OneToMany(targetEntity=Student.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="college")
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
Student.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Student {
private int studentId;
private String studName;
private College college;
@Id
@GeneratedValue
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
@ManyToOne
@JoinColumn(name="college_id")
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}
}
Test.javaimport java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(College.class);
config.addAnnotatedClass(Student.class);
config.configure("hibernate.cfg.xml");
//creating tables
// new SchemaExport(config).create(true, true);
//adding sample data
// SessionFactory factory=config.buildSessionFactory();
// Session session=factory.getCurrentSession();
//
// session.beginTransaction();
//
// College c1=new College();
// c1.setCollegeName("Richmond College");
//
// College c2=new College();
// c2.setCollegeName("Mahinda College");
//
// Student st1=new Student();
// st1.setStudName("Kanishka Dilshan");
// st1.setCollege(c1);
// Student st2=new Student();
// st2.setStudName("Sajith Athukorala");
// st2.setCollege(c1);
// Student st3=new Student();
// st3.setStudName("Sampath Bandara");
// st3.setCollege(c1);
//
// Student st4=new Student();
// st4.setStudName("Dasun Pathirana");
// st4.setCollege(c2);
// Student st5=new Student();
// st5.setStudName("Kasun Sameera");
// st5.setCollege(c2);
//
// session.save(c1);
// session.save(c2);
// session.save(st1);
// session.save(st2);
// session.save(st3);
// session.save(st4);
// session.save(st5);
//
// session.getTransaction().commit();
//query part
SessionFactory factory=config.buildSessionFactory();
Session session=factory.getCurrentSession();
session.beginTransaction();
Query query=session.createQuery("from Student s where s.studName like :name_p");
query.setParameter("name_p", "%k%");
List lst=query.list();
printTheList(lst);
session.getTransaction().commit();
System.out.println("Size : " + lst.size());
}
private static void printTheList(List lst){
Iterator itr=lst.iterator();
while(itr.hasNext()){
Student s=(Student) itr.next();
System.out.println(s.getStudName());
}
}
}
Note :
The simplest way to execute a HQL query is
- Start a transaction
- Create a Query object using session.createQuery("HQL Query")
- Set parameters if any
- Retrieve the HQL result to a List object by executing list() method in the Query object
- Now it is the time to play with the retrieved List object :)
Note 2 :session.beginTransaction();Query query=session.createQuery("from Student");List listResults=query.list();session.getTransaction().commit();
:name_p is a parameter. in HQL colon(:) is used to denote the beginning of a parameter.
Result :


0 comments:
Post a Comment