1.创建项目,项目名称hibernatedemo27,目录结构如图所示
2.在项目中创建lib目录存储jar文件,目录结构如图所示
3.在src目录中创建实体类Forum,包名(com.mycompany.demo.bean),如图所示
4.实体类Forum的内容如下
package com.mycompany.demo.bean;import java.util.Set;public class Forum { private int fid; private String name; private Set forumPosts; 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; }}
5.在src目录中创建实体类Forum的映射文件Forum.hbm.xml,包名(com.mycompany.demo.bean),如图所示
6.映射文件Forum.hbm.xml的内容如下
This class contains the forum detail.
7.在src目录中创建实体类ForumPost,包名(com.mycompany.demo.bean),如图所示
8.实体类ForumPost的内容如下
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; }}
9.在src目录中创建实体类ForumPost的映射文件ForumPost.hbm.xml,包名(com.mycompany.demo.bean),如图所示
10.映射文件ForumPost.hbm.xml的内容如下
This class contains the forumpost detail. from Forum f left outer join fetch f.forumPosts
11.在src目录中创建工具类 HbnUtil,包名(com.mycompany.demo.util),如图所示
12.工具类 HbnUtil的内容如下
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(); }}
13.在src目录中创建Hibernate的配置文件hibernate.cfg.xml,如图所示
14.Hibernate的配置文件hibernate.cfg.xml的内容如下
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test root update true true thread org.hibernate.c3p0.internal.C3P0ConnectionProvider
15.在项目中创建test目录存储测试文件,文件名称TestApp,包名(com.mycompany.demo.bean),目录结构如图所示
16.TestApp测试类的内容如下
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"); Set forumPosts = 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="select",lazy="false" */ @Test public void testFetchForSelect(){ try { session.beginTransaction(); ForumPost forumPost = session.get(ForumPost.class, 10); Forum forum = forumPost.getForum(); System.out.println(forum.getFid()); System.out.println(forum.getName()); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } }}