-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: [Solved] The foreign key is not being displayed!
PostPosted: Thu Dec 17, 2009 9:08 pm 
Newbie

Joined: Mon Dec 07, 2009 5:22 pm
Posts: 2
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
reply5


The 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.


Last edited by Yeamin on Sun Dec 20, 2009 7:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: The foreign key is not being displayed!
PostPosted: Sat Dec 19, 2009 5:02 am 
Beginner
Beginner

Joined: Mon Dec 14, 2009 12:26 am
Posts: 23
Edit: Whoops, I lost your actual question in the bulk of your post.

I only use Hibernate via JPA2 and annotations mapping, so the XML mapping files don't tell me much. Sorry.


Top
 Profile  
 
 Post subject: [Solved] Re: The foreign key is not being displayed!
PostPosted: Sun Dec 20, 2009 7:05 pm 
Newbie

Joined: Mon Dec 07, 2009 5:22 pm
Posts: 2
ringerc wrote:
Edit: Whoops, I lost your actual question in the bulk of your post.

I only use Hibernate via JPA2 and annotations mapping, so the XML mapping files don't tell me much. Sorry.


Ringerc,

Hello. Yes I was initially thinking of the bulk of code being confusing for people. Well I kind of figured out what the problem was. As I mentioned on my first post that I am not so confident with my Java skills, I happened to have left something out on my main method which was causing the error.

In fact I had two problems with my project. My mapping associations were incorrect. I was trying to do a 'many-to-one' association for something that could be done with a 'one-to-one' association. So I reverted back to the 'one-to-one' association. So the following changes had to be made.

On the XML mapping files:

For the Student.hbm.xml:

I simply added: <one-to-one name="questions" class="Questions"/>

On the Questions.hbm.xml:

Inside the 'id' element:

<generator class="foreign">
<param name="property">student</param>
</generator>


And the mapping association element:

<one-to-one name="student" class="Student" constrained="true"/>


I have had to add the following lines as well on the POJOs:

Student.java:

private Questions questions;

//Getters and Setters
public Questions getQuestions() {
return questions;
}

public void setQuestions(Questions questions) {
this.questions = questions;
}


Question.java:

private Student student;
//Getters and Setters
public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}


I had to add the following lines onto my 'main' method too to establish the relationship between the entities:


Student s1 = new Student();
Questions q1= new Questions();

s1.setQuestions(q1);
q1.setStudent(s1);


Once again I am really sorry for pasting in all the codes for the separate files on my initial post. It must have discouraged many potential helpers.

Thanks for reading
Yeamin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.