Hibernate之集合映射
2021-07-14 11:05
阅读:562
标签:分享 ns2 png list conf pac 全局配置 tor pre
一 List映射
如果持久化类中具有List对象,可以通过映射文件中的类元素或注解来映射List.
例如,一个问题有多个答案:
1)创建持久化类
package list; import java.util.List; public class Question { private int id; private String qname; private Listanswers; 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 getAnswers() { return answers; } public void setAnswers(List answers) { this.answers = answers; } }
2)创建映射文件
3)在全局配置文件中增加
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(); ArrayListlist1 = new ArrayList (); 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 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 getAnswers() { return answers; } public void setAnswers(List 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)配置文件
3)在hibernate.cfg.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 list1 = new ArrayList(); 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之集合映射
标签:分享 ns2 png list conf pac 全局配置 tor pre
原文地址:http://www.cnblogs.com/pebbles/p/7076380.html
评论
亲,登录后才可以留言!