Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Mapping Map


Hibernate allows you to map Map elements with the RDBMS. As we know, list and map are index-based collections. In case of map, index column works as the key and element column works as the value.

Example of Mapping Map in collection mapping using xml file

You need to create following pages for mapping map elements.

  • Question.java
  • question.hbm.xml
  • hibernate.cfg.xml
  • StoreTest.java
  • FetchTest.java

Question.java

  1. package com.Kreationnext;
  2. import java.util.Map;
  3. public class Question {
  4. private int id;
  5. private String name,username;
  6. private Map<String,String> answers;
  7. public Question() {}
  8. public Question(String name, String username, Map<String, String> answers) {
  9.     super();
  10.     this.name = name;
  11.     this.username = username;
  12.     this.answers = answers;
  13. }
  14. public int getId() {
  15.     return id;
  16. }
  17. public void setId(int id) {
  18.     this.id = id;
  19. }
  20. public String getName() {
  21.     return name;
  22. }
  23. public void setName(String name) {
  24.     this.name = name;
  25. }
  26. public String getUsername() {
  27.     return username;
  28. }
  29. public void setUsername(String username) {
  30.     this.username = username;
  31. }
  32. public Map<String, String> getAnswers() {
  33.     return answers;
  34. }
  35. public void setAnswers(Map<String, String> answers) {
  36.     this.answers = answers;
  37. }
  38. }

question.hbm.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.           “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5. <hibernate-mapping>
  6. <class name=“com.Kreationnext.Question” table=“question736”>
  7. <id name=“id”>
  8. <generator class=“native”></generator>
  9. </id>
  10. <property name=“name”></property>
  11. <property name=“username”></property>
  12. <map name=“answers” table=“answer736” cascade=“all”>
  13. <key column=“questionid”></key>
  14. <index column=“answer” type=“string”></index>
  15. <element column=“username” type=“string”></element>
  16. </map>
  17. </class>
  18. </hibernate-mapping>

hibernate.cfg.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  5. <!– Generated by MyEclipse Hibernate Tools.                   –>
  6. <hibernate-configuration>
  7.     <session-factory>
  8.         <property name=“hbm2ddl.auto”>update</property>
  9.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  10.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  11.         <property name=“connection.username”>system</property>
  12.         <property name=“connection.password”>oracle</property>
  13.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  14.     <mapping resource=“question.hbm.xml”/>
  15.     </session-factory>
  16. </hibernate-configuration>

StoreTest.java

  1. package com.Kreationnext;
  2. import java.util.HashMap;
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.*;
  5. public class StoreTest {
  6. public static void main(String[] args) {
  7. Session session=new Configuration().configure().buildSessionFactory().openSession();
  8. Transaction tx=session.beginTransaction();
  9. HashMap<String,String> map1=new HashMap<String,String>();
  10. map1.put(“java is a programming language”,“John Milton”);
  11. map1.put(“java is a platform”,“Ashok Kumar”);
  12. HashMap<String,String> map2=new HashMap<String,String>();
  13. map2.put(“servlet technology is a server side programming”,“John Milton”);
  14. map2.put(“Servlet is an Interface”,“Ashok Kumar”);
  15. map2.put(“Servlet is a package”,“Rahul Kumar”);
  16. Question question1=new Question(“What is java?”,“Alok”,map1);
  17. Question question2=new Question(“What is servlet?”,“Jai Dixit”,map2);
  18. session.persist(question1);
  19. session.persist(question2);
  20. tx.commit();
  21. session.close();
  22. System.out.println(“successfully stored”);
  23. }
  24. }

FetchTest.java

  1. package com.Kreationnext;
  2. import java.util.*;
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.*;
  5. public class FetchTest {
  6. public static void main(String[] args) {
  7.  Session session=new Configuration().configure().buildSessionFactory().openSession();
  8.  Query query=session.createQuery(“from Question “);
  9.  List<Question> list=query.list();
  10.  Iterator<Question> iterator=list.iterator();
  11.  while(iterator.hasNext()){
  12.   Question question=iterator.next();
  13.   System.out.println(“question id:”+question.getId());
  14.   System.out.println(“question name:”+question.getName());
  15.   System.out.println(“question posted by:”+question.getUsername());
  16.   System.out.println(“answers…..”);
  17.   Map<String,String> map=question.getAnswers();
  18.   Set<Map.Entry<String,String>> set=map.entrySet();
  19.   Iterator<Map.Entry<String,String>> iteratoranswer=set.iterator();
  20.   while(iteratoranswer.hasNext()){
  21.    Map.Entry<String,String> entry=(Map.Entry<String,String>)iteratoranswer.next();
  22.    System.out.println(“answer name:”+entry.getKey());
  23.    System.out.println(“answer posted by:”+entry.getValue());
  24.   }
  25.  }
  26. session.close();
  27. }
  28. }

Comments

comments