Home > Java > javaTutorial > Detailed explanation of Hibernate collection mapping

Detailed explanation of Hibernate collection mapping

零下一度
Release: 2017-06-27 10:18:19
Original
1361 people have browsed it

One List Mapping

If there is a List object in the persistence class, the List can be mapped through the class element or annotation in the mapping file.

For example, a question has Multiple answers:

##1) Create a persistence class

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;
	}
	
	

}
Copy after login
2) Create a mapping file

<?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>
Copy after login
3) Add

 	<!-- List of XML mapping files -->
   		<mapping resource="list/Question.hbm.xml"/>
Copy after login
# in the global configuration file 4) Test

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();
		
		
	}

}
Copy after login

Two List one-to-many mapping

A question has multiple answers, each answer has its own information, and one-to-many association needs to be used to map.

1) Create a persistence class

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;
	}


	
	

}
Copy after login
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;
	}
	
	

}
Copy after login
2) Configuration file

<?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>
Copy after login
3 ) Add configuration

	 	<!-- List of XML mapping files -->
   		<mapping resource="list/Question.hbm.xml"/>
Copy after login
4 in hibernate.cfg.xml 4) Test

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();
			
	}
}
Copy after login

The above is the detailed content of Detailed explanation of Hibernate collection mapping. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template