Hibernate 关联映射

实体之间的关系
  关联,聚合,依赖,组合,继承

关系的类型:
  一对多
  多对一
  一对一
  多对多


 

  学生表和年级表:

 1 --学生表

 2 create table student(

 3        stuno number(4) primary key,

 4        name varchar2(20) not null,

 5        gradeid number(4)

 6 

 7 )

 8 --年级表

 9 create table grade(

10        gradeid number(4) primary key,

11        gradename varchar2(20) not null

12 

13 )

 

当一对多和多对一关系组合就是双向关联了

  所以整合在一起写了个添加数据和删除外键表的 级联代码

  1.建立对象

    student:    

 1 package entity;

 2 

 3 import java.io.Serializable;

 4 /**

 5  * 学生实体类

 6  * @author Administrator

 7  *

 8  */

 9 public class Student implements Serializable{

10     private Integer stuno;

11     private String name;

12     private Integer gradeid;

13     //学生与年级属于多多对一的关系

14     private Grade grade;

15     

16     public Student(){}

17     public Integer getStuno() {

18         return stuno;

19     }

20     public void setStuno(Integer stuno) {

21         this.stuno = stuno;

22     }

23     public String getName() {

24         return name;

25     }

26     public void setName(String name) {

27         this.name = name;

28     }

29     public Integer getGradeid() {

30         return gradeid;

31     }

32     public void setGradeid(Integer gradeid) {

33         this.gradeid = gradeid;

34     }

35     public Grade getGrade() {

36         return grade;

37     }

38     public void setGrade(Grade grade) {

39         this.grade = grade;

40     }

41     

42     

43 }
Student

    grade:

 1 package entity;

 2 

 3 import java.io.Serializable;

 4 import java.util.Set;

 5 /**

 6  * 年级实体类

 7  * @author Administrator

 8  *

 9  */

10 public class Grade implements Serializable{

11     //年级编号

12     private Integer gradeid;

13     //年级

14     private String gradename;

15     

16     private Set<Student> stu;

17     

18     public Grade(){}

19     

20     public Set<Student> getStu() {

21         return stu;

22     }

23 

24     public void setStu(Set<Student> stu) {

25         this.stu = stu;

26     }

27 

28     public Integer getGradeid() {

29         return gradeid;

30     }

31     public void setGradeid(Integer gradeid) {

32         this.gradeid = gradeid;

33     }

34     public String getGradename() {

35         return gradename;

36     }

37     public void setGradename(String gradename) {

38         this.gradename = gradename;

39     }

40     

41     

42     

43 }
grade

 

  2.建立对象之间的关系

 1 <!--grade表-->

 2 <?xml version="1.0"?>

 3 <!DOCTYPE hibernate-mapping PUBLIC 

 4     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 5     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 6 

 7 <hibernate-mapping

 8     package="entity">

 9     

10     <class name="Grade" table="grade" dynamic-update="true">

11         <id name="gradeid" type="integer">

12             <generator class="assigned">

13                 

14             </generator>

15         </id>

16         <property name="gradename" type="java.lang.String" />

17         <set name="stu" cascade="all">

18             <key column="gradeid"/>

19             <one-to-many class="Student"/>

20         </set>

21         

22     </class>

23 </hibernate-mapping>

24 

25 

26 <!--student表-->

27 <?xml version="1.0"?>

28 <!DOCTYPE hibernate-mapping PUBLIC 

29     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

30     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

31 

32 <hibernate-mapping

33     package="entity">

34     

35     <class name="Student" table="student" dynamic-update="true">

36         <id name="stuno" type="integer">

37             <generator class="sequence">

38                 <param name="sequence">seq_student</param>

39             </generator>

40         </id>

41         <property name="name" type="java.lang.String" />

42         

43         <!-- 多对一关系 -->

44         <many-to-one name="grade" class="Grade">

45             <column name="gradeid"></column>

46         </many-to-one>

47     </class>

48 </hibernate-mapping>
两表的配置文件

  3.配置hibernate

 1 <!DOCTYPE hibernate-configuration PUBLIC

 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 3     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 4 

 5 <hibernate-configuration>

 6     <session-factory>

 7         

 8         <!-- 指定oracle对应得dialect -->

 9         <property name="dialect">

