首页 > 代码库 > Hibernate5-单向关联-多对多(n:n)
Hibernate5-单向关联-多对多(n:n)
1.创建项目,项目名称hibernatedemo14,目录结构如图所示
2.在项目中创建lib目录存储jar文件,目录结构如图所示
3.在src目录中创建实体类Course,包名(com.mycompany.demo.bean),如图所示
4.实体类Course的内容如下
package com.mycompany.demo.bean; public class Course { private int cid; private String name; public Course() { super(); } public Course(String name) { super(); this.name = name; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
5.在src目录中创建实体类Course的映射文件Course.hbm.xml,包名(com.mycompany.demo.bean),如图所示
6.映射文件Course.hbm.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- package:指定<class/>所在的包 --> <hibernate-mapping package="com.mycompany.demo.bean"> <!-- name:类名 table:表名 catalog:数据库名称,默认为hibernate.cfg.xml中配置的数据库名称 --> <class name="Course" table="course"> <meta attribute="class-description"> This class contains the course detail. </meta> <!-- name:属性名 colum:列名 --> <id name="cid" type="int" column="cid"> <!-- increment:hibernate维护主键值 identity:数据库自增长 sequence:序列 native:根据不同的数据库选择生成策略 uuid:通过UUID算法生成,实际使用较多 assigned:手工设置 --> <generator class="native"/> </id> <!-- length:字节长度 type:字段类型,支持java和hibernate类型 not-null:非空约束 unique:唯一性约束 --> <property name="name" column="name" /> </class> </hibernate-mapping>
7.在src目录中创建实体类Student,包名(com.mycompany.demo.bean),如图所示
8.实体类Student的内容如下
package com.mycompany.demo.bean; import java.util.Set; public class Student { private int sid; private String name; private Set<Course> courses; public Student() { super(); } public Student(String name) { super(); this.name = name; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } }
9.在src目录中创建实体类Student的映射文件Student.hbm.xml,包名(com.mycompany.demo.bean),如图所示
10.映射文件Student.hbm.xml的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- package:指定<class/>所在的包 --> <hibernate-mapping package="com.mycompany.demo.bean"> <!-- name:类名 table:表名 catalog:数据库名称,默认为hibernate.cfg.xml中配置的数据库名称 --> <class name="Student" table="student"> <meta attribute="class-description"> This class contains the student detail. </meta> <!-- name:属性名 colum:列名 --> <id name="sid" type="int" column="sid"> <!-- increment:hibernate维护主键值 identity:数据库自增长 sequence:序列 native:根据不同的数据库选择生成策略 uuid:通过UUID算法生成,实际使用较多 assigned:手工设置 --> <generator class="native"/> </id> <!-- length:字节长度 type:字段类型,支持java和hibernate类型 not-null:非空约束 unique:唯一性约束 --> <property name="name" column="name" type="string" length="50" not-null="true" unique="false"/> <set name="courses" table="student_course" cascade="save-update"> <key column="sid"></key> <many-to-many class="Course" column="cid"></many-to-many> </set> </class> </hibernate-mapping>
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的内容如下
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 方言,可以从Hibernate核心jar(hibernate-core-x.x.x.Finall.jar) 文件中的or.hibernate.dialect包中找到相应的类,类的全名就是 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库连接地址 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <!-- 用户名 --> <property name="hibernate.connection.username"> root </property> <!-- 密码 --> <property name="hibernate.connection.password"></property> <!-- create:每次都新创建,如果存在就删除 create-drop:创建新表,sessionFactory关闭,表会删除 update :表字段增加,会同步,字段减少不同步,数据改变会同步修改 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 输出sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 事务环境一个线程对一个事务 thread:本地事务环境 jta:分布式事务环境 SpringSessionContext:用于ssh整合 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 使用c3p0数据源 --> <property name="hibernate.connection.provider_class"> org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!-- List of XML mapping files --> <mapping resource="com/mycompany/demo/bean/Course.hbm.xml"/> <mapping resource="com/mycompany/demo/bean/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
15.在项目中创建test目录存储测试文件,文件名称TestApp,包名(com.mycompany.demo.bean),目录结构如图所示
16.TestApp测试类的内容如下
package com.mycompany.demo.bean; import java.util.HashSet; 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(); } /* * 单向关联-多对多(n:n) */ @Test public void testManyToMany(){ try { session.beginTransaction(); Course course1 = new Course("git"); Course course2 = new Course("gradle"); Set<Course> courses = new HashSet<Course>(); courses.add(course1); courses.add(course2); Student student = new Student("xiaoming"); student.setCourses(courses); session.save(student); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } }
本文出自 “素颜” 博客,谢绝转载!
Hibernate5-单向关联-多对多(n:n)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。