1. 一对多关联
(1). 创建工程与数据库
打开MyEclipse,创建工程导入所需要的jar包,配置hibernate.cfg.xml文件:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123465</property> <property name="hibernate.connection.url"> <![CDATA[ jdbc:mysql://localhost:3306/one2many:useUnicode=true&characterEncoding=utf8 ]]> </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
创建MySQL中数据库one2many。
(2). 创建工具类:
public class HibernateUtil { private static SessionFactory sessionFactory; private static Session session; static { // 创建configuration对象,读取hibernate.cfg.xml配置 Configuration config = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()); StandardServiceRegistry registry = builder.build(); sessionFactory = config.buildSessionFactory(registry); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getSession() { session = sessionFactory.openSession(); return session; } public static void closeSession(Session session) { if (session != null) { session.close(); } } }
(3). 创建表
create table grade( gid int primary key, gname varchar(20) not null, gdesc varchar(50) ); create table student( sid int primary key, sname varchar(20) not null, sex char(2), gid int ); alter table student add constraint fk_student_gid foreign key (gid) references grade(gid);
(4). 创建Student和Grade类以及他们的关联映射文件
<hibernate-mapping> <class name="com.thr.entity.Student" table="student"> <id name="sid" column="sid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="sname" type="java.lang.String"> <column name="sname" length="20" not-null="true"></column> </property> <property name="sex"> <column name="sex"></column> </property> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.thr.entity.Grade" table="grade"> <id name="gid" column="gid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="gname" type="java.lang.String"> <column name="gname" length="20" not-null="true"></column> </property> <property name="gdesc"> <column name="gdesc"></column> </property> <!-- 配置单向一对多的关联关系 --> <set name="students" table="student"> <!-- 指定关联的外键列 --> <key column="gid"></key> <one-to-many class="com.thr.entity.Student" /> </set> </class> </hibernate-mapping>
然后在hibernate.cfg.xml配置文件中添加这两个文件的路径:
<!-- 指定关联映射文件的路径 --> <mapping resource="com/thr/entity/Grade.hbm.xml"/> <mapping resource="com/thr/entity/Student.hbm.xml"/>
(5). 编写测试类
测试添加和查询:
/** * 学生添加到班级 */ @Test public void add() { Grade g = new Grade("三年二班", "哎呦不错哦"); Student s1 = new Student("张三", "男"); Student s2 = new Student("李四", "女"); g.getStudents().add(s1); g.getStudents().add(s2); Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); session.save(g); session.save(s1); session.save(s2); transaction.commit(); HibernateUtil.closeSession(session); } /** * 查询班级中包含的学生 */ @Test public void findStudentByGrade() { Session session = HibernateUtil.getSession(); Grade g = (Grade) session.get(Grade.class, 1); System.out.println(g.toString()); Set<Student> students = g.getStudents(); for (Student s : students) { System.out.println(s); } }
修改和删除测试:
/** * 修改学生信息 */ @Test public void update() { Grade g = new Grade("三年三班", "还行吧"); Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Student s = (Student) session.get(Student.class, 1); g.getStudents().add(s); session.save(g); transaction.commit(); HibernateUtil.closeSession(session); } /** * 删除学生信息 */ @Test public void delete() { Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Student s = (Student) session.get(Student.class, 1); session.delete(s); transaction.commit(); HibernateUtil.closeSession(session); }
(6). set元素的常用属性
2. 多对一关联
给原来的Student类添加一个Grade成员,设置set和get方法,修改Student.hbm.xml配置,添加:
<!-- 配置多对一关联配置 --> <many-to-one name="grade" class="com.thr.entity.Grade" column="gid"> </many-to-one>
编写测试类:
@Test public void save() { Grade g = new Grade("四年三班", "四年三班"); Student s1 = new Student("Jerry", "男"); Student s2 = new Student("kate", "女"); // 设置关联关系 s1.setGrade(g); s2.setGrade(g); Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); session.save(g); session.save(s1); session.save(s2); transaction.commit(); HibernateUtil.closeSession(session); }
3. inverse和cascade属性
(1). inverse属性
<set>节点的inverse属性指定关联关系的控制方向,默认由one方来维护。
关联关系中,inverse="false"则为主动方,由主动方负责维护关联关系。
在一对多关联中,只能设置one方的inverse为true,这将有助于性能的改善。
(2). cascade属性
当设置了cascade属性不为none时,Hibernate会自动持久化所关联的对象
cascade属性的设置会带来性能上的变动,需谨慎设置
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-21 17:56:14