仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1393|回复: 19
打印 上一主题 下一主题

[学习教程] JAVA编程:bug? Hbm2JavaTask没法完成joined-subc...

[复制链接]
海妖 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:50:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
手机用到的是用j2me所编出来的小程序。
假如你其实不盘算利用类承继布局并非很有需要浏览本文。请先浏览我写的另外一篇文章"利用hibernate扩大工具Hbm2JavaTask依据设置文件天生耐久化对象类(2.1.2)"1.在文档第8章(hibernate/doc/reference/zh-cn/html/inheritance.html)有提到

“每一个子类一个表”的映照是如许的:

<classname="Payment"table="PAYMENT"><idname="id"type="long"column="PAYMENT_ID"><generatorclass="native"/></id><propertyname="amount"column="AMOUNT"/>...<joined-subclassname="CreditCardPayment"table="CREDIT_PAYMENT"><keycolumn="PAYMENT_ID"/>...</joined-subclass><joined-subclassname="CashPayment"table="CASH_PAYMENT"><keycolumn="PAYMENT_ID"/>...</joined-subclass><joined-subclassname="ChequePayment"table="CHEQUE_PAYMENT"><keycolumn="PAYMENT_ID"/>...</joined-subclass></class>

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

同意在自力的映照文档中界说subclass和joined-subclass,间接位于hibernate-mapping下。这就能够让你每次扩大你的类条理的时分,到场新的映照文件就好了。在子类的映照中你必需指定一个extents属性,指明先前已映照过的超类。利用这个功效的时分,必定要注重映照文件的排序长短常主要的!

