次は、1:1の関連です。
こちらは、1:Nよりも楽です。
ポイントは、1:Nの時と同じく適切にresultMapを定義することだけです。
[テーブル定義]
create table one_to_one_test_table1 (id int primary key, val varchar(50), table2_id int); create table one_to_one_test_table2 (id int primary key, val varchar(50), table1_id int); create sequence test_table1_seq; create sequence test_table2_seq;
[ドメインオブジェクト]
// vim:set ts=4 sw=4 et ws is nowrap ft=java fenc=cp932 ff=dos: package gsf.samples.ibatis.sample003; import java.io.*; import org.apache.commons.lang.builder.*; /** * ONE_TO_ONE_TEST_TABLE1に対応するドメインオブジェクト.<br/> * * @author gsf_zero1 * */ public class Table1 implements Serializable{ /** ID */ private Integer id; /** VAL */ private String value; /** ONE_TO_ONE_TEST_TABLE2オブジェクト */ private Table2 table2; /** * コンストラクタ.<br/> * */ public Table1(){ } /** * Get id. * * @return id as Integer. */ public Integer getId(){ return this.id; } /** * Set id. * * @param id the value to set. */ public void setId(Integer id){ this.id = id; } /** * Get value. * * @return value as String. */ public String getValue(){ return this.value; } /** * Set value. * * @param value the value to set. */ public void setValue(String value){ this.value = value; } /** * Get table2. * * @return table2 as Table2. */ public Table2 getTable2(){ return this.table2; } /** * Set table2. * * @param table2 the value to set. */ public void setTable2(Table2 table2){ this.table2 = table2; } /** * オブジェクトの文字列表現を返す.<br/> * * @return 文字列表現 * * @see java.lang.Object#toString() * */ @Override public String toString(){ return new ToStringBuilder(this) .append("id", this.getId()) .append("val", this.getValue()) .toString(); } }
// vim:set ts=4 sw=4 et ws is nowrap ft=java fenc=cp932 ff=dos: package gsf.samples.ibatis.sample003; import java.io.*; import org.apache.commons.lang.builder.*; /** * ONE_TO_ONE_TEST_TABLE2に対応するドメインオブジェクト.<br/> * * @author gsf_zero1 * */ public class Table2 implements Serializable{ /** ID */ private Integer id; /** VAL */ private String value; /** ONE_TO_ONE_TEST_TABLE1オブジェクト */ private Table1 table1; /** * コンストラクタ.<br/> * */ public Table2(){ } /** * Get id. * * @return id as Integer. */ public Integer getId(){ return this.id; } /** * Set id. * * @param id the value to set. */ public void setId(Integer id){ this.id = id; } /** * Get value. * * @return value as String. */ public String getValue(){ return this.value; } /** * Set value. * * @param value the value to set. */ public void setValue(String value){ this.value = value; } /** * Get table1. * * @return table1 as Table1. */ public Table1 getTable1(){ return this.table1; } /** * Set table1. * * @param table1 the value to set. */ public void setTable1(Table1 table1){ this.table1 = table1; } /** * オブジェクトの文字列表現を返す.<br/> * * @return 文字列表現 * * @see java.lang.Object#toString() * */ @Override public String toString(){ return new ToStringBuilder(this) .append("id", this.getId()) .append("val", this.getValue()) .toString(); } }
[SQL設定ファイル]
<?xml version="1.0" encoding="Windows-31J"?> <!-- vim:set ts=4 sw=4 et ws is nowrap ft=xml fenc=cp932 ff=dos: --> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <!-- 1:1の関連を行うSQL設定ファイル
-
- >
[動作確認クラス]
// vim:set ts=4 sw=4 et ws is nowrap ft=java fenc=cp932 ff=dos: package gsf.samples.ibatis.sample003; import com.ibatis.common.resources.*; import com.ibatis.sqlmap.client.*; /** * IBatisSample003の動作確認を行うクラス.<br/> * * @author gsf_zero1 * */ public class IBatisSample003{ /** * アプリケーションエントリポイント.<br/> * * @param args 起動時引数 * @throws Exception エラーが発生した場合 * */ public static void main(String[] args) throws Exception{ SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient( Resources.getResourceAsReader("SqlMapConfig.xml")); try{ sqlMap.startTransaction(); Table1 t1 = new Table1(); t1.setId( (Integer) sqlMap.queryForObject("getNextTable1Id", null) ); t1.setValue("table1_value"); Table2 t2 = new Table2(); t2.setId( (Integer) sqlMap.queryForObject("getNextTable2Id", null) ); t2.setValue("table2_value"); t1.setTable2(t2); t2.setTable1(t1); sqlMap.insert("insertTable1", t1); sqlMap.insert("insertTable2", t2); sqlMap.commitTransaction(); }finally{ sqlMap.endTransaction(); } Table1 aTable1 = (Table1) sqlMap.queryForObject( "getTable1Data", (Integer) sqlMap.queryForObject("getMostRecentTable1Id", null)); Table2 aTable2 = aTable1.getTable2(); System.out.println(aTable1); System.out.println(aTable2); } }
実行すると、Table1からTable2オブジェクトが参照できることが確認できます。
N:Nは、1:Nの応用になるので、特にとりあげるのはやめます。
iBatisのDeveloper'sガイドを見ていると複合キーとかいろいろ対応できるみたいです。
次は、Dynamic Mapped Statementsに挑戦。