Hi. I am a newbie in Hibernate and I have been trying to develop a simple Hibernate project for my last year project at university. This is my first post in this forum. I'll try to explain what I am trying to do in brief along with the codes. I would really appreciate it if some helps me out with this rather silly problem.
At the end of every semester at University students are given questionnaires regarding their experience for that semester, the modules etc. I have two tables called Student and Questions. The Questions table has 5 questions and 5 replies in the form of numbers from 1-5; 1 being poor and 5 being excellent. Now when I am executing my main method, I can't get the Student object to set the 'qid' and as a result although the Student and Question objects are being created the Student table only shows the 'id' and 'name' and nothing on the 'qid' row:
******** Table: student *******
+----------+----------------------+-------------+
|.....ID.......|.............NAME...........|.......QID.......|
+----------+----------------------+-------------+
| 1048576 | Samia Oussena .........|...................|
| 1048577 | Yeamin Arafat...........|....................|
+----------+---------------------.+-------------+
The attributes for the Questions table are:
qid <PK in the corresponding Questions table>
question1
reply1
question2
reply2
question3
reply3
question4
reply4
question5
reply5The POJO for the Questions class is as follows:
Code:
import java.io.Serializable;
public class Questions implements Serializable{
private int qid;
private String question1;
private int reply1;
private String question2;
private int reply2;
private String question3;
private int reply3;
private String question4;
private int reply4;
private String question5;
private int reply5;
public Questions() {
}
public Questions(String question1, int reply1, String question2, int reply2, String question3, int reply3, String question4, int reply4, String question5, int reply5) {
this.question1 = question1;
this.reply1 = reply1;
this.question2 = question2;
this.reply2 = reply2;
this.question3 = question3;
this.reply3 = reply3;
this.question4 = question4;
this.reply4 = reply4;
this.question5 = question5;
this.reply5 = reply5;
}
public int getQid() {
return qid;
}
public void setQid(int qid) {
this.qid = qid;
}
public String getQuestion1() {
return question1;
}
public void setQuestion1(String question1) {
this.question1 = question1;
}
public String getQuestion2() {
return question2;
}
public void setQuestion2(String question2) {
this.question2 = question2;
}
public String getQuestion3() {
return question3;
}
public void setQuestion3(String question3) {
this.question3 = question3;
}
public String getQuestion4() {
return question4;
}
public void setQuestion4(String question4) {
this.question4 = question4;
}
public String getQuestion5() {
return question5;
}
public void setQuestion5(String question5) {
this.question5 = question5;
}
public int getReply1() {
return reply1;
}
public void setReply1(int reply1) {
this.reply1 = reply1;
}
public int getReply2() {
return reply2;
}
public void setReply2(int reply2) {
this.reply2 = reply2;
}
public int getReply3() {
return reply3;
}
public void setReply3(int reply3) {
this.reply3 = reply3;
}
public int getReply4() {
return reply4;
}
public void setReply4(int reply4) {
this.reply4 = reply4;
}
public int getReply5() {
return reply5;
}
public void setReply5(int reply5) {
this.reply5 = reply5;
}
The primary key of the Questions table is going to be the foreign key in the Student table where the attributes are:
id<PK in the corresponding Student table>
name
qid<FK referencing qid of Questions table>
The POJO for the Student class is as follows:
Code:
import java.io.Serializable;
public class Student implements Serializable{
private int id;
private String name;
private int qid;
private Questions questions;
public Student(){
}
public Student(int id, String name, int qid) {
this.name = name;
this.qid = qid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQid() {
return qid;
}
public void setQid(int qid) {
this.qid = qid;
}
public Questions getQuestions() {
return questions;
}
public void setQuestions(Questions questions) {
this.questions = questions;
}
}
The code on the corresponding two mapping files are as follows:
Questions:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Questions" table="questions">
<id name="qid" type="int">
<generator class="assigned"/>
</id>
<property column="question1" name="question1" type="string"/>
<property column="reply1" name="reply1" type="int"/>
<property column="question2" name="question2" type="string"/>
<property column="reply2" name="reply2" type="int"/>
<property column="question3" name="question3" type="string"/>
<property column="reply3" name="reply3" type="int"/>
<property column="question4" name="question4" type="string"/>
<property column="reply4" name="reply4" type="int"/>
<property column="question5" name="question5" type="string"/>
<property column="reply5" name="reply5" type="int"/>
</class>
</hibernate-mapping>
Student:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Student" table="student" >
<id name="id" type="int">
<generator class="hilo"/>
</id>
<property name="name" column="name" type="string" />
<many-to-one name="questions" class="Questions" column="qid" unique="true" foreign-key="fk_qid"/>
</class>
</hibernate-mapping>
Following is my main method where I seem to have the problem. I am highlighting the bit with ** where I think I am doing it wrong.
Code:
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
// Set up database tables
HibernateUtil.droptable("DROP TABLE student");
HibernateUtil.droptable("DROP TABLE questions");
// Create SessionFactory and Session object
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
// Perform life-cycle operations under a transaction
Transaction tx = null;
try{
tx = session.beginTransaction();
// Create a Questions Object
Questions q1 = new Questions();
q1.setQid(3030);
q1.setQuestion1("The way in which the information was presented");
q1.setReply1(4);
q1.setQuestion2("The amount of information presented");
q1.setReply2(3);
q1.setQuestion3("Usefulness of the handout");
q1.setReply3(4);
q1.setQuestion4("Your confidence before the session in using the sources demonstrated");
q1.setReply4(3);
q1.setQuestion5("Your confidence in using the sources now");
q1.setReply5(4);
session.save(q1);
// Create another Questions Object
Questions q2 = new Questions();
q2.setQid(2020);
q2.setQuestion1("The way in which the information was presented");
q2.setReply1(2);
q2.setQuestion2("The amount of information presented");
q2.setReply2(3);
q2.setQuestion3("Usefulness of the handout");
q2.setReply3(4);
q2.setQuestion4("Your confidence before the session in using the sources demonstrated");
q2.setReply4(5);
q2.setQuestion5("Your confidence in using the sources now");
q2.setReply5(2);
session.save(q2);
// Create a Student object and save it
Student s1 = new Student();
s1.setId(20301234);
s1.setName("Samia Oussena");
** s1.setQid(2020); [This bit where I am trying to set the 'qid' manually]
session.save(s1);
session.flush();
// Create another Student object and save it.
Student s2 = new Student();
s2.setId(20221273);
s2.setName("Yeamin Arafat");
** s2.setQid(3030); [color=#FF0000][And this..][/color]
session.save(s2);
session.flush();
// Retrieve the Student objects
Student student = (Student)session.get(Student.class, s1.getId());
System.out.println("Name of the first person: " + student.getName());
student = (Student)session.get(Student.class, s2.getId());
System.out.println("Name of the second person: " + student.getName());
//Retrieve the Questions ojects
Questions questions = (Questions)session.get(Questions.class, q1.getQid());
System.out.println("The first person " + s1.getName() + " has Question id: " + questions.getQid());
questions = (Questions)session.get(Questions.class, q2.getQid());
System.out.println("The second person" + s2.getName() + " has Question id: " + questions.getQid());
tx.commit();
tx = null;
}catch(HibernateException e){
if (tx!=null){
tx.rollback();
e.printStackTrace();
}
}finally{
session.close();
}
//Display tables
HibernateUtil.checkData("select * from student");
HibernateUtil.checkData("select * from questions");
}
}
The whole post looks huge so I am not going to paste the other HibernateUtil and Hibernate configuration file to make it look worse.
I am exposed to JavaEE for the first time trying to use Hibernate and my Java knowledge hasn't been very solid either. I would really appreciate it if someone took their precious time and helped me out with this silly problem. It would mean a lot to me. I am still having to write the report and I don't really have much time left in hand.
Warm Regards and thanks for reading,
Yeamin.