<hibernate-mapping><subclassname="eg.subclass.DomesticCat"extends="eg.Cat"discriminator-value="D"><propertyname="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.1Payment.hbm.xml<?xmlversion="1.0"encoding="gbk"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><classname="payment.Payment"table="PAYMENT"><idname="id"type="long"column="PAYMENT_ID"><generatorclass="native"/></id><propertyname="amount"column="AMOUNT"type="long"/></class></hibernate-mapping>4.2CreditCardPayment.hbm.xml
<?xmlversion="1.0"encoding="gbk"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><joined-subclassname="payment.CreditCardPayment"table="CREDIT_PAYMENT"extends="payment.Payment"><keycolumn="PAYMENT_ID"/></joined-subclass></hibernate-mapping>
4.3CashPayment.hbm.xml
<?xmlversion="1.0"encoding="gbk"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><joined-subclassname="payment.CashPayment"table="CASH_PAYMENT"extends="payment.Payment"><keycolumn="PAYMENT_ID"/></joined-subclass></hibernate-mapping>
5.在命令行进进工程目次,运转ant,产生毛病,关头提醒以下:net.sf.hibernate.MappingException:Cannotextendunmappedclasspayment.Payment6.查错历程我就不说了,对照无聊,只说一下成绩出在那里6.1Hbm2JavaTask里对设置文件列表做了轮回,每一个文件独自处置,以是有联系关系的类就找不到了。6.2可是CodeGenerator类也有不当,没有思索文件分列成绩,由于子类大概先于父类被处置。7.上面帖出两个修正过的文件代码。在修正的中央加了中文正文。7.1net.sf.hibernate.tool.hbm2java.Hbm2JavaTask
packagenet.sf.hibernate.tool.hbm2java;importjava.io.File;importjava.io.PrintWriter;importjava.io.StringWriter;importjava.util.ArrayList;importjava.util.List;importorg.apache.tools.ant.BuildException;importorg.apache.tools.ant.DirectoryScanner;importorg.apache.tools.ant.Task;importorg.apache.tools.ant.types.FileSet;importorg.apache.tools.ant.types.Path;importorg.apache.tools.ant.types.Reference;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;/***Taskforhbm2java(Hibernatescodegenerator)***@authorGBegleyandmax**/publicclassHbm2JavaTaskextendsTask{privatestaticfinalLoglog=LogFactory.getLog(CodeGenerator.class);privateFileconfigurationFile;privatePathcompileClasspath;privateFileoutputDir;privateListfilesets=newArrayList();/***Setahbm2java<literal>config.xml</literal>configurationfile*@paramthefilename*/publicvoidsetConfig(FileconfigurationFile){this.configurationFile=configurationFile;}/***Settheclasspathtobeusedforthiscompilation.**@paramclasspathanAntPathobjectcontainingthecompilationclasspath.*/publicvoidsetClasspath(Pathclasspath){if(compileClasspath==null){compileClasspath=classpath;}else{compileClasspath.append(classpath);}}/**Getstheclasspathtobeusedforthiscompilation.*/publicPathgetClasspath(){returncompileClasspath;}/***Addsapathtotheclasspath.*/publicPathcreateClasspath(){if(compileClasspath==null){compileClasspath=newPath(getProject());}returncompileClasspath.createPath();}/***Addsareferencetoaclasspathdefinedelsewhere.*/publicvoidsetClasspathRef(Referencer){createClasspath().setRefid(r);}/***Addsasetoffilestotranslate.*/publicvoidaddFileset(FileSetset){filesets.add(set);}/***Setstheoutputdirectory.**@parambinDirectorydirectory*/publicvoidsetOutput(FileoutDirectory){this.outputDir=outDirectory;}publicvoidexecute()throwsBuildException{ListfileList=getTargetFiles();if(fileList.size()==0)return;log("Processing"+fileList.size()+"files.");try{log("Buildinghibernateobjects");//这个轮回是毛病1,//for(inti=0;i<fileList.size();i++){//processFile(outputDir,(File)fileList.get(i));//}//要修正processFile办法processFile(outputDir,fileList);}catch(Throwablet){StringWritersw=newStringWriter();t.printStackTrace(newPrintWriter(sw));thrownewBuildException("Causedby:
"+sw.toString());}}/*****Comment:*Theinitialanttaskhadsomeinitialfilteringonthehbm.xml/javanamestoonlyprocesstheneededfiles.*Thatisnotpossibletodecideintheanttaskwithoutimplementingthesamelogicpresentinhbm2java.*ThusIveremoveditandletitbesomethingthathbm2javashoulddo.***@return*/privateListgetTargetFiles(){Listl=newjava.util.ArrayList();//dealwiththefilesetsfor(inti=0;i<filesets.size();i++){FileSetfs=(FileSet)filesets.get(i);Fileparent=fs.getDir(getProject());DirectoryScannerds=fs.getDirectoryScanner(getProject());String[]files=ds.getIncludedFiles();for(intj=0;j<files.length;j++){FilesrcFile=newFile(parent,files[j]);l.add(srcFile);}}returnl;}//修正了办法参数privatevoidprocessFile(FileoutputDir,ListfileList){Listargs=newArrayList();if(outputDir!=null){args.add("--output="+outputDir.getAbsolutePath());}if(configurationFile!=null){args.add("--config="+configurationFile);}//把一切文件都到场命令行参数for(inti=0;i<fileList.size();i++){args.add(((File)fileList.get(i)).getAbsolutePath());}try{net.sf.hibernate.tool.hbm2java.CodeGenerator.main((String[])args.toArray(newString[args.size()]));}catch(Throwablet){StringWritersw=newStringWriter();t.printStackTrace(newPrintWriter(sw));thrownewBuildException("Causedby:
"+sw.toString());}}}
7.2net.sf.hibernate.tool.hbm2java.CodeGenerator
/**$Id:CodeGenerator.java,v1.72004/03/2220:41:47maxcsaucdkExp$*/packagenet.sf.hibernate.tool.hbm2java;importjava.io.File;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importnet.sf.hibernate.MappingException;importnet.sf.hibernate.util.DTDEntityResolver;importorg.apache.commons.collections.MultiHashMap;importorg.apache.commons.collections.MultiMap;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.jdom.Document;importorg.jdom.Element;importorg.jdom.input.SAXBuilder;importorg.xml.sax.ErrorHandler;importorg.xml.sax.SAXParseException;/****/publicclassCodeGenerator{privatestaticfinalLoglog=LogFactory.getLog(CodeGenerator.class);publicstaticvoidmain(String[]args){if(args.length==0){System.err.println("Noargumentsprovided.Nothingtodo.Exit.");System.exit(-1);}try{ArrayListmappingFiles=newArrayList();SAXBuilderbuilder=newSAXBuilder(true);builder.setEntityResolver(newDTDEntityResolver());builder.setErrorHandler(newErrorHandler(){publicvoiderror(SAXParseExceptionerror){log.error("ErrorparsingXML:"+error.getSystemId()+(+error.getLineNumber()+),error);}publicvoidfatalError(SAXParseExceptionerror){error(error);}publicvoidwarning(SAXParseExceptionerror){log.warn("WarningparsingXML:"+error.getSystemId()+(+error.getLineNumber()+));}});StringoutputDir=null;Listgenerators=newArrayList();MultiMapglobalMetas=newMultiHashMap();//parsecommandlineparametersfor(inti=0;i<args.length;i++){if(args[i].startsWith("--")){if(args[i].startsWith("--config=")){//parseconfigxmlfilebuilder.setValidation(false);Documentdocument=builder.build(newFile(args[i].substring(9)));globalMetas=MetaAttributeHelper.loadAndMergeMetaMap(document.getRootElement(),null);IteratorgenerateElements=document.getRootElement().getChildren("generate").iterator();while(generateElements.hasNext()){generators.add(newGenerator((Element)generateElements.next()));}builder.setValidation(true);}elseif(args[i].startsWith("--output=")){outputDir=args[i].substring(9);}}else{mappingFiles.add(args[i]);}}//ifnoconfigxmlfile,addadefaultgeneratorif(generators.size()==0){generators.add(newGenerator());}HashMapclassMappings=newHashMap();builder.setValidation(true);//这个轮回是毛病2//改成只处置class映照for(Iteratoriter=mappingFiles.iterator();iter.hasNext();){//parsethemappingfileFilefile=newFile((String)iter.next());log.debug(file.getAbsolutePath());Documentdocument=builder.build(file);ElementrootElement=document.getRootElement();org.jdom.Attributea=rootElement.getAttribute("package");Stringpkg=null;if(a!=null){pkg=a.getValue();}MappingElementme=newMappingElement(rootElement,null/**TODO-hbm2java:-shouldbeconfig.xml**/);IteratorclassElements=rootElement.getChildren("class").iterator();MultiMapmm=MetaAttributeHelper.loadAndMergeMetaMap(rootElement,globalMetas);handleClass(pkg,me,classMappings,classElements,mm,false);}//复制了上一个轮回//处置subclass和joined-class映照for(Iteratoriter=mappingFiles.iterator();iter.hasNext();){//parsethemappingfileFilefile=newFile((String)iter.next());Documentdocument=builder.build(file);ElementrootElement=document.getRootElement();org.jdom.Attributea=rootElement.getAttribute("package");Stringpkg=null;if(a!=null){pkg=a.getValue();}MappingElementme=newMappingElement(rootElement,null/**TODO-hbm2java:-shouldbeconfig.xml**/);MultiMapmm=MetaAttributeHelper.loadAndMergeMetaMap(rootElement,globalMetas);IteratorclassElements=rootElement.getChildren("subclass").iterator();handleClass(pkg,me,classMappings,classElements,mm,true);classElements=rootElement.getChildren("joined-subclass").iterator();handleClass(pkg,me,classMappings,classElements,mm,true);}//generatesourcefilesfor(Iteratoriterator=generators.iterator();iterator.hasNext();){Generatorg=(Generator)iterator.next();g.setBaseDirName(outputDir);g.generate(classMappings);}}catch(Exceptione){e.printStackTrace();}}privatestaticvoidhandleClass(StringclassPackage,MappingElementme,HashMapclassMappings,IteratorclassElements,MultiMapmm,booleanextendz)throwsMappingException{while(classElements.hasNext()){Elementclazz=(Element)classElements.next();if(!extendz){ClassMappingcmap=newClassMapping(classPackage,clazz,me,mm);classMappings.put(cmap.getFullyQualifiedName(),cmap);}else{Stringex=clazz.getAttributeValue("extends");if(ex==null){thrownewMappingException("Missingextendsattributeon<"+clazz.getName()+"name="+clazz.getAttributeValue("name")+">");}ClassMappingsuperclass=(ClassMapping)classMappings.get(ex);if(superclass==null){thrownewMappingException("Cannotextendunmappedclass"+ex);}ClassMappingsubclassMapping=newClassMapping(classPackage,me,superclass.getClassName(),superclass,clazz,mm);superclass.addSubClass(subclassMapping);}}}}

IDE是好。java中的IDE更是百花齐放,你用jbuilder能说jbuilder赶不上vs吗?用eclipse,netbeans也很舒服啊。我就不明白“稍微差一些”那一些是从哪里差来的。
蒙在股里 该用户已被删除
沙发
发表于 2015-1-21 16:10:41 | 只看该作者
多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。
灵魂腐蚀 该用户已被删除
板凳
发表于 2015-1-30 20:47:00 | 只看该作者
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
只想知道 该用户已被删除
地板
发表于 2015-1-31 18:43:48 | 只看该作者
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
变相怪杰 该用户已被删除
5#
发表于 2015-2-2 22:06:18 | 只看该作者
如果你学过HTML,那么事情要好办的多,如果没有,那你快去补一补HTML基础吧。其实JSP中的Java语法也不多,它更象一个脚本语言,有点象ASP。
若天明 该用户已被删除
6#
发表于 2015-2-8 09:50:43 | 只看该作者
Java 不同于一般的编译执行计算机语言和解释执行计算机语言。它首先将源代码编译成二进制字节码(bytecode),然后依赖各种不同平台上的虚拟机来解释执行字节码。从而实现了“一次编译、到处执行”的跨平台特性。
金色的骷髅 该用户已被删除
7#
发表于 2015-3-2 15:10:31 | 只看该作者
象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。
活着的死人 该用户已被删除
8#
发表于 2015-3-6 23:43:59 | 只看该作者
是一种语言,用以产生「小应用程序(Applet(s))
精灵巫婆 该用户已被删除
9#
发表于 2015-3-13 22:34:56 | 只看该作者
是一种突破用户端机器环境和CPU
莫相离 该用户已被删除
10#
发表于 2015-3-20 20:50:19 | 只看该作者
你现在最缺的是实际的工作经验,而不是书本上那些凭空想出来的程序。
山那边是海 该用户已被删除
11#
发表于 2015-3-23 18:01:59 | 只看该作者
关于设计模式的资料,还是向大家推荐banq的网站 [url]http://www.jdon.com/[/url],他把GOF的23种模式以通俗易懂的方式诠释出来,纯Java描述,真是经典中的经典。
不帅 该用户已被删除
12#
发表于 2015-4-1 10:01:04 | 只看该作者
设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧
小妖女 该用户已被删除
13#
发表于 2015-4-1 21:08:50 | 只看该作者
另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。
乐观 该用户已被删除
14#
发表于 2015-4-3 02:41:34 | 只看该作者
至于JDBC,就不用我多说了,你如果用java编过存取数据库的程序,就应该很熟悉。还有,如果你要用Java编发送电子邮件的程序,你就得看看Javamail 了。
15#
发表于 2015-4-6 09:01:51 | 只看该作者
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
海妖 该用户已被删除
16#
 楼主| 发表于 2015-4-26 12:12:39 | 只看该作者
Pet Store.(宠物店)是SUN公司为了演示其J2EE编程规范而推出的开放源码的程序,应该很具有权威性,想学J2EE和EJB的朋友不要 错过了。
愤怒的大鸟 该用户已被删除
17#
发表于 2015-5-5 13:16:10 | 只看该作者
是一种使用者不需花费很多时间学习的语言
admin 该用户已被删除
18#
发表于 2015-6-5 22:25:07 | 只看该作者
是一种语言,用以产生「小应用程序(Applet(s))
第二个灵魂 该用户已被删除
19#
发表于 2015-6-17 07:21:44 | 只看该作者
不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。
再现理想 该用户已被删除
20#
发表于 2015-6-19 21:57:58 | 只看该作者
让你能够真正掌握接口或抽象类的应用,从而在原来的Java语言基础上跃进一步,更重要的是,设计模式反复向你强调一个宗旨:要让你的程序尽可能的可重用。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-4-28 16:53

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表