Mapping List in Collection Mapping

Mapping List in Collection Mapping (using xml file)

If our persistent class has List object, we can map the List easily either by <list> element of class in mapping file or by annotation.

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

In this example, we are going to see full example of collection mapping by list. This is the example of List that stores string value not entity reference that is why are going to use element instead of one-to- many within the list element.

Question.java

package com.smgc;

import java.util.List;

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

       //generate getters and setters
}

Question.hbm.xml

<?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="com.smgc.Question" table="q100">
              <id name="id">
                     <generator class="increment"></generator>
              </id>
              <property name="qname"></property>

              <list name="answers" table="ans100">
                     <key column="qid"></key>
                     <index column="type"></index>
                     <element column="answer" type="string"></element>
              </list>

       </class>
</hibernate-mapping>

hibernate.cfg.xml

<?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>
              <!-- Database connection settings -->
              <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</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.pool_size">1</property>
              <!-- SQL dialect -->
              <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

              <!-- Show all executed SQL to stdout -->
              <property name="show_sql">true</property>

              <!-- Drop and re-create the database schema on startup -->
              <property name="hbm2ddl.auto">update</property>

             
              <mapping resource="Question.hbm.xml" />

       </session-factory>
</hibernate-configuration>

StoreMain.java

package com.smgc;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class StoreData {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub
                                Session session = new AnnotationConfiguration().configure()
                                                                .buildSessionFactory().openSession();
                                Transaction t = session.beginTransaction();

                                ArrayList<String> list1 = new ArrayList<String>();
                                list1.add("java is a programming language");
                                list1.add("java is a platform");
                                list1.add("java is a iceland");
                                list1.add("java is a country");

                                ArrayList<String> list2 = new ArrayList<String>();
                                list2.add("Servlet is an Interface");
                                list2.add("Servlet is an API");

                                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");
                }
}

If you run storemain .java, it will create two tables in database and store data in it.

FetchMain.java

package com.smgc;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class FetchData {

                public static void main(String[] args) {
                                // TODO Auto-generated method stub
                                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();
                                System.out.println(itr.hasNext());
                                while (itr.hasNext()) {                                   
                                                Question q = itr.next();
                                                System.out.println("Question Name: " + q.getQname());

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


If you run fetchmain .java, it will display data form table.


Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes