本文共 4647 字,大约阅读时间需要 15 分钟。
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. from Forum f left outer join fetch f.forumPosts
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.Criteria;import org.hibernate.Session;import org.hibernate.sql.JoinType;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(); } /* * 一对多双向关联-添加 * 需要设置Forum.hbm.xml中的Set属性为cascade="all" */ @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(); } } /* * 一对多双向关联-fetch="join" */ @Test public void testFetchForJoin(){ try { session.beginTransaction(); Forum forum = session.get(Forum.class, 10); Set forumPosts = forum.getForumPosts(); for (ForumPost forumPost : forumPosts) { System.out.println(forumPost.getSubject()); } session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } }}
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1912318
转载地址:http://peupo.baihongyu.com/