Hibernate –多対多の例(注釈)
このチュートリアルでは、前の「http://www.example.com/hibernate/hibernate-many-to-many-relationship-example/ [Hibernate多対多の例–XMLマッピング]」チュートリアルのインフラストラクチャ全体を再利用します。 、Hibernare / JPAアノテーションをサポートするように拡張します。
Note
結合テーブルに余分な列がある多対多の場合は、このtutorialを参照してください。
プロジェクト構造
このチュートリアルの新しいプロジェクト構造を確認してください。
1. 「多対多」テーブルの関係
前の多対多テーブルの関係をもう一度参照してください。
2. Hibernateモデルクラス
以前のモデルクラス–Stock.java
およびCategory.java
を更新し、内部に新しい注釈コードを定義します。
ファイル:Stock.java
package com.example.stock; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.CascadeType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @Entity @Table(name = "stock", catalog = "exampledb", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock implements java.io.Serializable { private Integer stockId; private String stockCode; private String stockName; private Setcategories = new HashSet (0); public Stock() { } public Stock(String stockCode, String stockName) { this.stockCode = stockCode; this.stockName = stockName; } public Stock(String stockCode, String stockName, Set categories) { this.stockCode = stockCode; this.stockName = stockName; this.categories = categories; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "STOCK_ID", unique = true, nullable = false) public Integer getStockId() { return this.stockId; } public void setStockId(Integer stockId) { this.stockId = stockId; } @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10) public String getStockCode() { return this.stockCode; } public void setStockCode(String stockCode) { this.stockCode = stockCode; } @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20) public String getStockName() { return this.stockName; } public void setStockName(String stockName) { this.stockName = stockName; } @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinTable(name = "stock_category", catalog = "exampledb", joinColumns = { @JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID", nullable = false, updatable = false) }) public Set getCategories() { return this.categories; } public void setCategories(Set categories) { this.categories = categories; } }
ファイル:Category.java
package com.example.stock; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name = "category", catalog = "exampledb") public class Category implements java.io.Serializable { private Integer categoryId; private String name; private String desc; private Setstocks = new HashSet (0); public Category() { } public Category(String name, String desc) { this.name = name; this.desc = desc; } public Category(String name, String desc, Set stocks) { this.name = name; this.desc = desc; this.stocks = stocks; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "CATEGORY_ID", unique = true, nullable = false) public Integer getCategoryId() { return this.categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } @Column(name = "NAME", nullable = false, length = 10) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "[DESC]", nullable = false) public String getDesc() { return this.desc; } public void setDesc(String desc) { this.desc = desc; } @ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories") public Set getStocks() { return this.stocks; } public void setStocks(Set stocks) { this.stocks = stocks; } }
3. Hibernate設定ファイル
注釈付きクラスStock.java
とCategory.java
を次のようにhibernate.cfg.xml
に配置します:
ファイル:hibernate.cfg.xml
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/exampledb root password org.hibernate.dialect.MySQLDialect true
4. それを実行します
実行すると、結果は一目瞭然です。
ファイル:App.java
package com.example; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import com.example.stock.Category; import com.example.stock.Stock; import com.example.util.HibernateUtil; public class App { public static void main(String[] args) { System.out.println("Hibernate many to many (Annotation)"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Stock stock = new Stock(); stock.setStockCode("7052"); stock.setStockName("PADINI"); Category category1 = new Category("CONSUMER", "CONSUMER COMPANY"); Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY"); Setcategories = new HashSet (); categories.add(category1); categories.add(category2); stock.setCategories(categories); session.save(stock); session.getTransaction().commit(); System.out.println("Done"); } }
出力
Hibernate many to many (Annotation) Hibernate: insert into exampledb.stock (STOCK_CODE, STOCK_NAME) values (?, ?) Hibernate: insert into exampledb.category (`DESC`, NAME) values (?, ?) Hibernate: insert into exampledb.category (`DESC`, NAME) values (?, ?) Hibernate: insert into exampledb.stock_category (STOCK_ID, CATEGORY_ID) values (?, ?) Hibernate: insert into exampledb.stock_category (STOCK_ID, CATEGORY_ID) values (?, ?) Done
ダウンロード–Hibernate-many-to-many-annotation.zip(9KB)