Hibernate Mapping Map using xml file

Hibernate enables you to outline components with the RDBMS. As we probably am know, index and map are record based accumulations. In the event of map, index column fills in as the key and component segment functions as the value.

Example of Mapping Map in accumulation mapping utilizing xml file

You need to make following pages for mapping map elements.

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

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

<?xml version='1.0' encoding='UTF-8'?>  
  • <!DOCTYPE hibernate-mapping PUBLIC  
  •           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  •           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  •             
  • <hibernate-mapping>  
  •   
  • <class name="com.javacodegeeks.Question" table="question736">  
  • <id name="id">  
  • <generator class="native"></generator>  
  • </id>  
  • <property name="name"></property>  
  • <property name="username"></property>  
  •   
  • <map name="answers" table="answer736" cascade="all">  
  • <key column="questionid"></key>  
  • <index column="answer" type="string"></index>  
  • <element column="username" type="string"></element>  
  • </map>  
  • </class>  
  •   
  • </hibernate-mapping>                    
  • hibernate.cfg.xml
    1. <?xml version='1.0' encoding='UTF-8'?>  
    2. <!DOCTYPE hibernate-configuration PUBLIC  
    3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
    4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
    5.   
    6. <hibernate-configuration>  
    7.   
    8.     <session-factory>  
    9.         <property name="hbm2ddl.auto">update</property>  
    10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
    11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
    12.         <property name="connection.username">system</property>  
    13.         <property name="connection.password">jtp</property>  
    14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    15.       
    16.     <mapping resource="question.hbm.xml"/>  
    17.     </session-factory>  
    18.   
    19. </hibernate-configuration>  

    StoreTest.java

    1. package com.javacodegeeks;    
    2.     
    3. import java.util.HashMap;    
    4. import org.hibernate.*;  
    5. import org.hibernate.boot.Metadata;  
    6. import org.hibernate.boot.MetadataSources;  
    7. import org.hibernate.boot.registry.StandardServiceRegistry;  
    8. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;    
    9.   
    10.   
    11. public class StoreTest {    
    12. public static void main(String[] args) {    
    13.    
    14.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
    15.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
    16.       
    17.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
    18.     Session session=factory.openSession();  
    19.    
    20. Transaction t=session.beginTransaction();    
    21.     
    22. HashMap<String,String> map1=new HashMap<String,String>();    
    23. map1.put("Java is a programming language","John Milton");    
    24. map1.put("Java is a platform","Ashok Kumar");    
    25.     
    26. HashMap<String,String> map2=new HashMap<String,String>();    
    27. map2.put("Servlet technology is a server side programming","John Milton");    
    28. map2.put("Servlet is an Interface","Ashok Kumar");    
    29. map2.put("Servlet is a package","Rahul Kumar");    
    30.     
    31. Question question1=new Question("What is Java?","Alok",map1);    
    32. Question question2=new Question("What is Servlet?","Jai Dixit",map2);    
    33.     
    34. session.persist(question1);    
    35. session.persist(question2);    
    36.     
    37. t.commit();    
    38. session.close();    
    39. System.out.println("successfully stored");    
    40. }    
    41. }    

    Output

    Hibernate Mapping Map Example 1 Hibernate Mapping Map Example 2

    FetchTest.java

    1. package com.javatpoint;    
    2. import java.util.*;  
    3.   
    4. import javax.persistence.TypedQuery;  
    5.   
    6. import org.hibernate.*;  
    7. import org.hibernate.boot.Metadata;  
    8. import org.hibernate.boot.MetadataSources;  
    9. import org.hibernate.boot.registry.StandardServiceRegistry;  
    10. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
    11.    
    12. public class FetchTest {    
    13. public static void main(String[] args) {    
    14.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
    15.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
    16.       
    17.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
    18.     Session session=factory.openSession();    
    19.             
    20.  TypedQuery query=session.createQuery("from Question ");    
    21.  List<Question> list=query.getResultList();    
    22.             
    23.  Iterator<Question> iterator=list.iterator();    
    24.  while(iterator.hasNext()){    
    25.   Question question=iterator.next();    
    26.   System.out.println("question id:"+question.getId());    
    27.   System.out.println("question name:"+question.getName());    
    28.   System.out.println("question posted by:"+question.getUsername());    
    29.   System.out.println("answers.....");    
    30.   Map<String,String> map=question.getAnswers();    
    31.   Set<Map.Entry<String,String>> set=map.entrySet();    
    32.                 
    33.   Iterator<Map.Entry<String,String>> iteratoranswer=set.iterator();    
    34.   while(iteratoranswer.hasNext()){    
    35.    Map.Entry<String,String> entry=(Map.Entry<String,String>)iteratoranswer.next();    
    36.    System.out.println("answer name:"+entry.getKey());    
    37.    System.out.println("answer posted by:"+entry.getValue());    
    38.   }    
    39.  }    
    40. session.close();    
    41. }    
    42. }    





    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition