hibernate annotation with hql & sql · hibernate query language : hql hql...
TRANSCRIPT
Hibernate Annotationwith
HQL & SQL
HQL
HQL ยอมาจาก Hibernate Query Language เปนภาษาสอบถามเชงวตถทใช
สาหรบจดการขอมลในฐานขอมลทมลกษณะคลายกบภาษา SQL
ขอแตกตางระหวาง SQL และ HQL ทสาคญ ๆ สามารถสรปไดดงตอไปน
SQL มพนฐานการทางานมาจากระบบฐานขอมลเชงสมพนธ ในขณะท
HQL เปนการทางานทอาศยหลกการเชงวตถรวมกบแนวคดของ
ฐานขอมลเชงสมพนธ
SQL ใชจดการขอมลทถกบนทกลงในตารางและมการแกไขผาน
คอลมนและแถว ขณะท HQL เปนการทางานทเกยวของกบออบเจกต
และแอททรบวตทถกกาหนดไว
SQL ใชการทางานจากความสมพนธทเกดขนระหวางตาราง ในขณะท
HQL จะใชการทางานจากความสมพนธระหวางออบเจกต
Hibernate query language : HQL
HQL มลกษณะเปน case-insensitive ยกเวนในกรณทมการอางถงชอคลาส
และชอตวแปรในจาวา
From Clause: มลกษณะเดยวกบ select clause ใน SQL
Join Clause: HQL สนบสนนการทางานประเภท Join ไดแก inner join,
left outer join, right outer join และ full join
Aggregate ฟงกชน : count(*), count(distinct x), min(), max(), avg()
และ sum()
Expressions: HQL สนบสนนนพจนทางคณตศาสตร (+, -, *, /) การ
เปรยบเทยบ Binay (=, >=, <=, <>, !=, like) เปรยบเทยบลอจก (and, or,
not)
HQL สนบสนนคาสง clauses ประเภท order by และ group by
Query interface
เปนออบเจกตทถกสรางขนจากการเรยกใชเมธอด createQuery() จาก
Session ประกอบไปดวยเมธอดหลก ๆ ดงน
public int executeUpdate() ใชประมวลผลควร update หรอ delete
public List list() คนคาผลลพธจากตารางในรปของ list
public Query setFirstResult(int rowno) ระบจานวนแถวทอานขอมล
public Query setMaxResult(int rowno) ระบจานวนแถวขอมลท
ตองการอานจากตาราง
public Query setParameter(int position, Object value) ใชสาหรบ
กาหนดคาตาแหนงพารามเตอรตามรปแบบการควรขอมล
public Query setParameter(String name, Object value) ใชกาหนด
ชอพารามเตอรรวมกบการควรขอมล
Part of Student Class@Entity@Table (name="STUDENT")public class Student {
@Id@GeneratedValue private int studentId;private String studentName;private String major;private double gpa;
public Student() {}
public Student( String studentName, String major, double gpa) {this.studentName = studentName;this.major = major;this.gpa = gpa;
}public String toString() {
return " "+studentId+ " "+studentName + " "+major + " "+ gpa;}
}
FROM Clause
เปนรปแบบ HQL ควรทใชโหลด Persistent ออบเจกตเขาสหนวยความจา
โดยมรปแบบการใชคาสงทคลายกบภาษา SQL ดงน
การใช HQL ตองผาน Query ออบเจกตทประกอบไปดวยเมธอดตาง ๆ ทใช
ในการควบคมการประมวลผลควรตาง ๆ ดงตวอยางคาสงตอไปน
HQL SQL
FROM Student Select * from Student
String hql = "FROM Student";Query query = session.createQuery(hql);List<Student> students = query.list();
list() : HQL
สวนเมธอด list() ใชสาหรบคนคาผลลพธจากการควรในรปกลมขอมลทถก
จดเกบภายใน List ดงตวอยางคาสงตอไปน
String hql = "FROM Student";Query query = session.createQuery(hql);List<Student> students = query.list();System.out.println("Student Details: ");
for(Student stu: students) {System.out.println(stu);
}
1 Somchai Jaidee Information Technology 2.752 Somsri Jaipak Computer Science 3.13 Somrak Jaijing Information Technology 2.54 Somying Jaimon Computer Science 3.12
list() : SQL
สวนเมธอด list() ของ SQL ใชสาหรบคนคาผลลพธจากการควรในรปกลม
ขอมลภายใน Object[] ผใชตองแยกผลลพธจากคาสงดวยตวเอง ดงตวอยาง
คาสงตอไปน
SQLQuery query = session.createSQLQuery("select * from Student");
List<Object[]> student = query.list();System.out.println("Student Details: ");
for(Object[] row : student){System.out.print("Student ID: "+ row[0]);System.out.print(" Name: "+ row[1]);System.out.println(" Major: "+row[2]); System.out.println(" and GPA: "+row[3]);
}
ขอมล row ขนอยกบลาดบของ
ขอมลภายในตารางฐานขอมล
WHERE Clause
ใชในกรณทตองการจากดผลลพธของการควรใหแคบลงผใชสามารถใช
WHERE clause เพอปรบแกผลลพธของออบเจกตทตองการคนคา โดยม
รปแบบการใชงานดงตวอยางคาสงตอไปน
String hql = "FROM Student where studentId = 3";Query query = session.createQuery(hql);List<Student> students = query.list();System.out.println("Student Details: ");
for(Student stu: students) {System.out.print(stu);
}
3 Somrak Jaijing Information Technology 2.5
SELECT Clause
คาสงนชวยใหการควบคมผลลพธจากการทางานทาไดดกว า from clause
ในกรณทตองการอานคา properties ของ objects ภายใน result ใหใช select
clause
ตวอยางเชน ในกรณทตองการอานคาเฉพาะชอของ Student เทานน ผใชไม
จาเปนตองอานคาจากออบเจกตทงหมด ดง select clause ตอไปน
String hql = "SELECT S.name FROM Student S";Query query = session.createQuery(hql);List<String> names = query.list();
for(String stu: names) {System.out.println("Student name: "+ stu.toString());
}
Named Parameters
Hibernate สนบสนนการใชชอของพารามเตอรในการควรขอมล เพอเพม
ความสะดวกในการเขยน HQL ควรรบขอมลอนพตจากผใช
เชนใ นกรณทตองการควรขอมลเฉพาะ Student ทมคา studentId เทากบ 1
สามารถใชคาสง setParameter() เพอกาหนดชอตวแปร stu_id แทนได
String hql = "FROM Student S WHERE S.studentId = :stu_id";
Query query = session.createQuery(hql);query.setParameter("stu_id",(long)1);List <Student> results = query.list();
for(Student stu: results) {System.out.println(stu);
}
Paging Through the Result Set
เมธอด setFirstResult() รบคาตวเลขทนาเสนอแถวแรกใน result set โดย
เรมตนจากแถวท 0
ผใชสามารถกาหนดจานวนออบเจกตทตองการนาเสนอได โดย ผานการ
เรยกใชเมธอด setMaxResults()
String hql = "FROM Student";Query query = session.createQuery(hql); query.setFirstResult(1);query.setMaxResults(2);List<Student> results = query.list();
for(Student stu: results) {System.out.println(stu);
}
2 Somsri Jaipak Computer Science 3.13 Somrak Jaijing Information Technology 2.5
Obtaining a Unique Result
เมธอด uniqueResult() ใชสาหรบอานคาเพยงออบเจกตเดยวจาก HQL ควร
ในกรณทไมมผลลพธจะคนคา null
สวนในกรณทมผลลพธมากกวาหนงคาเมธอด uniqueResult() จะโยนคา
NonUniqueResultException
String hql = "from Student where gpa > 2.5";Query query = session.createQuery(hql);query.setMaxResults(1);Student stu = (Student) query.uniqueResult();
System.out.println(stu);
1 Somchai Jaidee Information Technology 2.75
HQL with Aggregate functions
HQL ยงสนบสนนการทางานของฟงกชนทใชอานวยความสะดวกตาง ๆ
อาท avg(), min(), max() เปนตน
ตวอยางเชน
query = session.createQuery("select sum(salary) from Employee");
double sumSalary = (Double) query.uniqueResult();
System.out.println("Sum of all Salaries= "+sumSalary);
query = session.createQuery("select sum(salary) from Employee");
List <Double> sumSalary = query.list();
System.out.println("Sum of all Salaries= "+sumSalary.toString);
HQL with Aggregate functions
max()
Query q=session.createQuery("select max(salary) from Emp");
min()
Query q=session.createQuery("select min(salary) from Emp");
count()
Query q=session.createQuery("select count(id) from Emp");
avg()
Query q=session.createQuery("select avg(salary) from Emp");
Hibernate Object Life Cycle
Object State : Transient
ทก ๆ ออบเจกตเมอถกสรางขนจะอยในสถานะ transient เสมอ
Student student = new Student("Somchai Jaidee", "IT", 2.75 );
Hibernate จะยงไมรบรเกยวกบออบเจกต student
สถานะดงกลาวนยงไมมความเกยวของกบฐานขอมล – ไมมคาของ
studentId
เมอไมมการอางองจากออบเจกตดงกลาวจะถกจดการโดย Garbage
collection
Object State : Persistent
Hibernate รบรความมตวตนของออบเจกตดงกลาว โดยออบเจกตมคา
database ID
สถานะนเกดขนเมอมการบนทกขอมลออบเจกตลงในฐานขอมล
ออบเจกตถกเปลยนสถานะเปน persistent ผานการเรยกใชเมธอดรวมกบ
Hibernate session เชน
session.save(obj) หรอ session.update(obj)
Object State : Persistent (Cont.)
Session session = SessionFactory.openSession();
Student student = new Student("Somchai Jaidee", "IT", 2.75 );
session.save(student);
// การแกไขใด ๆ ทเกดขนกบออบเจกตจะถกบนทกโดยอตโนมต
// เนองจาก Student ออบเจกตอยในสถานะ ‘persistent’
student.setMajor(“CS”);
session.getTransaction().commit();
Object State : Removed
เกดขนเมอ persistent ออบเจกตถกลบออกไปโดยการเรยกใชเมธอด delete()
หรอ remove() รวมกบ Session เชน
session.delete(account);
แมวาจะถกลบออกไปแลว แตจาวาออบเจกตยงคงอย แต Hibernate จะ
ignore ออบเจกตดงกลาว
การเปลยนแปลงทเกดขนกบออบเจกตจะไมถกบนทกลงในฐานขอมล
ถกจดการโดย garbage collection เมออยนอกขอบเขตการใชงาน
Hibernate ไมมการ not null-out the in-memory object
Object State : Removed
Session session = SessionFactory.openSession();
// อานคา Student ออบเจกตทมคา id 1 และอยในสถานะ ‘persistent’
Student student = (Student) session.get(Student.class, 1);
session.delete(student);
// การแกไขไมสงผลใด ๆ เนองจากออบเจกตอยในสถานะ ‘removed’
student.setMajor(“IT”);
session.getTransaction().commit();
// หมายเหต Student ออบเจกตยงคงอยสถานะ Transient
// แมวาจะถกลบออกจากฐานขอมลกตาม
Object State : Detached
Persistent ออบเจกตทยงสามารถอางองไดหลงจากปด session ไปแลว
session.close() เปลยนสถานะออบเจกตจาก persisted เปน detached
ยงถกนาเสนอในรปแถวขอมลภายในตารางฐานขอมล
ไมถกจดการโดย Hibernate อกตอไป
การเปลยนแปลงทเกดขนกบ detached ออบเจกตจะไมถกบนทกลงใน
ฐานขอมล ขณะทออบเจกตยงคงอยในสถานะ detached state
Object State : Detached
Session session = SessionFactory.openSession();
// อานคา Student ออบเจกตทมคา id 1 และอยในสถานะ ‘persistent’
Student student = (Student) session.get(Student.class, 1);
// เปลยนสถานะเปน ‘detached’ เมอปด session
session.close();
// การแกไขไมสงผลกบออบเจกตสถานะ ‘detached’ แตขอมลคงอยในตาราง
student.setGpa(3.00);
// ออบเจกตสามารถกลบมาอยในสถานะ persistent ไดเมอมการเปด session
// และสามารถบนทกการเปลยนแปลงลงในฐานขอมลได
Session session1 = sessionFactory.openSession();
session1.beginTransaction(); Session1.update(student);
session1.getTransaction().commit();
CRUD Operation
สถานะของออบเจกตใน Hibernate จะมความเกยวของโดยตรงกบการ
ทางานแบบ CRUD (Create(save), Read(select), Update(update) และ
Delete(delete)) โดยการใชงานรวมกบ Session ออบ-เจกตทถกกาหนดไวใน
Hibernate แพคเกจ ซงเมธอดตาง ๆ ทใชในการทางานแบบ CRUD ม
ดงตอไปน
save()
get()
delete()
update()
Bidirectional One To Many : CRUD
@Entity @Table(name = "COMPANY") public class Company { .....;
@OneToMany (cascade=CascadeType.ALL, mappedBy = "company")private Set<Employee> employeeSet = new HashSet<Employee>();
.....;}
@Entity @Table (name = "EMPLOYEE") public class Employee {
....;@ManyToOne(cascade=CascadeType.ALL) @JoinColumn (name = "company_id")private Company company;
}
Create
Company company1 = new Company("Indythaitester"); Employee employee1 = new Employee("Somchai Jaidee"); Employee employee2 = new Employee("Somsri Jaipak");
company1.getEmployees().add(employee1); company1.getEmployees().add(employee2); employee1.setCompany(company1);employee2.setCompany(company1);
Company company2 = new Company("Information Technology"); Employee employee3 = new Employee("Somrak Jaijing"); Employee employee4 = new Employee("Somying Jaimon");
company2.getEmployees().add(employee3); company2.getEmployees().add(employee4); employee3.setCompany(company2);employee4.setCompany(company2);company1.setEmployees(company1.getEmployees());session.save(company1);company2.setEmployees(company2.getEmployees());session.save(company2);
List
@SuppressWarnings("unchecked")public static void listCompany() {
Session session = HibernateUtil.getSessionFactory().openSession();session.beginTransaction();List <Company> companies = session.createQuery("from Company").list();for (Company com: companies) {
System.out.println(company);
for(Employee employee : company.getEmployees()) {System.out.println(employee);
}}
session.getTransaction().commit();session.close();
}
}
Update
@SuppressWarnings("unchecked")public static void updateCompany(String oldName, String newName) {
Session session = HibernateUtil.getSessionFactory().openSession();session.beginTransaction();List <Company> companies = session.createQuery("from Company where
company_name ='" + oldName + "'").list();for (int i = 0; i < companies.size(); i++) {
Company company = (Company) companies.get(i);if(company.getCompanyName().equals(oldName)) {company.setCompanyName(newName);session.update(company);
}}
session.getTransaction().commit();session.close();
}}
Delete
@SuppressWarnings("unchecked")public static void deleteCompany(String name) {
Session session = HibernateUtil.getSessionFactory().openSession();session.beginTransaction();List <Company> companies = session.createQuery("from Company").list();
for (int i = 0; i < companies.size(); i++) {
Company company = (Company) companies.get(i);if (company.getCompanyName().equals(name))
session.delete(company);
}}
session.getTransaction().commit(); session.close();
}}
Delete : Orphan
@SuppressWarnings("unchecked")public static void listCompany() {
Session session = HibernateUtil.getSessionFactory().openSession();session.beginTransaction();List <Employee> employees = session.createQuery("from Employee").list();
for(Employee employee : employees ) {if (employee.getEmployeeName().equals(name)) {
Company company = Company)session.get(Company.class,employee.getCompany().getId());
company.getEmployees().remove(employee);session.saveOrUpdate(company);break;
}
}
session.getTransaction().commit();session.close();
}} @OneToMany (cascade=CascadeType.ALL, orphanRemoval = true,
mappedBy = "company")
HCQL (Hibernate Criteria Query Language)
Criteria API ถกออกแบบมาเพอใหผใชสามารถเขยนคาสงเงอนไขตาง ๆ ท
ใชในการควรขอมลทตองการไดอยางมประสทธภาพ
Criteria API ประกอบไปดวยเมธอดตาง ๆ ทใชงานบอย ๆ ดงตอไปน
public Criteria add(Criterion c)
public Criteria addOrder(Order o)
public Criteria setFirstResult(int firstResult)
public Criteria setMaxResult(int totalResult)
public List list()
public Criteria setProjection(Projection projection)
Criteria API : How To
Criteria สามารถเรยกใชออบเจกตจากคลาส Session รวมกบเมธอด
createCriteria() เพอใชในการประมวลผลการควรผานการเรยกใชเมธอดตาง
ๆ พรอมทงระบเงอนไขทผใชตองการทางาน
Criteria criteria = session.createCriteria(Student.class).addOrder(Order.asc("gpa") );
@SuppressWarnings("unchecked")List<Student> results = criteria.list();
for (Student stu: results) {System.out.println(stu);
}
Criteria restrictions
Criteria ยงสามารถใชในการจากดผลลพธจากควร โดยใชคลาส Restriction
ทประกอบไปดวยเมธอดสาคญ ๆ ดงตอไปน
public static SimpleExpression lt(String propertyName,Object value)public static SimpleExpression le(String propertyName,Object value)public static SimpleExpression gt(String propertyName,Object value)public static SimpleExpression ge(String propertyName,Object value)public static SimpleExpression ne(String propertyName,Object value)public static SimpleExpression eq(String propertyName,Object value)public static SimpleExpression like(String propertyName, Object value)public static Criterion between(String propertyName, Object low, Object high)
Restrictions.eq()
Criteria criteria = session.createCriteria(Student.class);criteria.add(Restrictions.eq("major","Information Technology"));@SuppressWarnings("unchecked")
List<Student> results = criteria.list();
for (Student stu: results) {System.out.println(stu);
}
คลาส Restriction จาเปนตองเรยกใชเมธอด add() จากคลาส Criteria เพอ
เรยกใชเมธอดตาง ๆ ทใชในการจากดผลลพธโดยการเปรยบเทยบคา
เชน ในกรณทตองการอานคาแอททรบวต (property) ทมการจากดคา
เฉพาะทเทากบ “equals” สามารถทาไดโดยเรยกใชเมธอด eq() ดงน
Restrictions : others
Criteria criteria = session.createCriteria(Student.class);criteria.add(Restrictions.ne("major","Information Technology"));
@SuppressWarnings("unchecked")List<Student> results = criteria.list();
Criteria criteria = session.createCriteria(Student.class);criteria.add(Restrictions.gt(“gpa", 3.0);
@SuppressWarnings("unchecked")List<Student> results = criteria.list();
Restrictions.like()
การคนหาโดยใชคาสงประเภท SQL LIKE clause รวมกบการเรยกใช
เมธอด like() หรอ ilike() ทมลกษณะเปน case-insensitive ทมโหมดการ
คนหาตาง ๆ อาท ANYWHERE, END, EXACT และ START ดง
ตวอยางตอไปน
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.like("studentName","%sri",MatchMode.ANYWHERE));@SuppressWarnings("unchecked")List<Student> results = criteria.list();
for (Student stu: results) {System.out.println(stu);
}
Suppliers & Product
กาหนดให คลาส Supplier มความสมพนธแบบหนงตอกลมไปยงคลาส Product
ภายในตารางประกอบไปดวยขอมลดงตอไปน
Criteria
Session session = HibernateUtil.getSessionFactory().openSession();Criteria c1=session.createCriteria(Supplier.class);List<Supplier> suppliers = c1.list();for(Supplier s: suppliers){
System.out.println(s);}
Session session = HibernateUtil.getSessionFactory().openSession();Criteria c2 = session.createCriteria(Product.class);c2.add(Restrictions.like("productName", "Dove"));Product pro = (Product) c2.uniqueResult();System.out.println("Name = " + pro.getProductName());
Criteria c3 = session.createCriteria(Product.class);c3.add(Restrictions.le("price", new Double(100.0)));List<Product> products = c3.list();for (Product p: products)System.out.println(p);
Criteria : Cont.
Criteria c4 = session.createCriteria(Product.class);c4.setProjection(Projections.rowCount());List<Long> rows = c4.list();System.out.println("Row count: "+rows);
Criteria c5 = session.createCriteria(Product.class);ProjectionList projList = Projections.projectionList();
projList.add(Projections.max("price"));projList.add(Projections.min("price"));projList.add(Projections.avg("price"));
projList.add(Projections.countDistinct("description"));c5.setProjection(projList);List<Object[]> result = c5.list();
for(int i=0; i<result.size(); i++) {Object[] row = (Object[]) result.get(i);System.out.println("Max price: "+row[0]+" Min price: "+row[1]+
" Avg price: "+row[2]);}
Criteria : Cont.
Max price: 112.0 Min price: 25.0 Avg price: 60.666666666666664
Criteria c6 = session.createCriteria(Product.class);ProjectionList projList1 = Projections.projectionList();projList1.add(Projections.property("productName"));projList1.add(Projections.property("description"));c6.setProjection(projList);
c6.addOrder(Order.asc("price"));List<Object[]> results = c6.list();
for(int i=0; i<results.size(); i++) {Object[] row = (Object[]) results.get(i);System.out.println(" "+row[0]+" "+row[1]);
}
Criteria : Cont.
Lux soapPao detergentDove shampoo
Entity Relationship : Join
Hibernate สนบสนนการทางานแบบ Join 4 ชนดดงตอไปน
Inner join เลอกจบคขอมลจากสองออบเจกตเฉพาะทมคาเทากนเทานน
inner join เปนคา DEFAULT สาหรบการใชงานรวมกบ Hibernate
Left join ใชสาหรบการเลอกจบคขอมลจากทงสองออบเจกตทเทากน
ของการ join โดยเนนไปทออบเจกตทอยทางดานซายมอเปนหลก
แมวาไมมออบเจกตทเทากนปรากฏอยทางดานขวามอกตาม
Right join ใชสาหรบการเลอกจบคขอมลจากทงสองออบเจกตทเทากน
ของการ join โดยเนนไปทออบเจกตทอยทางดานขวามอเปนหลก
แมวาไมมออบเจกตทเทากนปรากฏอยทางดานซายมอกตาม
Full join ใชสาหรบการเลอกจบคขอมลจากทงสองออบเจกตทงท
เทากนและไมเทากนจากทงสองดาน
Bidirectional 1 : M
@Entity @Table(name = "DOCTOR") public class Doctor {
..........;
@OneToMany (cascade=CascadeType.ALL, mappedBy = "doctor")private Set<Patient> patients = new HashSet<Patient>();
}
@Entity @Table (name = "PATIENT") public class Patient {
@ManyToOne @JoinColumn (name = "doctor_id")private Doctor doctor;
Data in DB
Doctor doctor1 = new Doctor("Dr.Sombat Jingjai", "General practitioner"); Patient patient1 = new Patient("Somchai Jaidee", "Headache"); Patient patient2 = new Patient("Somsri Jaipak", "Stomachache");
doctor1.getPatients().add(patient1); doctor1.getPatients().add(patient2); patient1.setDoctor(doctor1);patient2.setDoctor(doctor1);
Doctor doctor2 = new Doctor("Dr.Somporn Jaijam", "Allergist"); Patient patient3 = new Patient("Somrak Jaijing", "cold"); Patient patient4 = new Patient("Somying Jaimon", "burn"); doctor2.getPatients().add(patient3); doctor2.getPatients().add(patient4);
Doctor doctor3 = new Doctor("Dr.Somboon Jaijade", "Surgery");patient3.setDoctor(doctor2);patient4.setDoctor(doctor2);doctor1.setPatients(doctor1.getPatients());doctor2.setPatients(doctor2.getPatients());session.save(doctor1);session.save(doctor2); session.save(doctor3);
Data in DB
Inner Join
String hql = "from Doctor as D inner join D.patients as P";Query query = session.createQuery(hql);List<Object[]> list = query.list();
for(int i=0; i<list.size(); i++) {Object[] row = (Object[]) list.get(i);Doctor doctor = (Doctor)row[0];Patient patient = (Patient)row[1];System.out.println("doctor Id: "+doctor.getDoctorId()+", doctor Name: "+ doctor.getDoctorName()+ ", patient Id: "+ patient.getPatientId()+", patient
Name: "+ patient.getPatientName());}
Left OUTER JOIN
String hql = "from Doctor as D left outer join D.patients as P";List<?> list = session.createQuery(hql).list();for(int i=0; i<list.size(); i++) {Object[] row = (Object[]) list.get(i);Doctor doctor = (Doctor)row[0];Patient patient = (Patient)row[1];if ( doctor != null)
System.out.println("doctor Id: "+doctor.getDoctorId()+", doctor Name: "+ doctor.getDoctorName());
if (patient != null)System.out.println("patient Id: "+ patient.getPatientId()+", patient Name: "+
patient.getPatientName());}
Right OUTER JOIN
String hql = "from Doctor as D right outer join D.patients as P";List<?> list = session.createQuery(hql).list();for(int i=0; i<list.size(); i++) {Object[] row = (Object[]) list.get(i);Doctor doctor = (Doctor)row[0];Patient patient = (Patient)row[1];if ( doctor != null)
System.out.println("doctor Id: "+doctor.getDoctorId()+", doctor Name: "+ doctor.getDoctorName());
if (patient != null)System.out.println("patient Id: "+ patient.getPatientId()+", patient Name: "+
patient.getPatientName());}
Cross JOIN
String hql = "from Doctor as D, patient as P";List<?> list = session.createQuery(hql).list();