AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-hibernate: How to fetch data from more than one table in Hibernate

i want information that how can i fatch data from more than one table in hibernate and how to create mapping file for it.

java x 210
hibernate x 23
Posted On : 2016-08-16 08:33:23.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
267150
up-rate
3
down-rate

Answers


It depends on what kind of relationship you maintained between those tables. Let´s consider there is one to many relation between two tables. If the persistent class has list of objects that contains the entity reference, we need to use one-to-many association to map the list element.

Here, we are using the scenario where one question has multiple answers.

Let´s see the persistent class that has list objects (containing Answer class objects).

1) create the Persistent class:
----------------------------------------------------------
This persistent class defines properties of the class including List.

Question.java:
---------------------------------
package in.anyforum;

import java.util.List;

public class Question {
private int id;
private String qname;
private List<Answer> answers;

//getters and setters

}

Answer.java:
-------------------------------
package in.anyforum;

public class Answer {
private int id;
private String answername;
private String postedBy;
//getters and setters

}
}


2) create the Mapping file for the persistent class:
-------------------------------------------------------------------------------------------
Here, we have created the question.hbm.xml file for defining the list.

<?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="in.anyforum.Question" table="question">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="qname"></property>

<list name="answers" cascade="all">
<key column="qid"></key>
<index column="type"></index>
<one-to-many class="in.anyforum.Answer"/>
</list>

</class>

<class name="in.anyforum.Answer" table="answer">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="answername"></property>
<property name="postedBy"></property>
</class>

</hibernate-mapping>


3) create the configuration file:
--------------------------------------------------------------------
This file contains information about the database and mapping file.

<?xml version=´1.0´ encoding=´UTF-8´?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="question.hbm.xml"/>
</session-factory>

</hibernate-configuration>


4) Create the class to store the data:
----------------------------------------------------------------
In this class we are storing the data of the question class.

package in.anyforum;
import java.util.ArrayList;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
Session session=new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory().openSession();
Transaction t=session.beginTransaction();

Answer ans1=new Answer();
ans1.setAnswername("java is a programming language");
ans1.setPostedBy("AAA");

Answer ans2=new Answer();
ans2.setAnswername("java is a platform");
ans2.setPostedBy("BBB");

Answer ans3=new Answer();
ans3.setAnswername("Servlet is an Interface");
ans3.setPostedBy("CCC");

Answer ans4=new Answer();
ans4.setAnswername("Servlet is an API");
ans4.setPostedBy("DDD");

ArrayList<Answer> list1=new ArrayList<Answer>();
list1.add(ans1);
list1.add(ans2);

ArrayList<Answer> list2=new ArrayList<Answer>();
list2.add(ans3);
list2.add(ans4);

Question question1=new Question();
question1.setQname("What is Java?");
question1.setAnswers(list1);

Question question2=new Question();
question2.setQname("What is Servlet?");
question2.setAnswers(list2);

session.persist(question1);
session.persist(question2);

t.commit();
session.close();
System.out.println("success");
}
}


How to fetch the data of List:
---------------------------------------------------------------
Here, we have used HQL to fetch all the records of Question class including answers. In such case, it fetches the data from two tables that are functional dependent. Here, we are direct printing the object of answer class, but we have overridden the toString() method in the Answer class returning answername and poster name. So it prints the answer name and postername rather than reference id.

FetchData.java:
-----------------------------------------
package in.anyforum;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class FetchData {
public static void main(String[] args) {

Session session=new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory().openSession();

Query query=session.createQuery("from Question");
List<Question> list=query.list();

Iterator<Question> itr=list.iterator();
while(itr.hasNext()){
Question q=itr.next();
System.out.println("Question Name: "+q.getQname());

//printing answers
List<Answer> list2=q.getAnswers();
Iterator<Answer> itr2=list2.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}

}
session.close();
System.out.println("success");
}
}

Posted On : 2016-08-17 23:24:02
Satisfied : 2 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222689
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in