生成方案以DB-->hbm.xml-->Pojo为例
ant的build文件
[b]问题[/b]
1.org.hibernate.cfg.JDBCBinderException: Duplicate class name 'com.kurui.kums._entity.Countries' generated for 'org.hibernate.mapping.Table(SH.COUNTRIES)'. Same name where generated for 'org.hibernate.mapping.Table(SH.COUNTRIES)'
Oracle下见此问题,在hibernate.properties里加上hibernate.default_schema=username 即可
2.表较多的情况下,可否将生成的hbm.xml和Pojo指定到不同的目录下?
3.关于使用模版。
实际需求中往往还需要为PoJo生成的过程中定义一些规则(首字母、前缀等、自定义代码等),网上资料看都提到了使用模版,如下:
<!--运用模板生成自己想要的类-->
生成pojo类,其实hbm2java,hbm2dao都是hbmtemplate的一个简化版本,可以用宏理解
其中hbm2java等价于:但是此处的模板位置都是在heibernate-tool.jar中。如果需要自定义模板,则需要明确编写hbmtemplate。
或者是
<hibernatetool destdir="${build.dir}/generated">
<classpath/>
<configuration configurationfile="hibernate.cfg.xml"/>
<!-- 用户模板 -->
<hbmtemplate exporterclass="my.own.Exporter" filepattern="."/>
</hibernatetool>
我的问题是,Pojo.ftl和exporterclass如何定义、如何引用,可有案例可供参阅?
原文链接:[url]https://riboseyim.github.io?source=iteye&pid=hibernate[/url]
[img]http://o8m8ngokc.bkt.clouddn.com/qrcode_for_gh_896dd3dd5255_344.jpg[/img]
ant的build文件
<?xml version="1.0" encoding="UTF-8" ?>
<project name="kums-Hibernate" basedir="." default="hibernatecode">
<property name="build.classpath" location="D:\jdk1.5.0_03" />
<property name="src.dir" location="../../src" />
<property name="build.dir" location="${app.dir}/WEB-INF/classes" />
<property name="lib.dir" location="${app.dir}/WEB-INF/lib" />
<!-- 引用的库 -->
<path id="toolslib">
<path location="lib/hibernate-tools.jar" />
<path location="lib/hibernate3.jar" />
<path location="lib/commons-logging.jar" />
<path location="lib/dom4j-1.6.1.jar" />
<path location="lib/ojdbc14.jar" />
<path location="lib/commons-collections-3.2.1.jar" />
<path location="lib/freemarker.jar" />
<path location="lib/jtidy-r8-20060801.jar" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib">
</taskdef>
<target name="hibernatecode">
<hibernatetool destdir="${src.dir}/generated">
<jdbcconfiguration propertyfile="${src.dir}/hibernate.properties" packagename="com.kurui.kums._entity" />
<!--由DB导出hbm.xml文件-->
<hbm2hbmxml />
<!-- 由hbm.xml文件生成POJO文件 -->
<hbm2java jdk5="true" />
<!-- 生成配置文件 -->
<hbm2cfgxml ejb3="false" />
<!-- 生成的dao -->
<hbm2dao />
</hibernatetool>
</target>
</project>
[b]问题[/b]
1.org.hibernate.cfg.JDBCBinderException: Duplicate class name 'com.kurui.kums._entity.Countries' generated for 'org.hibernate.mapping.Table(SH.COUNTRIES)'. Same name where generated for 'org.hibernate.mapping.Table(SH.COUNTRIES)'
Oracle下见此问题,在hibernate.properties里加上hibernate.default_schema=username 即可
2.表较多的情况下,可否将生成的hbm.xml和Pojo指定到不同的目录下?
3.关于使用模版。
实际需求中往往还需要为PoJo生成的过程中定义一些规则(首字母、前缀等、自定义代码等),网上资料看都提到了使用模版,如下:
<!--运用模板生成自己想要的类-->
<hbmtemplate templatepath="template" template="dao/daohome"
filepattern="{package-name}/{class-name}Dao.java">
<property key="ejb3" value="false"/>
<property key="jdk5" value="true"/>
</hbmtemplate>
生成pojo类,其实hbm2java,hbm2dao都是hbmtemplate的一个简化版本,可以用宏理解
其中hbm2java等价于:但是此处的模板位置都是在heibernate-tool.jar中。如果需要自定义模板,则需要明确编写hbmtemplate。
<hbmtemplate templateprefix="pojo/" template="pojo/Pojo.ftl"
filepattern="{package-name}/{class-name}.java">
<property key="jdk5" value="true" />
<property key="ejb3" value="true" />
</hbmtemplate>
或者是
<hibernatetool destdir="${build.dir}/generated">
<classpath/>
<configuration configurationfile="hibernate.cfg.xml"/>
<!-- 用户模板 -->
<hbmtemplate exporterclass="my.own.Exporter" filepattern="."/>
</hibernatetool>
我的问题是,Pojo.ftl和exporterclass如何定义、如何引用,可有案例可供参阅?
原文链接:[url]https://riboseyim.github.io?source=iteye&pid=hibernate[/url]
[img]http://o8m8ngokc.bkt.clouddn.com/qrcode_for_gh_896dd3dd5255_344.jpg[/img]