前面已經講解了一對多和多對一的映射是怎麼配置了,也講解了inverse和cascade屬性對關聯關係的影響,本博文講解多對多的映射和一對一的映射!java
需求:一個項目由多個員工開發,一個員工開發多個項目數據庫
通常地,若是是多對多的映射,咱們都會使用一張中間表來保存它們的關聯關係….markdown
咱們在設計實體的時候,通常是核心數據表對應一個JavaBean實體【中間表並非核心數據表】,那麼咱們將會設計兩個JavaBean對象session
project.javaapp
package zhongfucheng.many2many; import java.util.HashSet; import java.util.Set; /** * Created by ozc on 2017/5/7. */ public class Project { private int projectId; private String projectName; //使用Set集合與developer實體維護關係 private Set<Developer> developers = new HashSet<>(); public int getProjectId() { return projectId; } public void setProjectId(int projectId) { this.projectId = projectId; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } public Set<Developer> getDevelopers() { return developers; } public void setDevelopers(Set<Developer> developers) { this.developers = developers; } }
developer.javadom
package zhongfucheng.many2many; import java.util.HashSet; import java.util.Set; /** * Created by ozc on 2017/5/7. */ public class Developer { private int developerId; private String developerName; //使用Set集合來維護與Project關係 private Set<Project> projects = new HashSet<>(); public int getDeveloperId() { return developerId; } public void setDeveloperId(int developerId) { this.developerId = developerId; } public String getDeveloperName() { return developerName; } public void setDeveloperName(String developerName) { this.developerName = developerName; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
以項目映射文件爲例:咱們不急着寫,首先來分析一下關鍵點……想要在多對多映射中產生正確的關聯關係,下面幾步必不可少:測試
Project和Developer的映射文件都須要這幾個關鍵步驟ui
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.many2many"> <class name="Project" table="Project"> <!--映射主鍵--> <id name="projectId" column="projectId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="projectName" column="projectName"></property> <!--映射多對多的關係--> <!--Set的屬性名稱爲developers,對應developer_project表--> <set name="developers" table="developer_project"> <!--對應developer_project表的外鍵列--> <key column="project_id"></key> <!--集合的類型和developer_project表的另外一個外鍵列--> <many-to-many column="developer_id" class="Developer"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.many2many"> <class name="Developer" table="Developer"> <!--映射主鍵--> <id name="developerId" column="developerId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="developerName" column="developerName"></property> <!--映射多對多的關係--> <!--Set的屬性名稱爲developers,對應developer_project表--> <set name="projects" table="developer_project"> <!--對應developer_project表的外鍵列--> <key column="developer_id"></key> <!--集合的類型和developer_project表的另外一個外鍵列--> <many-to-many column="project_id" class="Project"></many-to-many> </set> </class> </hibernate-mapping>
package zhongfucheng.many2many; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App2 { public static void main(String[] args) { /* * * * Project Developer 電商系統 曹吉 王春 OA系統 王春 老張 */ //建立對象 Developer cj = new Developer(); Developer wc = new Developer(); Developer lz = new Developer(); Project ds = new Project(); Project oa = new Project(); //設置對象的數據 cj.setDeveloperName("曹吉"); wc.setDeveloperName("王春"); lz.setDeveloperName("老張"); oa.setProjectName("OA系統"); ds.setProjectName("電商系統"); //使用Project來關聯數據【在多對多中,同樣的】 oa.getDevelopers().add(wc); oa.getDevelopers().add(lz); ds.getDevelopers().add(cj); ds.getDevelopers().add(wc); //獲取加載配置管理類 Configuration configuration = new Configuration(); //加載User的映射文件! configuration.configure().addClass(Developer.class).addClass(Project.class); //建立Session工廠對象 SessionFactory factory = configuration.buildSessionFactory(); //獲得Session對象 Session session = factory.openSession(); //使用Hibernate操做數據庫,都要開啓事務,獲得事務對象 Transaction transaction = session.getTransaction(); //開啓事務 transaction.begin(); //在Project映射文件中設置級聯保存了 session.save(oa); session.save(ds); //提交事務 transaction.commit(); //關閉Session session.close(); } }
執行了9條SQL語句,數據庫中的記錄也是正確的。this
需求:用戶與身份證信息..一個用戶對應一個身份證spa
對於數據庫表設計咱們有兩種方式
idCard.java
package zhongfucheng.one2one; /** * Created by ozc on 2017/5/7. */ public class IdCard { private int idCardId; private String idCardName; //維護與用戶之間的關係 private User user ; public int getIdCardId() { return idCardId; } public void setIdCardId(int idCardId) { this.idCardId = idCardId; } public String getIdCardName() { return idCardName; } public void setIdCardName(String idCardName) { this.idCardName = idCardName; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
User.java
package zhongfucheng.one2one; /** * Created by ozc on 2017/5/7. */ public class User { private int userId; private String userName; //維護與身份證一對一的關係 private IdCard idCard ; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
咱們有兩種方式來設計數據庫中的表實現一對一的關係,首先咱們來挑比較熟悉的外鍵方式來寫映射文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.one2one"> <class name="User" table="User"> <!--映射主鍵--> <id name="userId" column="userId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="userName" column="userName"></property> <!-- User是沒有外鍵字段的表 一對一的關係的屬性名稱name是idCard 類型是IdCard --> <one-to-one name="idCard" class="IdCard"></one-to-one> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.one2one"> <class name="IdCard" table="IdCard"> <!--映射主鍵--> <id name="idCardId" column="idCardId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="idCardName" column="idCardName"></property> <!--idCart是有外鍵的表,要把字段映射成外鍵,用的是manyToOne--> <!-- 外鍵的屬性name是user 對應表的字段是userId 屬性的類型是User 該字段須要惟一性 unique --> <many-to-one name="user" column="user_id" class="User" unique="true" cascade="save-update"></many-to-one> </class> </hibernate-mapping>
要使用IdCart來維護User的關聯關係。
package zhongfucheng.one2one; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App3 { public static void main(String[] args) { //建立對象 User user = new User(); IdCard idCard = new IdCard(); //設置對象的數據 user.setUserName("你好"); idCard.setIdCardName("身份證001"); //一對一關聯數據 idCard.setUser(user); //獲取加載配置管理類 Configuration configuration = new Configuration(); //加載User的映射文件! configuration.configure().addClass(User.class).addClass(IdCard.class); //建立Session工廠對象 SessionFactory factory = configuration.buildSessionFactory(); //獲得Session對象 Session session = factory.openSession(); //使用Hibernate操做數據庫,都要開啓事務,獲得事務對象 Transaction transaction = session.getTransaction(); //開啓事務 transaction.begin(); //保存對象的數據,idCard配置文件使用級聯保存 session.save(idCard); //提交事務 transaction.commit(); //關閉Session session.close(); } }
由於IdCart使用userId做爲了主鍵,所以須要在JavaBean中配置多一個屬性UserId…其餘的都不用變
private int userId; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; }
在Hibernate第三篇講解配置的時候,在generator節點下還有一個屬性沒有講解,也就是foreign屬性…如今來填坑了..
idCart的映射文件主要在於:將主鍵也映射成外鍵來使用,這就須要用到foreign屬性值了
使用<one-to-one>
標籤來配置基於主鍵的映射
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.one2one2.one2one"> <class name="IdCard" table="IdCard"> <!--映射主鍵--> <id name="userId" column="userId"> <!-- 作主鍵的同時也作外鍵 外鍵的類型名稱爲user --> <generator class="foreign"> <param name="property">user</param> </generator> </id> <!--映射普通字段--> <property name="idCardName" column="idCardName"></property> <property name="idCardId" column="idCartId"></property> <!-- 有外鍵的一方: 基於主鍵映射,使用oneToOne constrained="true" 指定在主鍵上添加外鍵約束 --> <one-to-one name="user" class="User" constrained="true"></one-to-one> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="zhongfucheng.one2one2.one2one"> <class name="User" table="User"> <!--映射主鍵--> <id name="userId" column="userId"> <generator class="native"></generator> </id> <!--映射普通字段--> <property name="userName" column="userName"></property> <!-- User是沒有外鍵字段的表 一對一的關係的屬性名稱name是idCard 類型是IdCard --> <one-to-one name="idCard" class="IdCard"></one-to-one> </class> </hibernate-mapping>
package zhongfucheng.one2one2.one2one; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App4 { public static void main(String[] args) { //建立對象 User user = new User(); IdCard idCard = new IdCard(); //設置對象的數據 user.setUserName("你好3"); idCard.setIdCardName("身份證003"); idCard.setIdCardId(4235); //一對一關聯數據 idCard.setUser(user); //獲取加載配置管理類 Configuration configuration = new Configuration(); //加載User的映射文件! configuration.configure().addClass(User.class).addClass(IdCard.class); //建立Session工廠對象 SessionFactory factory = configuration.buildSessionFactory(); //獲得Session對象 Session session = factory.openSession(); //使用Hibernate操做數據庫,都要開啓事務,獲得事務對象 Transaction transaction = session.getTransaction(); //開啓事務 transaction.begin(); //保存對象的數據,idCard配置文件使用級聯保存 session.save(idCard); //提交事務 transaction.commit(); //關閉Session session.close(); } }