WEB开发网
开发学院WEB开发Jsp bug? Hbm2JavaTask无法实现joined-subclass单独配... 阅读

bug? Hbm2JavaTask无法实现joined-subclass单独配置文件(2.1.2)

 2008-01-05 08:53:51 来源:WEB开发网   
核心提示:假如你并不打算使用类继续结构并不是很有必要阅读本文,请先阅读我写的另一篇文章 "使用hibernate扩展工具Hbm2javaTask根据配置文件生成持久化对象类(2.1.2)"1.在文档第8章(hibernate/doc/reference/zh-cn/Html/inheritance.HTML)

假如你并不打算使用类继续结构并不是很有必要阅读本文。请先阅读我写的另一篇文章 "使用hibernate扩展工具Hbm2javaTask根据配置文件生成持久化对象类(2.1.2)"1.在文档第8章(hibernate/doc/reference/zh-cn/Html/inheritance.HTML)有提到

“每个子类一个表”的映射是这样的:

<class name="Payment" table="PAYMENT"> <id name="id" type="long" column="PAYMENT_ID"> <generator class="native"/> </id> <PRoperty name="amount" column="AMOUNT"/> ... <joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT"> <key column="PAYMENT_ID"/> ... </joined-subclass> <joined-subclass name="CashPayment" table="CASH_PAYMENT"> <key column="PAYMENT_ID"/> ... </joined-subclass> <joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT"> <key column="PAYMENT_ID"/> ... </joined-subclass></class>

2.文档第5章(hibernate/doc/reference/zh-cn/HTML/mapping.HTML)有提到

答应在独立的映射文档中定义subclass和joined-subclass,直接位于hibernate-mapping下。这就可以让你每次扩展你的类层次的时候,加入新的映射文件就行了。在子类的映射中你必须指定一个extents属性,指明先前已经映射过的超类。使用这个功能的时候,一定要注重映射文件的排序是非常重要的!

<hibernate-mapping> <subclass name="eg.subclass.DomesticCat" extends="eg.Cat" discriminator-value="D"> <property name="name" type="string"/> </subclass></hibernate-mapping>
3.根据以上两点,偶根据第8章的PAYMENT创建了一个工程,把joined-subclass移了出来,现在工程目录结构如下 Payment  <dir>-src  <dir>-hbm  <dir> -payment  <dir>  -Payment.hbm.xml  -CreditCardPayment.hbm.XML  -CashPayment.hbm.XML-classes  <dir>-lib  <dir>-build.XML-hibernate.codegen.XML-log4j.properties4. 本文不再重复build.XML, hibernate.codegen.XML, log4j.properties三个文件的内容。可在"使用hibernate扩展工具Hbm2JavaTask根据配置文件生成持久化对象类(2.1.2)"一文查看这三个文件的内容。此处仅列出.hbm.XML文件内容。 4.1 Payment.hbm.XML<?XML version="1.0" encoding="gbk"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="payment.Payment" table="PAYMENT">  <id name="id" type="long" column="PAYMENT_ID">   <generator class="native"/>  </id>  <property name="amount" column="AMOUNT" type="long"/> </class></hibernate-mapping> 4.2 CreditCardPayment.hbm.XML
<?XML version="1.0" encoding="gbk"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><joined-subclass name="payment.CreditCardPayment" table="CREDIT_PAYMENT" extends="payment.Payment"> <key column="PAYMENT_ID"/></joined-subclass></hibernate-mapping>
 4.3 CashPayment.hbm.XML
<?XML version="1.0" encoding="gbk"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><joined-subclass name="payment.CashPayment" table="CASH_PAYMENT" extends="payment.Payment"> <key column="PAYMENT_ID"/></joined-subclass></hibernate-mapping>
5.在命令行进入工程目录,运行ant,发生错误,要害提示如下: net.sf.hibernate.MappingException: Cannot extend unmapped class payment.Payment6.查错过程我就不说了,比较无聊,只说一下问题出在哪里 6.1 Hbm2JavaTask里对配置文件列表做了循环,每个文件单独处理,所以有关联的类就找不到了。  6.2 但是CodeGenerator类也有不妥,没有考虑文件排列问题,因为子类可能先于父类被处理。7.下面帖出两个修改过的文件代码。在修改的地方加了中文注释。 7.1 net.sf.hibernate.tool.hbm2java.Hbm2JavaTask
package net.sf.hibernate.tool.hbm2java;import java.io.File;import java.io.PrintWriter;import java.io.StringWriter;import java.util.ArrayList;import java.util.List;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.Reference;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Task for hbm2java (Hibernates codegenerator) * * * @author GBegley and max * */public class Hbm2JavaTask extends Task { private static final Log log = LogFactory.getLog(CodeGenerator.class); private File configurationFile; private Path compileClasspath; private File outputDir; private List filesets = new ArrayList(); /** * Set a hbm2java <literal>config.XML</literal> configuration file * @param the file name */ public void setConfig(File configurationFile) { this.configurationFile = configurationFile; } /** * Set the classpath to be used for this compilation. * * @param classpath an Ant Path object containing the compilation classpath. */ public void setClasspath(Path classpath) { if (compileClasspath == null) { compileClasspath = classpath; } else { compileClasspath.append(classpath); } } /** Gets the classpath to be used for this compilation. */ public Path getClasspath() { return compileClasspath; } /** * Adds a path to the classpath. */ public Path createClasspath() { if (compileClasspath == null) { compileClasspath = new Path(getProject()); } return compileClasspath.createPath(); } /** * Adds a reference to a classpath defined elsewhere. */ public void setClasspathRef(Reference r) { createClasspath().setRefid(r); } /** * Adds a set of files to translate. */ public void addFileset(FileSet set) { filesets.add(set); } /** * Sets the output directory. * * @param binDirectory directory */ public void setOutput(File outDirectory) { this.outputDir = outDirectory; } public void execute() throws BuildException { List fileList = getTargetFiles(); if (fileList.size() == 0) return; log("Processing " + fileList.size() + " files."); try { log("Building hibernate objects"); //这个循环是错误1, //for (int i = 0; i < fileList.size(); i++) { // processFile(outputDir, (File) fileList.get(i)); //} //要修改processFile方法 processFile(outputDir, fileList); } catch (Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); throw new BuildException("Caused by:\n" + sw.toString()); } } /** * * * Comment: * The initial ant task had some initial

Tags:bug HbmJavaTask 无法

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接