Hibernateコンポーネントマッピングの例
Hibernateコンポーネントは、エンティティ(テーブル)ではなく、値またはプロパティのグループとして表します。 Hibernateでのコンポーネントの動作を理解するには、次のチュートリアルを参照してください。
1.顧客表
下記のcustomerテーブルを参照してください。
Customerテーブル、MySQLデータベースのSQLスクリプト.
CREATE TABLE `customer` ( `CUST__ID` int(10) unsigned NOT NULL AUTO__INCREMENT, `CUST__NAME` varchar(10) NOT NULL, `AGE` int(10) unsigned NOT NULL, `ADDRESS1` varchar(255) NOT NULL, `ADDRESS2` varchar(255) NOT NULL, `ADDRESS3` varchar(255) NOT NULL, `CREATED__DATE` date NOT NULL, `CREATED__BY` varchar(10) NOT NULL, PRIMARY KEY (`CUST__ID`) USING BTREE );
2.モデルクラス
上記の ”
customer
“テーブルを表すために、2つのモデルクラス、Customer.javaとAddress.javaを作成します。
File:Customer.java
package com.mkyong.customer; import java.util.Date; public class Customer implements java.io.Serializable { private Integer custId; private String custName; private int age; private Address address; private Date createdDate; private String createdBy; //setters and getters }
File:Address.java
package com.mkyong.customer; public class Address implements java.io.Serializable { private String address1; private String address2; private String address3; //setters and getters }
この場合、
Address.java`は
Customer.java`の “Address1″、 “Address2″、 “Address3″の列を表す ”
component
“です
3.コンポーネントマッピング
これで、コンポーネントマッピングを次のように宣言できます。
File:Customer.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"> <hibernate-mapping> <class name="com.mkyong.customer.Customer" table="customer" catalog="mkyongdb"> <id name="custId" type="java.lang.Integer"> <column name="CUST__ID"/> <generator class="identity"/> </id> <property name="custName" type="string"> <column name="CUST__NAME" length="10" not-null="true"/> </property> <property name="age" type="int"> <column name="AGE" not-null="true"/> </property> <component name="Address" class="com.mkyong.customer.Address"> <property name="address1" type="string"> <column name="ADDRESS1" not-null="true"/> </property> <property name="address2" type="string"> <column name="ADDRESS2" not-null="true"/> </property> <property name="address3" type="string"> <column name="ADDRESS3" not-null="true"/> </property> </component> <property name="createdDate" type="date"> <column name="CREATED__DATE" length="10" not-null="true"/> </property> <property name="createdBy" type="string"> <column name="CREATED__BY" length="10" not-null="true"/> </property> </class> </hibernate-mapping>
4.それを実行する
Addressオブジェクトを作成し、これをCustomerオブジェクトに組み込み、永続化します。
package com.mkyong; import java.util.Date; import org.hibernate.Session; import com.mkyong.customer.Address; import com.mkyong.customer.Customer; import com.mkyong.util.HibernateUtil; public class App { public static void main(String[]args) { System.out.println("Hibernate component mapping"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Address address = new Address(); address.setAddress1("Address 1"); address.setAddress2("Address 2"); address.setAddress3("Address 3"); Customer cust = new Customer(); cust.setCustName("mkyong"); cust.setAge(30); cust.setAddress(address); cust.setCreatedDate(new Date()); cust.setCreatedBy("system"); session.save(cust); session.getTransaction().commit(); System.out.println("Done"); } }
出力…
Hibernate component mapping Hibernate: insert into mkyongdb.customer (CUST__NAME, AGE, ADDRESS1, ADDRESS2, ADDRESS3, CREATED__DATE, CREATED__BY) values (?, ?, ?, ?, ?, ?, ?) Done
それをダウンロードする –
Hibernate-Component-Mapping-Example.zip
(10KB)
リファレンス
コンポーネントドキュメント]