﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-为了遗忘的记录-随笔分类-Java</title><link>http://www.cnitblog.com/Raistlin/category/1569.html</link><description>Think!</description><language>zh-cn</language><lastBuildDate>Fri, 30 Sep 2011 06:36:29 GMT</lastBuildDate><pubDate>Fri, 30 Sep 2011 06:36:29 GMT</pubDate><ttl>60</ttl><item><title>CLASSPATH杂记</title><link>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5576.html</link><dc:creator>Raistlin</dc:creator><author>Raistlin</author><pubDate>Mon, 19 Dec 2005 01:25:00 GMT</pubDate><guid>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5576.html</guid><wfw:comment>http://www.cnitblog.com/Raistlin/comments/5576.html</wfw:comment><comments>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5576.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Raistlin/comments/commentRss/5576.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Raistlin/services/trackbacks/5576.html</trackback:ping><description><![CDATA[<P>本来java虚拟机是这么运行的<BR>D:\java -classpath D:\hibernate-3.0\eg org.hibernate.auction.Main </P>
<P>这个命令行将告诉java虚拟机的类装载器从D:\hibernate-3.0\eg\org\hibernate\auction处装载Main.class。<BR>如果省略"-classpath D:\hibernate-3.0\eg"，即D:\java org.hibernate.auction.Main 则类装载器将会从系统环境变量CLASSPATH指定的位置寻找符合条件的Main.class。</P>
<P><BR>如果CLASSPATH为.;D:\jdk150\lib\tools.jar;D:\jdk150\jre\lib\rt.jar;D:\hibernate-3.0\eg<BR>则类装载器先查找当前运行目录(.表示当前目录)，此处为D:\，如果D:\下面有D:\org\hibernate\auction\Main.class，则装载这个文件。如果没有，继续在D:\jdk150\lib\tools.jar文件中寻找是否有路径为org\hibernate\auction\Main.class的文件,如果没有则寻找D:\hibernate-3.0\eg\org\hibernate\auction\Main.class。如果在这些路径中都没找到，就会提示一个鸟错误。注意D:\hibernate-3.0\eg只表示将在此路径下寻找D:\hibernate-3.0\eg\org\hibernate\auction\Main.class文件，不会在此路径下的.jar文件中继续寻找。</P>
<P><BR>据说很多开发环境往往不去读系统路径，直接就java -classpath .....，这也就是在外面运行的东东挺溜的进了开发环境就死菜的原因喽。</P><img src ="http://www.cnitblog.com/Raistlin/aggbug/5576.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Raistlin/" target="_blank">Raistlin</a> 2005-12-19 09:25 <a href="http://www.cnitblog.com/Raistlin/archive/2005/12/19/5576.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Hibernate的记录</title><link>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5575.html</link><dc:creator>Raistlin</dc:creator><author>Raistlin</author><pubDate>Mon, 19 Dec 2005 01:23:00 GMT</pubDate><guid>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5575.html</guid><wfw:comment>http://www.cnitblog.com/Raistlin/comments/5575.html</wfw:comment><comments>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5575.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/Raistlin/comments/commentRss/5575.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Raistlin/services/trackbacks/5575.html</trackback:ping><description><![CDATA[<STRONG>我和hibernate的第一次亲密：<BR></STRONG><BR>我用的数据库是mssql，用middlegen稀里哗啦生成一堆文件。编译运行，hibernate报告一堆异常，大概是说，MSSQL JDBC抗议说他不能读取已经读过的字段，所以hibernate不能生成需要的collection。最后icepeak传给我一个JSQLConnect，终于把这个问题解决了。<BR><BR><STRONG>设置hibernate生成定制的主键ID</STRONG>：<BR>
<P>在和hibernate亲密接触之后,我想按照自己的想法来设计组织机构.对于Organ,Department,Post和Stuff.我希望这样设计主键的ID,类型都是char,格式为:<BR>Organ0000000001<BR>Dept0000000001<BR>Post0000000001<BR>Stuff0000000002</P>
<P>修改middlegen产生的.hbm.xml文件:</P>
<P>......<BR>&lt;class<BR>&nbsp;&nbsp;&nbsp; name="com.abcdefg.hibernate.Organ"<BR>&nbsp;&nbsp;&nbsp; table="Organ"<BR>&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;id<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name="organId"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type="java.lang.String"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column="OrganId"<BR>&nbsp;&nbsp;&nbsp; &gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;generator class="hbtest.PkGenerator"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="table"&gt;PKGenerator_table&lt;/param&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="column"&gt;NextOrganId&lt;/param&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param name="max_lo"&gt;0&lt;/param&gt;<BR>&nbsp;&lt;/generator&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/id&gt;<BR>......<BR>&lt;/class&gt;</P>
<P>找到的资料说:"如果需要采用定制的主键产生算法，则在此处配置主键生成器，主键生成器必须实现net.sf.hibernate.id.IdentifierGenerator 接口".我找了半天都没找到net.sf.hibernate.id.IdentifierGenerator.其实只要从hibernate自身提供的一些主键生成器继承就可以,从hibernate的源文件可以看到这些主键生成器实现的接口是org.hibernate.id</P>
<P>我的PkGenerator.java文件:<BR>package hbtest;</P>
<P>import org.hibernate.id.TableHiLoGenerator;<BR>import java.io.Serializable;<BR>import java.util.Properties;</P>
<P>import org.apache.commons.logging.Log;<BR>import org.apache.commons.logging.LogFactory;<BR>import org.hibernate.HibernateException;<BR>import org.hibernate.dialect.Dialect;<BR>import org.hibernate.engine.SessionImplementor;<BR>import org.hibernate.type.Type;<BR>import org.hibernate.type.IntegerType;<BR>import org.hibernate.util.PropertiesHelper;</P>
<P>public class PkGenerator<BR>&nbsp;&nbsp;&nbsp; extends TableHiLoGenerator {</P>
<P>&nbsp; private String columnName;</P>
<P>&nbsp; public void configure(Type type, Properties params, Dialect d) {<BR>&nbsp;&nbsp;&nbsp; super.configure(new IntegerType(), params, d);<BR>&nbsp;&nbsp;&nbsp; this.columnName = PropertiesHelper.getString(COLUMN, params,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT_COLUMN_NAME);<BR>&nbsp; }</P>
<P>&nbsp; public synchronized Serializable generate(SessionImplementor session,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object obj) throws<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HibernateException {<BR>&nbsp;&nbsp;&nbsp; Object result = super.generate(session, obj);<BR>&nbsp;&nbsp;&nbsp; String strNumber = result.toString();<BR>&nbsp;&nbsp;&nbsp; StringBuffer strResult = new StringBuffer("");<BR>&nbsp;&nbsp;&nbsp; if (columnName.compareToIgnoreCase("NextOrganId") == 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strResult.append("Organ");<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; else if(columnName.compareToIgnoreCase("NextDeptId") == 0){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strResult.append("Dept");<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; ......</P>
<P>&nbsp;&nbsp;&nbsp; if (strNumber.length() &gt; 10)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new HibernateException("The generated " + strResult +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Id is too long!");<BR>&nbsp;&nbsp;&nbsp; else if (strNumber.length() == 10) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strResult.append(strNumber);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int count = 10 - strNumber.length();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (count &gt; 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strResult.append("0");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count--;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strResult.append(strNumber);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; return strResult.toString();<BR>&nbsp; }<BR>}</P>
<P>实现了以后发现在这些主键前面添加"Organ","Dept"之类的并不是特别有用,而且我开始的出发点还是错的.也许我应该建立更多的映射表...?</P>
<P><A HREF="/Files/Raistlin/lifeapplet.rar" rel=nofollow><FONT color=#003399>Hibernate的初级指南<BR></FONT></A><BR><STRONG>hibernate贴心的地方</STRONG>：<BR></P>
<DIV>验证了一下，我的担心终于被证实是多余的了，我担心的是hibernate会为同一条记录产生一堆不同的对象。</DIV>
<DIV>我觉得使hibernate真正实用的条件：为同一条记录产生的多个对象是同一个对象。当然前提是设置了主键。</DIV>
<DIV>hibernate也许是将当前读出的对象保存在一个哈希表，在读一个新对象的时候会先从这个哈希表中查找。好像这并不是多困难的事情。</DIV>
<DIV>&nbsp;</DIV>
<DIV>但多线程情况下就有所不同了..</DIV>
<DIV>因为Hibernate说(对于Session)：</DIV>
<DIV>It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory</DIV>
<DIV>&nbsp;</DIV>
<DIV>Session并没有要求是线程安全的，而通过实验表明记录和对象的一一对应只存在于同一个Session中。这也是hibernate的文档中HibernateUtil类使用ThreadLocal的原因吧。</DIV>
<DIV>所以对于多线程的情况，在不同的线程中，对应同一条记录的对象是不同的。<BR><BR>(另hibernate有二级缓存，是全局的缓存，可以用来减少数据库负载，二级缓存的功能需要设置才会启用)<BR><BR><STRONG>Hibernate中修改多对多关系的属性：</STRONG><BR>
<P>&lt;middlegen&gt;项中有如下&lt;table&gt;配置:<BR>&nbsp;&nbsp; &lt;table generate="true" name="Post"/&gt;<BR>&nbsp;&nbsp; &lt;table generate="true" name="Stuff"/&gt;<BR>&nbsp;&nbsp; &lt;many2many&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;tablea generate="true" name="Post" /&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;jointable name="PostStuff" generate="true" /&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;tableb generate="true" name="Stuff" /&gt; <BR>&nbsp;&nbsp; &lt;/many2many&gt;<BR>即表Post与Stuff的关系是多对多，而且表PostStuff中一个字段时JoinDate:员工加入部门的时间。</P>
<P>我现在想加入一条Post记录，一条Stuff记录，然后设置Stuff加入Post的时间。经过多次尝试，代码如下：<BR>&nbsp;&nbsp; ...<BR>&nbsp;&nbsp; Dept dept = ....<BR>&nbsp;&nbsp; Post post = new Post("third post1",dept,new HashSet(),new HashSet());<BR>&nbsp;&nbsp; Stuff stuff = new Stuff("third stuff",new HashSet(),new HashSet());<BR>&nbsp;&nbsp; session.save(stuff);<BR>&nbsp;&nbsp; session.save(post);<BR>&nbsp;&nbsp; post.getStuffs().add(stuff);<BR>&nbsp;&nbsp; //stuff.getPosts().add(post);/*不能与上一行并存，会有异常，因为是一个死循环？*/<BR>&nbsp;&nbsp; PostStuff postStuff = (PostStuff)session.createQuery("from PostStuff where PostId = '" + post.getPostId() + "' AND StuffId = '" + stuff.getStuffId() + "'").iterate().next();<BR>&nbsp;&nbsp; postStuff.setJoinDate(new java.util.Date());<BR>&nbsp;&nbsp; tx.commit();<BR>&nbsp;&nbsp; HibernateUtil.closeSession();<BR>&nbsp;&nbsp; ...</P>
<P>post.getStuffs().add(stuff);在两个Save()之后，而且没有调用session.flush()，但是createQuery能够得到PostStuff说明HQL是先从内存中查询数据的。不需要调用session.flush()来写入数据库，因为tx.commit()会自动调用。</P>
<P>可以用session.persist(stuff)来替换session.save(stuff)，我晕，明明save有说明会创建id，而persist没有说明会创建id的(但实际上是创建了)。</P>
<P>试图用以下替代post.getStuffs().add(stuff):<BR>&nbsp;&nbsp; PostStuff postStuff = new PostStuff();<BR>&nbsp;&nbsp; postStuff.setPost(post);<BR>&nbsp;&nbsp; postStuff.setStuff(stuff);<BR>&nbsp;&nbsp; postStuff.setJoinDate(new java.util.Date());<BR>&nbsp;&nbsp; postStuff.setComp_id(new PostStuffPK(post.getPostId(),stuff.getStuffId()));<BR>&nbsp;&nbsp; post.getPostStuffs().add(postStuff);<BR>结果是虽然没有异常，但并不会添加一条关系。</P>
<P>或者</P>
<P>&nbsp;&nbsp; session.flush();<BR>&nbsp;&nbsp; session.refresh(stuff);<BR>&nbsp;&nbsp; ((PostStuff)(stuff.getPostStuffs().iterator().next())).setJoinDate(new java.util.Date());<BR>注意这些代码只是用来作试验用的。</P><BR><BR></DIV><img src ="http://www.cnitblog.com/Raistlin/aggbug/5575.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Raistlin/" target="_blank">Raistlin</a> 2005-12-19 09:23 <a href="http://www.cnitblog.com/Raistlin/archive/2005/12/19/5575.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于java CORBA和Wf-XML2.0 的一些链接</title><link>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5571.html</link><dc:creator>Raistlin</dc:creator><author>Raistlin</author><pubDate>Mon, 19 Dec 2005 00:58:00 GMT</pubDate><guid>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5571.html</guid><wfw:comment>http://www.cnitblog.com/Raistlin/comments/5571.html</wfw:comment><comments>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5571.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/Raistlin/comments/commentRss/5571.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Raistlin/services/trackbacks/5571.html</trackback:ping><description><![CDATA[<P>Wf-XML2.0 extends ASAP<BR>ASAP use SOAP<BR>SOAP use XML</P>
<P>ASAP (Asynchronous Service Access Protocol)<BR><A href="http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=asap">http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=asap</A><BR>SOAP(<A href="http://www.w3.org/TR/soap/">http://www.w3.org/TR/soap/</A>)<BR>apache soap(<A href="http://ws.apache.org/soap/">http://ws.apache.org/soap/</A>)</P>
<P>&nbsp;<BR>GNOME CORBA简介 <A href="http://263.aka.org.cn/Lectures/002/Lecture-2.1.9/Lecture-2.1.9/Corba.html">http://263.aka.org.cn/Lectures/002/Lecture-2.1.9/Lecture-2.1.9/Corba.html</A><BR>使用JAVA开发CORBA应用 <A href="http://www.evget.com/articles/evget_424.html">http://www.evget.com/articles/evget_424.html</A><BR>CORBA 及Java IDL应用编程 <A href="http://fanqiang.chinaunix.net/program/java/2001-08-03/2178.shtml">http://fanqiang.chinaunix.net/program/java/2001-08-03/2178.shtml</A><BR>&nbsp;<BR>以上都已经out了...浪费时间，直接看下面的<BR>Java IDL <A href="http://java.sun.com/j2se/1.5.0/docs/guide/idl/index.html">http://java.sun.com/j2se/1.5.0/docs/guide/idl/index.html</A><BR>&nbsp;<BR>Naming Service <A href="http://java.sun.com/j2se/1.5.0/docs/guide/idl/jidlNaming.html#using">http://java.sun.com/j2se/1.5.0/docs/guide/idl/jidlNaming.html#using</A> 名字和对象的绑定。。。<BR>ORBD&nbsp;&nbsp;&nbsp; Transient Naming Service - tnameserv<BR>&nbsp;<BR>JDK1.5.0 chm 下载 <A href="http://www.allimant.org/">http://www.allimant.org/</A></P><img src ="http://www.cnitblog.com/Raistlin/aggbug/5571.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Raistlin/" target="_blank">Raistlin</a> 2005-12-19 08:58 <a href="http://www.cnitblog.com/Raistlin/archive/2005/12/19/5571.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate启动过慢的问题以及我的批处理</title><link>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5567.html</link><dc:creator>Raistlin</dc:creator><author>Raistlin</author><pubDate>Mon, 19 Dec 2005 00:43:00 GMT</pubDate><guid>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5567.html</guid><wfw:comment>http://www.cnitblog.com/Raistlin/comments/5567.html</wfw:comment><comments>http://www.cnitblog.com/Raistlin/archive/2005/12/19/5567.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/Raistlin/comments/commentRss/5567.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Raistlin/services/trackbacks/5567.html</trackback:ping><description><![CDATA[<P>icepeak:<BR>&nbsp;hibernate肯定没什么问题的 <BR>&nbsp;我也遇到启动很慢的情况，都是我的问题 <BR>&nbsp;你把你的映射文件发给我看看<BR>icepeak:<BR>&nbsp;&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "<A href="http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd</A>" &gt;<BR>&nbsp;你用h3，但是却写h2的配置文件，h就傻了 把2变成3就好 <BR>icepeak:<BR>&nbsp;我想你得不到id也许和映射文档有问题也有关系 <BR>&nbsp;什么都不用改，就用identity，再试试看 <BR>&nbsp;我觉得是映射文件配置错误导致了不能读取 <BR>&nbsp;就是2.0和3.0的错误 <BR>&nbsp;<BR>So...问题解决了.</P>
<P>俺用了三个批处理来自动生成hibernate的文件。<BR>&nbsp;<BR>execant.bat:<BR>d:<BR>cd D:\hibernate-3.0\middlegen-2.1\samples\<BR>rem 打开build.xml修改middlegen配置<BR>editplus.exe build.xml<BR>ant<BR>&nbsp;<BR>myant.bat:<BR>call execant.bat<BR>pause<BR>rem 复制hbm和java文件到工程目录<BR>xcopy .\build\gen-src\com\glacierbbs\hibernate\*.java E:\eclipse\workspace\JGlacier\src\com\glacierbbs\hibernate\ /Y /D<BR>xcopy .\build\gen-src\com\glacierbbs\hibernate\*.xml E:\eclipse\workspace\JGlacier\src\ /Y /D<BR>rem 替换掉hbm中的字符串<BR>call E:\eclipse\workspace\JGlacier\cutter.bat<BR>pause<BR>&nbsp;<BR>cutter.bat:<BR>e:<BR>cd E:\eclipse\workspace\JGlacier\src<BR>dir *.xml /d /b /a-d &gt;hbmfiles.txt<BR>rem 将版本2.0改成3.0<BR>for /f %%i in (hbmfiles.txt) do strrpc /s:%%i 2.0 3.0 /c<BR>rem 将hi/lo的max_lo由100改成0...我只需要简单的数字<BR>for /f %%i in (hbmfiles.txt) do strrpc /s:%%i 100 0 /c<BR>del hbmfiles.txt&gt;nul<BR>rem 所有hi/lo算法读的是同一个表中的不同字段<BR>strrpc /s:Board.hbm.xml next_value next_boardid /c<BR>strrpc /s:Post.hbm.xml next_value next_postid /c<BR>strrpc /s:Topic.hbm.xml next_value next_topicid /c<BR>strrpc /s:UserBasic.hbm.xml next_value next_userid /c<BR>strrpc /s:Vote.hbm.xml next_value next_voteid /c<BR>strrpc /s:VoteOption.hbm.xml next_value next_voteoptionid /c<BR>strrpc /s:VoteParticipant.hbm.xml next_value next_voteparticipantid /c<BR>cd ..<BR>pause<BR>&nbsp;<BR>然后，为每个hibernate生成的java类写个Adapter类...从此世界就安静了.... </P><img src ="http://www.cnitblog.com/Raistlin/aggbug/5567.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Raistlin/" target="_blank">Raistlin</a> 2005-12-19 08:43 <a href="http://www.cnitblog.com/Raistlin/archive/2005/12/19/5567.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>菜鸟学用middlegen</title><link>http://www.cnitblog.com/Raistlin/archive/2005/09/14/2653.html</link><dc:creator>Raistlin</dc:creator><author>Raistlin</author><pubDate>Wed, 14 Sep 2005 03:25:00 GMT</pubDate><guid>http://www.cnitblog.com/Raistlin/archive/2005/09/14/2653.html</guid><wfw:comment>http://www.cnitblog.com/Raistlin/comments/2653.html</wfw:comment><comments>http://www.cnitblog.com/Raistlin/archive/2005/09/14/2653.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/Raistlin/comments/commentRss/2653.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/Raistlin/services/trackbacks/2653.html</trackback:ping><description><![CDATA[<DIV>俺是java和hibernate和ant的绝对的三料菜鸟。hibernate似乎很好用，但是写表对应的类和.hbm.xml文件实在头大。所以俺想找一款可以自动生成这些文件的工具。</DIV>
<DIV><BR>俺找到了middlegen。它的主页地址是<A href="http://boss.bekk.no/boss/middlegen/index.html"><FONT color=#003399>http://boss.bekk.no/boss/middlegen/index.html</FONT></A>。我google了一些中文网页，提供的地址没有一个不失效的。-_-。光有middlegen还不够，因为它使用ant来配置它的build。它的一个bulid生成的东西就是一堆的你可以用的.java,.hbm.xml,.class,.war文件。真是够贴心的。可是俺觉得它的配置可是一点都不贴心。</DIV>
<DIV><BR>你要是没有ant，请先下载ant。俺一直打不开官方的地址，所以从pchome下载<A href="http://dlfjgwbn.pchome.net:8080/development/java/apacheant161.zip"><FONT color=#003399>http://dlfjgwbn.pchome.net:8080/development/java/apacheant161.zip</FONT></A>。解压文件后就可以用了，但等等，你需要安装了jdk，设置了CLASSPATH，JAVA_HOME。然后把ant的目录下的bin文件夹添加到path环境变量，设置ANT_HOME。<BR>我的机器上的设置：<BR>CLASSPATH&nbsp;.;D:\jdk150\lib\tools.jar;D:\jdk150\jre\lib\rt.jar<BR>JAVA_HOME&nbsp;D:\jdk150<BR>Path&nbsp;&nbsp;D:\apache-ant-1.6.1\bin;<BR>ANT_HOME&nbsp;D:\apache-ant-1.6.1</DIV>
<DIV>&nbsp;</DIV>
<DIV>然后下载并解压middlegen。我解压到了D:\hibernate-3.0\middlegen-2.1。好了，现在可以开始build hibernate需要的文件了，有个D:\hibernate-3.0\middlegen-2.1samples的文件夹，它存放了一个build的例子，我决定修改这个例子然后使用。</DIV>
<DIV>&nbsp;</DIV>
<DIV>但是再等等。middlegen需要你提供一些必要的配置。它需要连接到你的数据库，所以你需要给它提供数据库的连接信息和驱动库。各种数据库的配置信息在目录D:\hibernate-3.0\middlegen-2.1\samples\config\database 下面。俺的数据库是mssql，所以俺打开了mssql.xml文件，配置了信息如下：</DIV>
<DIV>&nbsp;&nbsp; &lt;property name="database.script.file"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value=""/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.driver.file"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="${lib.dir}/mssqlserver.jar"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.driver"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.url"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="jdbc:microsoft:sqlserver://localhost:1433"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.userid"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="sa"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.password"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="haha"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.schema"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="dbo"/&gt;<BR>&nbsp;&nbsp; &lt;property name="database.catalog"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="aljoin_info"/&gt;<BR>&nbsp;&nbsp; &lt;property name="jboss.datasource.mapping"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="MS SQLSERVER"/&gt;</DIV>
<DIV>aljoin_info是俺的数据库名称。并把msbase.jar，mssqlserver.jar，msutil.jar拷贝到D:\hibernate-3.0\middlegen-2.1\samples\lib目录。</DIV>
<DIV>然后编辑D:\hibernate-3.0\middlegen-2.1\samples\build.xml文件，将"ENTITY database SYSTEM"的值改成"file:./config/database/mssql.xml"</DIV>
<DIV>&nbsp;</DIV>
<DIV>更改这个build的其它属性。build.xml中更改project的属性:<BR>&lt;project name="Middlegen aljoni_info" default="hbm2java" basedir="."&gt;<BR>&nbsp;...<BR>&nbsp;&lt;property name="name"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value="com.aljoin.gas"/&gt;</DIV>
<DIV><BR>删除D:\hibernate-3.0\middlegen-2.1\samples\build下的所有文件。然后打开命令行，<BR>cd D:\hibernate-3.0\middlegen-2.1\samples<BR>ant<BR>这时候ant会自动读build.xml文件，找到project，运行名为"hbm2java"及其依赖的target.因为俺是菜鸟,什么ejb,jbo俺通通用不到.如果你不是菜鸟,可以设置成default="all",而且如果您的系统没有安装jboss之类的会出错,但我想如果您不是菜鸟应该也不会看到这里了hoho :p</DIV>
<DIV>&nbsp;</DIV>
<DIV>在跳出的程序界面上按generate。等待结束，然后关闭程序界面。在命令行窗口看到正在生成.java文件的信息。</DIV>
<DIV>查看D:\hibernate-3.0\middlegen-2.1\samples\build目录。上帝保佑你会看到一些你需要的文件。</DIV>
<DIV>&nbsp;</DIV>
<DIV>到写完这篇烂文，一个早上已经过去，俺又把青春贡献给了党-_-||</DIV><img src ="http://www.cnitblog.com/Raistlin/aggbug/2653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/Raistlin/" target="_blank">Raistlin</a> 2005-09-14 11:25 <a href="http://www.cnitblog.com/Raistlin/archive/2005/09/14/2653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>