程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Hibernate 關聯映射,hibernate關聯映射

Hibernate 關聯映射,hibernate關聯映射

編輯:Oracle教程

Hibernate 關聯映射,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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved