一步步學習Hibernate架構(四):採用jpa實現一對多關聯映射(二)

來源:互聯網
上載者:User

標籤:hibernate   jpa   java   資料庫   

        上篇部落格已經採用jpa註解來實現了一對多關聯映射,將這種關聯映射以外鍵的形式處理,現在來看看怎麼以第三張表的形式處理。採用jpa註解來映射資料庫要使用的jar包見上篇部落格:

                  一步步學習Hibernate架構(三):採用jpa實現一對多關聯映射(一)

 現在看第二種方式:以第三張表的關係來體現

Group.java

package com.tgb.zhudan;import java.util.List;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="t_group")public class Group {private int id;private String name;private List<User> user;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@OneToManypublic List<User> getUser() {return user;}public void setUser(List<User> user) {this.user = user;}}

User.java

package com.tgb.zhudan;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {private int id;private String name;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_jpa_one2many</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><mapping class="com.tgb.zhudan.Group"/><mapping class="com.tgb.zhudan.User"/></session-factory></hibernate-configuration>

ExportDB:

package com.tgb.zhudan;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 將hbm產生ddl * @author Administrator * */public class ExportDB {public static void main(String[] args) {//預設讀取hibernate.cfg.xml檔案Configuration cfg = new AnnotationConfiguration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true, true);}}

產生的資料庫表如下:

            
t_group表:

          

t_user表:

          

t_group_t_user表:

             

總結:

        從上面的三張圖來看,在第三張表(t_group_t_user)中,將一端(t_group)中的id和多端(t_user)中的id拿出來作為第三張表的聯合主鍵。這個關係由hebernate自己維護,不需要人為操作。

        

一步步學習Hibernate架構(四):採用jpa實現一對多關聯映射(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.