本文共 4946 字,大约阅读时间需要 16 分钟。
package com.mycompany.demo.bean;import java.util.Set;public class Forum { private int fid; private String name; private SetforumPosts; public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getForumPosts() { return forumPosts; } public void setForumPosts(Set forumPosts) { this.forumPosts = forumPosts; }}
This class contains the forum detail.
package com.mycompany.demo.bean;public class ForumPost { private int pid; private String subject; private Forum forum; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Forum getForum() { return forum; } public void setForum(Forum forum) { this.forum = forum; }}
This class contains the forumpost detail.
package com.mycompany.demo.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HbnUtil { private static SessionFactory sessionFactory; public static Session getSession(){ if(sessionFactory == null || sessionFactory.isClosed()){ sessionFactory = new Configuration().configure().buildSessionFactory(); } return sessionFactory.getCurrentSession(); }}
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test root update true true thread org.hibernate.c3p0.internal.C3P0ConnectionProvider
package com.mycompany.demo.bean;import java.util.HashSet;import java.util.List;import java.util.Set;import org.hibernate.Session;import org.junit.Before;import org.junit.Test;import com.mycompany.demo.util.HbnUtil;public class TestApp { private Session session; @Before public void init(){ session = HbnUtil.getSession(); } /* * 一对多双向关联-添加 */ @Test public void testOneToManyAdd(){ try { session.beginTransaction(); ForumPost forumPost1 = new ForumPost(); forumPost1.setSubject("A"); ForumPost forumPost2 = new ForumPost(); forumPost2.setSubject("B"); SetforumPosts = new HashSet (); forumPosts.add(forumPost1); forumPosts.add(forumPost2); Forum forum = new Forum(); forum.setName("foruma"); forum.setForumPosts(forumPosts); session.save(forum); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 一对多双向关联-迫切左外连接-HQL */ @Test public void testLeft(){ try { session.beginTransaction(); String hsql = "FROM Forum f LEFT OUTER JOIN FETCH f.forumPosts"; List list = session.createQuery(hsql).list(); for (Forum forum : list) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 一对多双向关联-迫切左外连接-去重复-HQL */ @Test public void testLeftFetch(){ try { session.beginTransaction(); String hsql = "SELECT distinct f FROM Forum f LEFT OUTER JOIN FETCH f.forumPosts"; List list = session.createQuery(hsql).list(); for (Forum forum : list) { System.out.println(forum.getName()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } }}
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1912090
转载地址:http://vivwo.baihongyu.com/