首页 > Java > java教程 > Hibernate之集合映射详解

Hibernate之集合映射详解

零下一度
发布: 2017-06-27 10:18:19
原创
1339 人浏览过

一 List映射

如果持久化类中具有List对象,可以通过映射文件中的类元素或注解来映射List.

例如,一个问题有多个答案:

 

1)创建持久化类

package list;

import java.util.List;

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

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getQname() {
		return qname;
	}

	public void setQname(String qname) {
		this.qname = qname;
	}

	public List<String> getAnswers() {
		return answers;
	}

	public void setAnswers(List<String> answers) {
		this.answers = answers;
	}
	
	

}
登录后复制

2)创建映射文件

<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="list.Question" table="quesion">  
  	 <cache usage="read-write"/>
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <property name="qname"></property> 
    <list name="answers" table="answers">
    	<key column="qid"></key>
    	<index column="type"></index>
    	<element column="answer" type="string"></element>
    </list>
     
  </class>  

 </hibernate-mapping>
登录后复制

3)在全局配置文件中增加

 	<!-- List of XML mapping files -->
   		<mapping resource="list/Question.hbm.xml"/>
登录后复制

4)测试

package list;

import java.util.ArrayList;

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

public class Test {
	
	public static void main(String[] args) {
		
		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		session.beginTransaction();
		
		ArrayList<String> list1 = new ArrayList<String>();
		list1.add("answer1");
		list1.add("answer2");
		
		Question question1  = new Question();
		question1.setQname("question1");
		question1.setAnswers(list1);
		
		session.save(question1);
		
		session.getTransaction().commit();
		session.close();
		
		
		//factory.close();
		
		
	}

}
登录后复制

二 List一对多映射

一个问题有多个答案,每个答案有自己的信息,需要使用一对多关联来映射。

1)创建持久化类

package list;

import java.util.List;

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

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getQname() {
		return qname;
	}

	public void setQname(String qname) {
		this.qname = qname;
	}

	public List<Answer> getAnswers() {
		return answers;
	}

	public void setAnswers(List<Answer> answers) {
		this.answers = answers;
	}


	
	

}
登录后复制
package list;

public class Answer {
	
	private int id;
	
	private String answername;
	
	private String postedBy;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAnswername() {
		return answername;
	}

	public void setAnswername(String answername) {
		this.answername = answername;
	}

	public String getPostedBy() {
		return postedBy;
	}

	public void setPostedBy(String postedBy) {
		this.postedBy = postedBy;
	}
	
	

}
登录后复制

2)配置文件

<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="list.Question" table="quesion">  
  	 <cache usage="read-write"/>
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <property name="qname"></property> 
    <list name="answers" cascade="all">
    	<key column="qid"></key>
    	<index column="type"></index>
    	<one-to-many class="list.Answer"/>
    </list>
   </class>
   
    <class name="list.Answer" table="answers">  
	  	 <cache usage="read-write"/>
	    <id name="id">  
	     <generator class="increment"></generator>  
	    </id>  
	    <property name="answername"></property> 
	    <property name="postedBy"></property>
    </class>  

 </hibernate-mapping>
登录后复制

3)在hibernate.cfg.xml中添加配置

	 	<!-- List of XML mapping files -->
   		<mapping resource="list/Question.hbm.xml"/>
登录后复制

4)测试

package list;

import java.util.ArrayList;

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

public class Test {
	
	public static void main(String[] args) {
		
		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		session.beginTransaction();
		
		Answer ans1 = new Answer();
		ans1.setAnswername("ans1");
		ans1.setPostedBy("post1");
		
		Answer ans2 = new Answer();
		ans2.setAnswername("ans2");
		ans2.setPostedBy("post2");
		
		Answer ans3 = new Answer();
		ans3.setAnswername("ans3");
		ans3.setPostedBy("post3");
		ArrayList<Answer> list1 = new ArrayList<Answer>();
		list1.add(ans1);
		list1.add(ans2);
		list1.add(ans3);
		Question question1  = new Question();
		question1.setQname("question1");
		question1.setAnswers(list1);
		session.save(question1);
		session.getTransaction().commit();
		session.close();
		//factory.close();
			
	}
}
登录后复制

以上是Hibernate之集合映射详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板