10             org.hibernate.dialect.Oracle10gDialect

11         </property>

12         <!-- 数据库jdbc驱动 -->

13         <property name="connection.driver_class">

14             oracle.jdbc.driver.OracleDriver

15         </property>

16         

17         <!-- 数据库url -->

18         <property name="connection.url">

19             jdbc:oracle:thin:@localhost:1521:XE

20         </property>

21         

22         <!-- 用户名 -->

23         <property name="connection.username">super_user</property>

24         <!-- 用户密码 -->

25         <property name="connection.password">abc123</property>

26         

27         <!-- session范围和上下文 -->

28         <property name="current_session_context_class">thread</property>

29         <!-- 是否在运行期间生成的SQL输出到日志以供调试 -->

30         <property name="show_sql">true</property>

31         <!-- 是否格式化sql -->

32         <property name="format_sql">true</property>

33         <!-- 根据映射文件自动生成表-->

34         <property name="hbm2ddl.auto">update</property> 

35         <!-- 映射 -->

36         <mapping resource="entity/Grade.hbm.xml" />

37         <mapping resource="entity/Student.hbm.xml" />

38         

39     </session-factory>

40 </hibernate-configuration>

  上面配置文件中出现了三个新的配置节点,和新的节点属性:

   1.<set name="stu" cascade="all">   --name指定对象中属性名   cascade:级联 ————》all/none/save-update/delete/都是字面意思

    <key column="gradeid"/>    --指定两个对象关联的主键
    <one-to-many class="Student"/>  --这里是个一对多关系的实体类配置 ,因为我在mapping中配置了package所以这里只写类名
   </set>

   2.<many-to-one name="grade" class="Grade"> --指定对象中处于“一”关系的属性名,并且指明类名

    <column name="gradeid"></column>    --制定关联的主键
    </many-to-one>

 

   3.<property name="hbm2ddl.auto">update</property>   --指定操作类型

      #hibernate.hbm2ddl.auto create-drop  
      #hibernate.hbm2ddl.auto create
      #hibernate.hbm2ddl.auto update
      #hibernate.hbm2ddl.auto validate

  

  测试代码:

    

 1 package dao;

 2 

 3 import org.hibernate.Session;

 4 import org.hibernate.SessionFactory;

 5 import org.hibernate.Transaction;

 6 import org.hibernate.cfg.Configuration;

 7 

 8 import entity.Grade;

 9 import entity.Student;

10 

11 /**

12  * 多对一关系联动练习

13  * @author Administrator

14  *

15  */

16 public class StudentDao {

17     //打开配置

18     SessionFactory factory=new Configuration().configure().buildSessionFactory();

19     Session session=null;

20     

21     public static void main(String[] args) {

22         StudentDao stu=new StudentDao();

23         try {

24             //stu.update();

25             stu.del();

26         } catch (Exception e) {

27             // TODO Auto-generated catch block

28             e.printStackTrace();

29             

30         }

31     }

32     /**

33      * 新增

34      */

35     public void update() throws Exception{

36         session=factory.openSession();

37         session.beginTransaction();

38         //Grade grade=(Grade)session.get(Grade.class, 1);

39         

40         Student student=new Student();

41         Grade grade=new Grade();

42         grade.setGradeid(1);

43         grade.setGradename("一年级");

44         session.save(grade);

45         student.setName("小明");

46         student.setGrade(grade);

47         session.save(student);

48         session.getTransaction().commit();

49         System.out.println("成功!");

50         

51         session.close();

52     }

53     /**

54      * 删除

55      */

56     public void del(){

57         session=factory.openSession();

58         session.beginTransaction();

59         //Student student=(Student)session.get(Student.class, 5);

60         //级联删除

61         Grade grade=(Grade)session.get(Grade.class, 1);

62         session.delete(grade);

63         //session.delete(student);

64         session.getTransaction().commit();

65         System.out.println("成功!");

66         

67         session.close();

68     }

69 }


 

 

   在代码中遇到的问题http://www.cnblogs.com/gcs1995/p/4139422.html

 

你可能感兴趣的:(Hibernate)