Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Many to Many by Map


We can map many to many relation either using set, bag, map etc. Here, we are going to use map for many-to-many mapping. In such case, three tables will be created.

Example of Many to Many Mapping

You need to create following pages for mapping map elements.

  • Question.java
  • User.java
  • question.hbm.xml
  • user.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;
  6. private Map<String,User> answers;
  7. public Question() {}
  8. public Question(String name, Map<String, User> answers) {
  9.     super();
  10.     this.name = name;
  11.     this.answers = answers;
  12. }
  13. public int getId() {
  14.     return id;
  15. }
  16. public void setId(int id) {
  17.     this.id = id;
  18. }
  19. public String getName() {
  20.     return name;
  21. }
  22. public void setName(String name) {
  23.     this.name = name;
  24. }
  25. public Map<String, User> getAnswers() {
  26.     return answers;
  27. }
  28. public void setAnswers(Map<String, User> answers) {
  29.     this.answers = answers;
  30. }
  31. }

User.java

  1. package com.Kreationnext;
  2. public class User {
  3. private int id;
  4. private String username,email,country;
  5. public User() {}
  6. public User(String username, String email, String country) {
  7.     super();
  8.     this.username = username;
  9.     this.email = email;
  10.     this.country = country;
  11. }
  12. public int getId() {
  13.     return id;
  14. }
  15. public void setId(int id) {
  16.     this.id = id;
  17. }
  18. public String getUsername() {
  19.     return username;
  20. }
  21. public void setUsername(String username) {
  22.     this.username = username;
  23. }
  24. public String getEmail() {
  25.     return email;
  26. }
  27. public void setEmail(String email) {
  28.     this.email = email;
  29. }
  30. public String getCountry() {
  31.     return country;
  32. }
  33. public void setCountry(String country) {
  34.     this.country = country;
  35. }
  36. }

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=“question738”>
  7. <id name=“id”>
  8. <generator class=“native”></generator>
  9. </id>
  10. <property name=“name”></property>
  11. <map name=“answers” table=“answer738” cascade=“all”>
  12. <key column=“questionid”></key>
  13. <index column=“answer” type=“string”></index>
  14. <many-to-many class=“com.Kreationnext.User” column=“userid”></many-to-many>
  15. </map>
  16. </class>
  17. </hibernate-mapping>

user.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.User” table=“user738”>
  7. <id name=“id”>
  8. <generator class=“native”></generator>
  9. </id>
  10. <property name=“username”></property>
  11. <property name=“email”></property>
  12. <property name=“country”></property>
  13. </class>
  14. </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.     <mapping resource=“user.hbm.xml”/>
  16.     </session-factory>
  17. </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,User> map1=new HashMap<String,User>();
  10. map1.put(“java is a programming language”,
  11. new User(“John Milton”,“john@gmail.com”,“usa”));
  12. map1.put(“java is a platform”,
  13. new User(“Ashok Kumar”,“ashok@gmail.com”,“india”));
  14. HashMap<String,User> map2=new HashMap<String,User>();
  15. map2.put(“servlet technology is a server side programming”,
  16. new User(“John Milton”,“john@gmail.com”,“usa”));
  17. map2.put(“Servlet is an Interface”,
  18. new User(“Ashok Kumar”,“ashok@gmail.com”,“india”));
  19. Question question1=new Question(“What is java?”,map1);
  20. Question question2=new Question(“What is servlet?”,map2);
  21. session.persist(question1);
  22. session.persist(question2);
  23. tx.commit();
  24. session.close();
  25. System.out.println(“successfully stored”);
  26. }
  27. }

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(“answers…..”);
  16.   Map<String,User> map=question.getAnswers();
  17.   Set<Map.Entry<String,User>> set=map.entrySet();
  18.   Iterator<Map.Entry<String,User>> iteratoranswer=set.iterator();
  19.   while(iteratoranswer.hasNext()){
  20.    Map.Entry<String,User> entry=(Map.Entry<String,User>)iteratoranswer.next();
  21.    System.out.println(“answer name:”+entry.getKey());
  22.    System.out.println(“answer posted by………”);
  23.    User user=entry.getValue();
  24.    System.out.println(“username:”+user.getUsername());
  25.    System.out.println(“user emailid:”+user.getEmail());
  26.    System.out.println(“user country:”+user.getCountry());
  27.   }
  28.  }
  29. session.close();
  30. }
  31. }

Comments

comments