Set ing of Hibernate and hibernate ing

Source: Internet
Author: User

Set ing of Hibernate and hibernate ing
List ing

If the persistence class has a List object, you can map the list object by using the <List> element or annotation in the ing file.

For example, one 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;}}

2) create a ing File

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

3) add

 <!-- List of XML mapping files -->   <mapping resource="list/Question.hbm.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();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();}}

Two-List one-to-multiple ing

One question has multiple answers, and each answer has its own information. You need to use one-to-many associations for ing.

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;}}
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) configuration file

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

3) Add the configuration in hibernate. cfg. xml.

 <!-- List of XML mapping files -->   <mapping resource="list/Question.hbm.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();}}

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.