Example: hql = \"select p from Person p\"; List result = session.createQuery(hql
ID: 3812312 • Letter: E
Question
Example:
hql = "select p from Person p";
List result = session.createQuery(hql).list();
String toShow = "";
Object[][] tableRow = new Object[result.size()][3];
for (int i = 0; i < result.size(); i++) {
Person thePerson = (Person) result.get(i);
tableRow[i][0] = thePerson.getId();
tableRow[i][1] = thePerson.name;
tableRow[i][2] = thePerson.address;
}
Object[] tableCol = { "ID", "Name", "Address" };
JTable theTable = new JTable(tableRow, tableCol);
JOptionPane.showMessageDialog(null, new JScrollPane(theTable));
session.getTransaction().commit();
person (NAME, ID, ADDRESS, DOB) o faculty (FACULTYID, RANK, SALARY) student (STUDENTID, CLASSIFICATION, GPA, MENTORID, CREDITHOURS) An object-oriented representation is shown as follows. person NAME ADDRE55 DOES ex ends exdends faculty Student RANK CLASSIFICATION GPA CREDITHOURS MENTORS MENTORExplanation / Answer
Two classes will be made,
1) Result.java
public class Result {
private String studentName, facultyName;
/**
* Getter and Setter function for the Output table which
* contain two column -> StudentName and FacultyName
*/
public String getStudentName() {
return studentName;
}
public String getFacultyName() {
return facultyName;
}
public void setStudentName(String name) {
studentName = name;
}
public void setFacultyName(String name) {
facultyName = name;
}
}
2) a.java
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import Result;
public class a {
private static SessionFactory factory;
public static void main(String[] args) {
/**
* Here we first create a session.
* Then queried for the relation of Student Name and Faculty Name and
* store the resultset in a List.
* Then used a loop to print all the rows in the resultset.
* In the finally block, the session is closed.
*/
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List result = session.createQuery("SELECT st.NAME, fac.NAME FROM (SELECT person.NAME as NAME, MENTORID FROM person, student WHERE person.ID = student.STUDENTID) st, (SELECT person.NAME as NAME, FACULTYID FROM person, faculty WHERE person.ID = faculty.FACULTYID) fac WHERE st.MENTORID = fac.FACULTYID").list();
for (Iterator it = result.iterator(); iterator.hasNext();) {
Result res = (Result) iterator.next();
System.out.print("Student Name: " + res.getStudentName());
System.out.print(" Faculty Name: " + res.getFacultyName());
System.out.println();
}
tx.commit();
} catch() {
if (tx != null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.