A:添加jar包:java项目下建立lib文件夹->把jar包拷贝进去(大概9个)->选中jar包右击->build path->add to path
B:从Hibernate解压缩包中随便拷贝一个hibernate.cfg.xml文件到src目录->修改相应配置即可
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">123property>
<property name="hibernate.connection.url">
property>
<property name="show_sql">trueproperty>
<property name="hbm2ddl.auto">updateproperty>
<mapping resource="com/imooc/entity/Grade.hbm.xml"/>
<mapping resource="com/imooc/entity/Student.hbm.xml"/>
session-factory>
hibernate-configuration>
Grade .java
//持久化类
public class Grade implements Serializable {
private int gid;
private String gname;
private String gdesc;
// 在一对多的一方定义一个多方的集合
private Set students = new HashSet();
public Grade(String gname, String gdesc) {
super();
this.gname = gname;
this.gdesc = gdesc;
}
public Grade(int gid, String gname, String gdesc, Set students) {
super();
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
this.students = students;
}
public Grade() {
super();
}
...
Student.java
//持久化类
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private int sid;
private String sname;
private String sex;
// 单向多对一配置(两个步骤)
// 1.在多方定义一个一方的引用
private Grade grade;
//2.修改多方映射文件的配置
//
public Student(String sname, String sex) {
super();
this.sname = sname;
this.sex = sex;
}
Grade.hbm.xml
<hibernate-mapping>
<class name="com.imooc.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.imooc.entity.Student"/>
set>
class>
hibernate-mapping>
<hibernate-mapping>
<class name="com.imooc.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>
HibernateUtil.java
//session工具类
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session session;
//用静态代码快来进行初始化
static{
//创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化
Configuration config = new Configuration().configure();
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(config.getProperties());
StandardServiceRegistry ssr = ssrb.build();
sessionFactory = config.buildSessionFactory(ssr);
}
//获取SessionFactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
//获取Session
public static Session getSession() {
session = sessionFactory.openSession();
return session;
}
//关闭Session
public static void closeSession(Session session) {
if (session!=null) {
session.close();
}
}
}
Test.java
/*
* 单向多对一关系(班级-->学生)
* 建立关联关系后,可以方便的从一个对象导航到另一个对象
* 注意关联的方向(目前不能通过学生找到学生所在的班级)
*/
public class Test {
public static void main(String[] args) {
//add();
findStudentByGrade();
// update();
//delete();
}
// 将学生添加到班级
public static void add() {
Grade g = new Grade("java一班", "java软件开发一班");
Student stu1 = new Student("张三", "男");
Student stu2 = new Student("穆女神", "女");
// 如果希望学生在学生表中添加对应的班级编号,需要在班级中添加学生,建立关联关系
g.getStudents().add(stu1);
g.getStudents().add(stu2);
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tx.commit();
HibernateUtil.closeSession(session);
}
// 查询班级中包含的学生(可以找到班级表中没有包含的学生详细信息)
public static void findStudentByGrade() {
Session session = HibernateUtil.getSession();
Grade grade = (Grade) session.get(Grade.class, 1);
System.out.println(grade.getGname() + "," + grade.getGdesc());
Set students = grade.getStudents();
for (Student student : students) {
System.out.println(student.getSname() + "," + student.getSex());
}
}
// 修改学生信息(将学生1的班级修改为2班)
public static void update() {
Grade g = new Grade("java二班", "java软件开发二班");
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Student stu = (Student) session.get(Student.class, 1);
g.getStudents().add(stu);
session.save(g);
tx.commit();
HibernateUtil.closeSession(session);
}
// 删除学生信息
public static void delete() {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Student stu = (Student) session.get(Student.class, 2);
session.delete(stu);
tx.commit();
HibernateUtil.closeSession(session);
}
}
// 测试保存方法
public static void save() {
Grade g = new Grade("java一班", "java软件开发一班");
Student stu1 = new Student("穆女神", "女");
Student stu2 = new Student("小穆穆", "男");
// 设置关联关系
// 班级到学生(一对多)向班级添加学生
g.getStudents().add(stu1);
g.getStudents().add(stu2);
// 学生到班级(多对一)为学生添加班级
stu1.setGrade(g);
stu2.setGrade(g);
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(g);// 级联操作
// session.save(stu1);
// session.save(stu2);
tx.commit();
HibernateUtil.closeSession(session);
}
测试查询
// 查询学生所在的班级信息(这些信息并没有在学生表中保存)
public static void findGradeByStudent() {
Session session = HibernateUtil.getSession();
Student stu = (Student) session.get(Student.class, 2);
System.out.println(stu.getSid() + "," + stu.getSname() + ","
+ stu.getSex());
//得到班级,就可以查询班级的信息了
Grade g = stu.getGrade();
System.out
.println(g.getGid() + "," + g.getGname() + "," + g.getGdesc());
HibernateUtil.closeSession(session);
}
在进行双向多对一测试时,可以发现执行的sql语句为:
Hibernate: select max(gid) from grade
Hibernate: select max(sid) from student
Hibernate: insert into grade (gname, gdesc, gid) values (?, ?, ?)
Hibernate: insert into student (sname, sex, gid, sid) values (?, ?, ?, ?)
Hibernate: insert into student (sname, sex, gid, sid) values (?, ?, ?, ?)
Hibernate: update student set gid=? where sid=?
Hibernate: update student set gid=? where sid=?
后面的两条update语句是是一方在维护一对多的关系(更新学生所在的班级),而多方(Student)知道自己所属的班级,所以这两条sql语句是多余的。可以在Grade.hbm.xml的set属性中设置inverse值为true表示由多方维护关联关系。如此,后面两条sql语句便不会再执行。
Grade.hbm.xml
<set name="students" table="student" inverse="true">
<key column="gid">key>
<one-to-many class="com.imooc.entity.Student"/>
set>
在进行双向多对一测试时,我们既保存了班级信息,又保存了学生信息。班级是知道自己班有哪些学生的,所以当我们保存班级时,正常来说应该自动保存班级的学生信息才对。所以需要设置级联属性。
<set name="students" table="student" inverse="true" cascade="save-update">
这样,测试方法就只需保存班级就可以了
session.save(g);
// 级联操作(无需再次显示的保存学生信息)
//session.save(stu1);
//session.save(stu2);
MyEclipse反向工程:根据数据库中的表自动生成实体化类和映射文件,并且可以选择一对多和多对一关联关系;
进入数据库视图,选中要配置的表->右键选中reverse engineering->配置相关选项即可