📜  Hibernate教程:在集合映射中映射映射

📅  最后修改于: 2021-01-02 15:38:01             🧑  作者: Mango

使用xml文件的Hibernate Mapping Map

Hibernate允许您使用RDBMS映射Map元素。众所周知,列表和映射是基于索引的集合。对于map,索引列用作键,元素列用作值。

使用xml文件的集合映射中的映射映射示例

您需要创建以下页面来映射地图元素。

  • Question.java
  • question.hbm.xml
  • hibernate.cfg.xml
  • StoreTest.java
  • FetchTest.java
package com.javatpoint;

import java.util.Map;

public class Question {
private int id;
private String name,username;
private Map answers;

public Question() {}
public Question(String name, String username, Map answers) {
    super();
    this.name = name;
    this.username = username;
    this.answers = answers;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public Map getAnswers() {
    return answers;
}
public void setAnswers(Map answers) {
    this.answers = answers;
}
}

question.hbm.xml



          
















                  





    
        update
        org.hibernate.dialect.Oracle9Dialect
        jdbc:oracle:thin:@localhost:1521:xe
        system
        jtp
        oracle.jdbc.driver.OracleDriver
    
    
    


StoreTest.java

package com.javatpoint;  
  
import java.util.HashMap;  
import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  


public class StoreTest {  
public static void main(String[] args) {  
 
    StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
    
    SessionFactory factory=meta.getSessionFactoryBuilder().build();
    Session session=factory.openSession();
 
Transaction t=session.beginTransaction();  
  
HashMap map1=new HashMap();  
map1.put("Java is a programming language","John Milton");  
map1.put("Java is a platform","Ashok Kumar");  
  
HashMap map2=new HashMap();  
map2.put("Servlet technology is a server side programming","John Milton");  
map2.put("Servlet is an Interface","Ashok Kumar");  
map2.put("Servlet is a package","Rahul Kumar");  
  
Question question1=new Question("What is Java?","Alok",map1);  
Question question2=new Question("What is Servlet?","Jai Dixit",map2);  
  
session.persist(question1);  
session.persist(question2);  
  
t.commit();  
session.close();  
System.out.println("successfully stored");  
}  
}  

输出量


FetchTest.java

package com.javatpoint;  
import java.util.*;

import javax.persistence.TypedQuery;

import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 
public class FetchTest {  
public static void main(String[] args) {  
    StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
    
    SessionFactory factory=meta.getSessionFactoryBuilder().build();
    Session session=factory.openSession();  
          
 TypedQuery query=session.createQuery("from Question ");  
 List list=query.getResultList();  
          
 Iterator iterator=list.iterator();  
 while(iterator.hasNext()){  
  Question question=iterator.next();  
  System.out.println("question id:"+question.getId());  
  System.out.println("question name:"+question.getName());  
  System.out.println("question posted by:"+question.getUsername());  
  System.out.println("answers.....");  
  Map map=question.getAnswers();  
  Set> set=map.entrySet();  
              
  Iterator> iteratoranswer=set.iterator();  
  while(iteratoranswer.hasNext()){  
   Map.Entry entry=(Map.Entry)iteratoranswer.next();  
   System.out.println("answer name:"+entry.getKey());  
   System.out.println("answer posted by:"+entry.getValue());  
  }  
 }  
session.close();  
}  
}  

输出量

下载