マッピングファイルからJavaBeansを作成する
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Sample.Book" table="TBL_BOOK_MST"> <meta attribute="class-description"> 書籍情報 </meta> <id name="id" type="int" column="BOOK_ID"> <meta attribute="scope-set">protected</meta> <generator class="native"></generator> </id> <property name="author" type="string" not-null="true"> <meta attribute="field-description">著者</meta> </property> <property name="title" type="string" not-null="true"> <meta attribute="field-description">タイトル</meta> </property> <property name="publishDate" type="date"> <meta attribute="field-description">発行日</meta> </property> </class> </hibernate-mapping>
idタグはサロゲートキーにマッピングすると書いてありますね(ていうかサロゲートキーって別にDBのもつ機能じゃないから,ようは主キーにマッピングすればいいのかな.主キーじゃなくても一意キーならよさそうな気もしますが)
と思ったけど,やっぱ主キーを使いまわすのではなく,サロゲートキーを別に持ってくるのがいいでしょう.このキーはNULLかそうでないかで永続化されているかいないを決めるので自動採番するようなキーである必要がありそうですね.
metaタグは単にJavaDocに書き出すだけなのでなくてもよい.
Hibernate ExtensionsがAntタスクを持っているので,Extensionsのファイルをlibに全部追加する.こんなタスクを書きます.${source.root}はソースファイルの基底.
<taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="project.class.path" /> <target name="codegen" description="設定ファイルからコードを生成"> <hbm2java output="${source.root}"> <fileset dir="${source.root}"> <include name="**/*.hbm.xml"/> </fileset> </hbm2java> </target>
を記述.さっそく実行してみると,Log4jの設定がねえよと起こられつつも,コードが作成されました.
Buildfile: C:\Eclipse\workspace\SampleProject\build.xml codegen: [hbm2java] Processing 1 mapping files. [hbm2java] log4j:WARN No appenders could be found for logger (net.sf.hibernate.util.DTDEntityResolver). [hbm2java] log4j:WARN Please initialize the log4j system properly. BUILD SUCCESSFUL Total time: 16 seconds
できたJavaBeansがこっち
package Sample; import java.io.Serializable; import java.util.Date; import org.apache.commons.lang.builder.ToStringBuilder; /** * 書籍情報 * */ public class Book implements Serializable { /** identifier field */ private Integer id; /** persistent field */ private String author; /** persistent field */ private String title; /** nullable persistent field */ private Date publishDate; /** full constructor */ public Book(String author, String title, Date publishDate) { this.author = author; this.title = title; this.publishDate = publishDate; } /** default constructor */ public Book() { } /** minimal constructor */ public Book(String author, String title) { this.author = author; this.title = title; } public Integer getId() { return this.id; } protected void setId(Integer id) { this.id = id; } /** * 著者 */ public String getAuthor() { return this.author; } public void setAuthor(String author) { this.author = author; } /** * タイトル */ public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } /** * 発行日 */ public Date getPublishDate() { return this.publishDate; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } }