一、Hibernate的執行流程
hibernate作為一個ORM框架,它封裝了大量數據庫底層的sql語句操作的方法,這樣在執行hibernate的過程中理解hibernate的執行流程很有必要。
由上圖我們可以很清楚的發現,想要獲得一個sessionFactory對象,需要進行很多步驟,需要讀取和加載文件信息,內存消耗很大,所以一般一個數據庫只會生成一個sessionFactory對象。
代碼展示hibernate:
1 import java.util.Date; 2 import java.util.List; 3 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.Transaction; 8 import org.hibernate.cfg.Configuration; 9 import org.hibernate.service.ServiceRegistry; 10 import org.hibernate.service.ServiceRegistryBuilder; 11 import org.junit.After; 12 import org.junit.Before; 13 import org.junit.Test; 14 15 import Entity.Students; 16 17 //測試類 18 public class StudentsTest { 19 private SessionFactory sessionFactory; 20 private Session session; 21 private Transaction transaction; 22 @Before 23 public void init() 24 { 25 //匹配hibernate.cfg.xml文件獲得配置對象 26 Configuration config=new Configuration().configure(); 27 //獲得服務注冊對象(該對象中封裝了hibernate.cfg.xml文檔的<properties>、<mapping>標簽信息) 28 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 29 //獲得sessionFactory對象(將ServiceRegistry中的相關信息,封裝到sessionFactory中) 30 sessionFactory=config.buildSessionFactory(serviceRegistry); 31 //獲得session對象 32 session=sessionFactory.openSession(); 33 //開啟事務 34 transaction=session.beginTransaction(); 35 36 } 37 38 39 @Test 40 public void testSaveStudents() 41 { 42 Students stu=new Students(5,"小宏","女",new Date(),"華山"); 43 session.save(stu); 44 } 45 46 @After 47 public void Destory() 48 { 49 //提交事務 50 transaction.commit(); 51 //關閉會話 52 session.close(); 53 //關閉會話工廠 54 sessionFactory.close(); 55 } 56 57 }
二、hibernate中映射問題--以配置文件方式的介紹(特別是外鍵約束)
1、一對多的關系映射
代碼示例:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!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.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">tiger</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="com/third/Dao1/Grader1.hbm.xml"/> <mapping resource="com/third/Dao1/Students1.hbm.xml"/> </session-factory> </hibernate-configuration>
Students1.java
package com.third.Dao1; import java.io.Serializable; public class Students1 implements Serializable { private int sid; private String sname; private String sgender; public Students1() { } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSgender() { return sgender; } public void setSgender(String sgender) { this.sgender = sgender; } }
Grader1.java
package com.third.Dao1; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Grader1 implements Serializable { private int gid; private String gname; private Set<Students1> stuSet=new HashSet<Students1>(); public Grader1() { } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public Set<Students1> getStuSet() { return stuSet; } public void setStuSet(Set<Students1> stuSet) { this.stuSet = stuSet; } }
由eclipse幫助生成的:
Students1.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-1 20:42:49 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao1.Students1" table="STUDENTS1"> <id name="sid" type="int"> <column name="SID" /> <generator class="native" /> </id> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <property name="sgender" type="java.lang.String"> <column name="SGENDER" /> </property> </class> </hibernate-mapping>
Grager1.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-1 20:42:49 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao1.Grader1" table="GRADER1"> <id name="gid" type="int"> <column name="GID" /> <generator class="increment" /> </id> <property name="gname" type="java.lang.String"> <column name="GNAME" /> </property> <set name="stuSet" inverse="false" table="STUDENTS1" lazy="true"> <key> <column name="GID" /> </key> <one-to-many class="com.third.Dao1.Students1" /> </set> </class> </hibernate-mapping>
Test1.java
package com.third; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.third.Dao1.Grader1; import com.third.Dao1.Students1; /*import com.third.Dao.Grader; import com.third.Dao.Students;*/ public class Test1 { private static SessionFactory sessionFactory; private static Session session; private static Transaction transaction; @Before public void init(){ //創建配置對象,匹配讀取hibernate.cfg.xml文件 Configuration config=new Configuration().configure(); //獲取服務注冊對象(該對象中封裝了hibernate.cfg.xml文件中的<properties>、<mapping>信息) ServiceRegistry serviceRegistry=new ServiceRegistryBuilder() .applySettings(config.getProperties()).buildServiceRegistry(); //獲取sessionFactory對象(該對象中通過傳參serviceRegistry對象,獲取了<mapping><properties>信息) sessionFactory=config.buildSessionFactory(serviceRegistry); } @Test public void test2(){ //通過sessionFactory對象獲取session對象 session=sessionFactory.openSession(); //通過session對象開啟事務,並返回Transaction對象 transaction=session.beginTransaction(); //創建students和Grader對象 Students1 student2=new Students1(); student2.setSname("小美"); student2.setSgender("女"); Students1 student3=new Students1(); student3.setSname("小宏"); student3.setSgender("女"); Set<Students1> stuSet=new HashSet<Students1>(); stuSet.add(student2); stuSet.add(student3); //實例Grader對象 Grader1 grader2=new Grader1(); grader2.setGname("信息與計算科學二班"); grader2.setStuSet(stuSet);; session.save(student2); session.save(student3); session.save(grader2); } @After public void destory(){ //提交事務 transaction.commit(); //關閉資源 if(session!=null){ session.close(); } if(sessionFactory!=null){ sessionFactory.close(); } } }
運行結果數據庫表格記錄和表格樣式截圖:
表格樣式:
表格記錄:
我們不難看出,對於一對多的映射,其核心的代碼是背景為綠色的代碼段,即在一方的bean中添加多方的引用的集合,並且在其一方的bean的映射文件hbm.xml中添加<one-to-many>標簽進行配置設置。hbm.xml文件中指出了多方的主鍵作為一方的一個外鍵,我們通過分析表格樣式,可以發現eclipse為一方生成了一個與多方主鍵關聯的外鍵。
2、多對一映射
代碼示例:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!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.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">tiger</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="com/third/Dao/Grader.hbm.xml"/> <mapping resource="com/third/Dao/Students.hbm.xml"/> </session-factory> </hibernate-configuration>
Grader.java
package com.third.Dao; import java.io.Serializable; public class Grader implements Serializable { private int gid; private String gname; public Grader() { } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } }
Students.java
package com.third.Dao; import java.io.Serializable; public class Students implements Serializable { private int sid; private String sname; private String sgender; private Grader grader; public Students() { } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSgender() { return sgender; } public void setSgender(String sgender) { this.sgender = sgender; } public Grader getGrader() { return grader; } public void setGrader(Grader grader) { this.grader = grader; } }
Grader.cfg.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-1 19:15:12 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao.Grader" table="GRADER"> <id name="gid" type="int"> <column name="GID" /> <generator class="increment" /> </id> <property name="gname" type="java.lang.String"> <column name="GNAME" /> </property> </class> </hibernate-mapping>
Syudents.cfg.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-1 19:15:12 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao.Students" table="STUDENTS"> <id name="sid" type="int"> <column name="SID" /> <generator class="increment" /> </id> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <property name="sgender" type="java.lang.String"> <column name="SGENDER" /> </property> <many-to-one name="grader" class="com.third.Dao.Grader" fetch="join"> <column name="GRADER" /> </many-to-one> </class> </hibernate-mapping>
Test1.java
1 package com.third; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 import org.hibernate.service.ServiceRegistry; 11 import org.hibernate.service.ServiceRegistryBuilder; 12 import org.junit.After; 13 import org.junit.Before; 14 import org.junit.Test; 15 16 /*import com.third.Dao1.Grader1; 17 import com.third.Dao1.Students1;*/ 18 19 import com.third.Dao.Grader; 20 import com.third.Dao.Students; 21 public class Test1 { 22 23 private static SessionFactory sessionFactory; 24 private static Session session; 25 private static Transaction transaction; 26 @Before 27 public void init(){ 28 //創建配置對象,匹配讀取hibernate.cfg.xml文件 29 Configuration config=new Configuration().configure(); 30 //獲取服務注冊對象(該對象中封裝了hibernate.cfg.xml文件中的<properties>、<mapping>信息) 31 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder() 32 .applySettings(config.getProperties()).buildServiceRegistry(); 33 //獲取sessionFactory對象(該對象中通過傳參serviceRegistry對象,獲取了<mapping><properties>信息) 34 sessionFactory=config.buildSessionFactory(serviceRegistry); 35 } 36 37 @Test 38 public void test1(){ 39 //通過sessionFactory對象獲取session對象 40 session=sessionFactory.openSession(); 41 //通過session對象開啟事務,並返回Transaction對象 42 transaction=session.beginTransaction(); 43 44 Grader grader1=new Grader(); 45 //grader1.setGid(1); 46 grader1.setGname("信息與計算科學一班"); 47 Students student1=new Students(); 48 student1.setSname("小明"); 49 student1.setSgender("男"); 50 student1.setGrader(grader1); 51 52 session.save(grader1); 53 session.save(student1); 54 } 86 @After 87 public void destory(){ 88 //提交事務 89 transaction.commit(); 90 //關閉資源 91 if(session!=null){ 92 session.close(); 93 } 94 if(sessionFactory!=null){ 95 sessionFactory.close(); 96 } 97 } 98 }
運行后數據庫的表格樣式和表格記錄截圖:
表格樣式:
表格記錄:
我們不難看出,對於多對一的映射,其核心的代碼是背景為綠色的代碼段,即在多方的bean中添加一方的引用,並且在其多方的bean的映射文件hbm.xml中添加<many-to-one>標簽進行配置設置。雖然多方hbm.xml文件中沒有指出外鍵,但是我們通過分析表格樣式,可以發現eclipse為一方生成了一個與多方主鍵關聯的外鍵。
3、多對多映射
代碼示例:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!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.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">tiger</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <!-- <mapping resource="com/third/Dao1/Grader1.hbm.xml"/> <mapping resource="com/third/Dao1/Students1.hbm.xml"/> --> <mapping resource="com/third/Dao2/Students2.hbm.xml"/> <mapping resource="com/third/Dao2/Teachers.hbm.xml"/> </session-factory> </hibernate-configuration>
Students2.java
package com.third.Dao2; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Students2 implements Serializable { private int sid; private String sname; private String sgender; private Set<Teachers> teaList=new HashSet<Teachers>(); public Students2() { } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSgender() { return sgender; } public void setSgender(String sgender) { this.sgender = sgender; } public Set<Teachers> getTeaList() { return teaList; } public void setTeaList(Set<Teachers> teaList) { this.teaList = teaList; } }
Teachers.java
package com.third.Dao2; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Teachers implements Serializable { private int tid; private String tname; private String tgender; private Set<Students2> stuList=new HashSet<Students2>(); public Teachers() { } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTgender() { return tgender; } public void setTgender(String tgender) { this.tgender = tgender; } public Set<Students2> getStuList() { return stuList; } public void setStuList(Set<Students2> stuList) { this.stuList = stuList; } }
又eclipse幫助創建的hbm.xml文件(這回這些hbm.xml文件需要進行改動不能進行直接使用)
Students2.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-2 14:57:36 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao2.Students2" table="STUDENTS2"> <id name="sid" type="int"> <column name="SID" /> <generator class="assigned" /> </id> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <property name="sgender" type="java.lang.String"> <column name="SGENDER" /> </property>
<set name="teaList" table="CLASSROOM" inverse="false" cascade="all" lazy="true"> <key> <column name="C_SID" /> </key> <many-to-many class="com.third.Dao2.Teachers" column="C_TID"/> </set> </class> </hibernate-mapping>
Teachers.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-2 14:57:36 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.third.Dao2.Teachers" table="TEACHERS"> <id name="tid" type="int"> <column name="TID" /> <generator class="assigned" /> </id> <property name="tname" type="java.lang.String"> <column name="TNAME" /> </property> <property name="tgender" type="java.lang.String"> <column name="TGENDER" /> </property> <set name="stuList" table="CLASSROOM" lazy="true"> <key> <column name="C_TID" /> </key> <many-to-many class="com.third.Dao2.Students2" column="C_SID"/> </set> </class> </hibernate-mapping>
Test2.java
1 package com.third; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 import org.hibernate.service.ServiceRegistry; 11 import org.hibernate.service.ServiceRegistryBuilder; 12 import org.junit.After; 13 import org.junit.Before; 14 import org.junit.Test; 15 16 import com.third.Dao2.Students2; 17 import com.third.Dao2.Teachers; 18 19 public class Test2 { 20 21 private static SessionFactory sessionFactory; 22 private static Session session; 23 private static Transaction transaction; 24 @Before 25 public void init(){ 26 //先獲取配置對象,匹配hibernate.cfg.xml文件 27 Configuration config=new Configuration().configure(); 28 //獲取注冊服務對象(該對象中包含hibernate.cfg.xml中的<properties>和<maping>信息 29 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 30 //獲取sessionFactory對象,通過sessionFactory對象讀取hibernate.cfg.xml文檔信息,並通過<mapping>標簽加載hbm.xml文件信息 31 sessionFactory=config.buildSessionFactory(serviceRegistry); 32 } 33 34 @Test 35 public void test2(){ 36 //通過sessionFactory對象獲取session對象 37 session=sessionFactory.openSession(); 38 //通過session對象開啟事務,並且返回事務(transaction)對象 39 transaction=session.beginTransaction(); 40 41 //實例Students2對象,完成基本賦值 42 Students2 stu1=new Students2(); 43 stu1.setSid(1); 44 stu1.setSname("小美"); 45 stu1.setSgender("女"); 46 47 Students2 stu2=new Students2(); 48 stu2.setSid(2); 49 stu2.setSname("小澤"); 50 stu2.setSgender("男"); 51 52 Students2 stu3=new Students2(); 53 stu3.setSid(3); 54 stu3.setSname("小敏"); 55 stu3.setSgender("女"); 56 57 //實例Teacher對象,完成基本賦值 58 Teachers tea1=new Teachers(); 59 tea1.setTid(1); 60 tea1.setTname("王老師"); 61 tea1.setTgender("男"); 62 63 Teachers tea2=new Teachers(); 64 tea2.setTid(2); 65 tea2.setTname("馬老師"); 66 tea2.setTgender("男"); 67 68 //我們通過Students2對象,進行Students2與Teachers的級聯關系儲存操作 69 //其中tea1和tea2都是stu1的老師 70 stu1.getTeaList().add(tea1); 71 stu1.getTeaList().add(tea2); 72 73 //其中tea1和tea2都是stu2的老師 74 Set<Teachers> teaList=new HashSet<Teachers>(); 75 teaList.add(tea1); 76 teaList.add(tea2); 77 78 //其中tea1是stu3的老師 79 stu3.getTeaList().add(tea1); 80 81 //保存這些對象(這里涉及到級聯,其中維護方為Students2) 82 session.save(stu1); 83 session.save(stu2); 84 session.save(stu3); 85 86 } 87 88 @After 89 public void destory(){ 90 transaction.commit(); 91 //關閉開啟的資源 92 if(session!=null){ 93 session.close(); 94 } 95 if(sessionFactory!=null){ 96 sessionFactory.close(); 97 } 98 } 99 }
運行后數據庫的表格樣式和表格記錄:
表中記錄:
我們不難發現綠色背景的代碼是多對多映射實現的核心代碼,即在兩個bean都需要在各自bean中添加對方的引用集合,但是他們的由eclipse生成的hbm.xml文件不能直接使用,不像一對多和多對一的生成的hbm.xml代碼不要做修改就可使用。因為eclipse幫忙生成的兩個hbm.xml文件中是生成<many-to-one>標簽的,如果不修改直接使用的話,將會在數據庫生成兩個表格,這兩個表格各自有一個外鍵,而且每個表的各自的外鍵分別與另一個表的主鍵關聯,理論上這種思路也行,也可以實現多對多的關系映射,但是這樣面臨的結果是記錄的插入和更新修改將會受到很多限制,很容易報錯,舉一個例子:如果我們想要直接刪掉這兩個表格,將會有外鍵的限制,不管先刪除誰都不好使,只能在解除外鍵約束后才能刪除。多對多映射的正確思路是兩個多方表格,通過第三個表格,這個表格擁有兩個外鍵分別與兩個多方表格的主鍵相對應,通過第三個表格來存儲多對多關系映射。這樣我們就需要修改eclipse幫我們生成的兩個hbm.xml文件,使用<many-to-many>標簽,並指出第三個表格的表格名。如下圖:
4、總結
(1)一對多映射,我們是通過在一方的bean中添加多方的引用集合,並在一方的hbm.xml文件中配置<one-to-many>標簽及相關信息。
(2)多對一映射,我們是通過在多方的bean中添加一方的引用,並在多方的hbm.xnl文件中配置<many-to-many>標簽及相關信息。
(3)多對多映射,我們是通過在兩個多方的bean中都添加對方的引用集合,並在兩個多方的hbm.xml文件中配置<many-to-many>標簽及相關信息。
(4)不管是一對多還是多對一的映射,由eclipse運行后都會在多方的表格中添加一個外鍵,而且這個外鍵與一方的主鍵關聯,區別在於:一對多映射多方的hbm.xml文件中會指明外鍵名,而多對一映射不會指出,但是查看生成的表格會看到多方的表格被添加了外鍵並且關聯與一方表格的主鍵。然而多對多映射,是在第三個表格設置兩個外鍵,這連個外鍵分別關聯着兩個多方的表格的主鍵,且這個第三方表格的主鍵是由這兩個外鍵聯合而成的復合主鍵。
(5)不管是多對一還是一對多映射,其通過eclipse幫助創建生成的hbm.xml文件可以直接使用,可以不需要改動。但是多對多映射時,其通過eclipse幫助創建說呢過成的hbm.xml文件需要進行一些修改,不能直接使用。
(6)既然涉及到映射關系,那么生成的表格會存在這級聯關系,我們可以通過inverse和cascade設置級聯的維護方和級聯操作的范圍,當表格的更新和添加等操作,在表格中顯示的信息不全時,我們可以考慮一下級聯。