|
Hibernateのマッピング定義についていろいろ調べてみました。例を使ってまとめておきます。 例となるモデル †あるサービスの顧客(Customer)とその担当者(User)というモデルを例にして考えてみます。担当者は複数の顧客を担当しています。逆に顧客にはひとり、担当者がついています。ついていない場合もあるとしましょう。 E-R図 †CustomerはUserへの外部キーをもつことで担当者を表現しています。また担当がまだついていない場合もあるということでこの外部キーカラムはnullをOKとしています。 テーブル定義 †
JavaBeans?とhbm.xmlファイル †
hibernate.cfg.xml †<?xml version="1.0" encoding="utf-8"?>
<!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">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.username">xxxx</property>
<property name="hibernate.connection.password">xxxx</property>
<property name="hibernate.connection.url">jdbc:mysql://xxxx/Samples?useUnicode=true
&characterEncoding=ujis</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<mapping resource="nu/mine/kino/entity/User.hbm.xml" />
<mapping resource="nu/mine/kino/entity/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
いろいろテスト †関連リンク †この記事は 現在のアクセス:23439 |