`
落花虽有意
  • 浏览: 183116 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

appfuse2 根据表生成自己想生成的pojo

阅读更多

我这里说的生成pojo是指在已经设计好数据表结构后, 通过执行 mvn appfuse:gen-model 命令根据表生成相应的pojo.

 

开始时我用的是 mysql 数据库, 设计好数据表后通过上面的命令能正确的生成所有我自己创建的表对应的 pojo, 但如果重复执行上面的命令, 系统也会按规按矩的再次重复生成所有的 pojo, 而如果我只是新加了一个表, 也会重复生成以前生成过的 pojo.

 

当然这还不是问题, 因为最后还是能得到要得到的结果.

 

后来我把数据库换成 sqlserver 2000, 这次的问题不只是上面的问题, 而且上面的命令会生成部分系统表对象的 pojo, 这显然不是我要的了, 不过这个问题其实也很好解决, 不就是多生成了几个 pojo嘛, 直接在源代码里删了就行了. 当然还是其他更"正规"点的解决办法.

 

下面就看"正规"点的解决办法:

找到生成项目的 myproject/target/test-classes/hibernate.reveng.xml 文件.

里面的内容大眼一看差不多能懂, 有定义类型映射的, 有按表名过滤表的.

原始文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
  SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

    <type-mapping>
        <!-- jdbc-type is name fom java.sql.Types -->
        <sql-type jdbc-type="VARCHAR" length='1' hibernate-type="yes_no"/>
        <!-- length, scale and precision can be used to specify the mapping precisly -->
        <sql-type jdbc-type="NUMERIC" precision='1' hibernate-type="boolean"/>
        <!-- the type-mappings are ordered. This mapping will be consulted last,
        thus overriden by the previous one if precision=1 for the column -->
        <sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long"/>
        <sql-type jdbc-type="INTEGER" hibernate-type="java.lang.Long"/>
        <sql-type jdbc-type="NUMERIC" hibernate-type="java.lang.Long"/>
    </type-mapping>

    <!-- BIN$ is recycle bin tables in Oracle -->
    <table-filter match-name="BIN$.*" exclude="true"/>

    <!-- Exclude AppFuse tables from all catalogs/schemas -->
    <!-- 按表名过滤表 -->
    <table-filter match-name="app_user" exclude="true"/>
    <table-filter match-name="role" exclude="true"/>
    <table-filter match-name="user_role" exclude="true"/>
    
</hibernate-reverse-engineering>

 

现在, 如果我要过滤掉 teacher 表 和 系统表, 则可以修改如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
  SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

	<schema-selection match-schema="dbo"/>

    <type-mapping>
        <!-- jdbc-type is name fom java.sql.Types -->
        <sql-type jdbc-type="VARCHAR" length='1' hibernate-type="yes_no"/>
        <!-- length, scale and precision can be used to specify the mapping precisly -->
        <sql-type jdbc-type="NUMERIC" precision='1' hibernate-type="boolean"/>
        <!-- the type-mappings are ordered. This mapping will be consulted last,
        thus overriden by the previous one if precision=1 for the column -->
        <sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long"/>
        <sql-type jdbc-type="INTEGER" hibernate-type="java.lang.Long"/>
        <sql-type jdbc-type="NUMERIC" hibernate-type="java.lang.Long"/>
    </type-mapping>

    <!-- BIN$ is recycle bin tables in Oracle -->
    <table-filter match-name="BIN$.*" exclude="true"/>

    <!-- Exclude AppFuse tables from all catalogs/schemas -->
    <table-filter match-name="app_user" exclude="true"/>
    <table-filter match-name="role" exclude="true"/>
    <table-filter match-name="user_role" exclude="true"/>
	<table-filter match-name="teacher" exclude="true"/>
	<table-filter match-name="dtpro.*" exclude="true"/>
	<table-filter match-name="sys.*" exclude="true"/>
    
</hibernate-reverse-engineering>

 

 

这样, 再次运行 mvn appfuse:gen-model 命令时就会过滤掉 teacher表, 以及以 dtpro 和 sys 开头的表.

还需要注意的是通配符写法.

 

这样就能生成自己想生成的 pojo了. 

 

 

1
0
分享到:
评论
2 楼 落花虽有意 2009-08-23  
teamojiao 写道
用这个能生产的基于annotation的pojo吗?  就是没有*.hbm文件的那种比如:

@Table(name="USER")
public class User{

}


这个是能的,至少我用的 2.0.2是支持的,1.x的就不知道了,
具体方法如下:
1.1 先写好带有注解的pojo,,放在 model包中
1.2 在资源文件夹中修改 hibernate.cfg.xml ,增加前面的model类映射到该文件,
如:<mapping class="com.mycompany.app.model.User"/>
    在 src/META-INF/persistence.xml 下加入实体映射,
如:<class>com.mycompany.app.model.User</class>
1.3 在该项目根目录下运行命令:mvn test-compile hibernate3:hbm2ddl

在通过其他命令生成 service  和 dao。

如果是通过数据库表生成 pojo 的话,生成出来就是 基于 annotation的,不会产生 .hbml.xml 文件。

1 楼 teamojiao 2009-08-20  
用这个能生产的基于annotation的pojo吗?  就是没有*.hbm文件的那种比如:

@Table(name="USER")
public class User{

}

相关推荐

Global site tag (gtag.js) - Google Analytics