【问题标题】:Hibernate Reverse Engineering using DelegatingReverseEngineeringStrategyHibernate 逆向工程使用 DelegatingReverseEngineeringStrategy
【发布时间】:2015-02-11 18:58:23
【问题描述】:

我正在尝试使用 DelegatingReverseEngineeringStrategy 生成 pojos 和 hbm 文件。我能够自定义诸如实现接口、toString 方法、急切获取所有表对象之类的东西。

不过,我还需要自定义两个功能:

考虑两个表 Parent 和 Child,其中 Parent 和 Child 之间存在一对多关系。

我想:

  1. 为父 hbm 中的子集合设置 inverse="false"

  2. 在父 hbm 中为子集合设置 cascade="all",这样如果我更新父集合,它应该将效果级联到子集合。

    <hibernate-mapping>
     <class name="com.xyz.Parent" table="PARENT" schema="FAMILY">
      <meta attribute="implements" inherit="false">SomeInterface</meta>
      <meta attribute="extra-import" inherit="false">com.xyz.SomeInterface</meta>
       <property name="parentColumn" type="date">
        <meta attribute="use-in-tostring" inherit="false">true</meta>
        <column name="PARENT_COLUMN" length="7" />
      </property>
     <set name="child" table="Child" **inverse="false"** lazy="false" fetch="select" **cascade="all"**>
     <key>
       ....
      </key>
     <one-to-many class="com.xyz.Child" />
     </set>
    </class>
    </hibernate-mapping>
    
  3. 在 Child 的 hbm 中将 Parent 作为外键排除 - 以避免在代码中反向查找。

    <hibernate-mapping>
     <class name="com.xyz.Child" table="CHILD" schema="FAMILY">
      <meta attribute="implements" inherit="false">SomeInterface</meta>
      <meta attribute="extra-import" inherit="false">com.xyz.SomeInterface</meta> 
      <property name="childColumn" type="date">
        <meta attribute="use-in-tostring" inherit="false">true</meta>  
        <column name="CHILD_COLUMN" length="7" />
      </property>
    </composite-id>
    **-- I do not want this in CHILD 
    <many-to-one name="parent" class="com.xyz.Parent" update="false" insert="false" fetch="select">
    <meta attribute="use-in-tostring" inherit="false">true</meta>
    ....
    </many-to-one>**
    </class>
    </hibernate-mapping>
    

有没有办法在 DelegatingReverseEngineeringStrategy 中找到关联信息?一些可以为每个表提供一对多、一对一等信息的类。

【问题讨论】:

    标签: hibernate reverse-engineering hibernate-tools hbm


    【解决方案1】:

    似乎可以通过覆盖 DelegatingReverseEngineeringStrategy.foreignKeyToAssociationInfo(ForeignKey) 但是在实体创建构建期间似乎根本没有调用该函数:(

    可以在构建完成后通过 maven 进行正则表达式替换。有点像 查找

    @ManyToOne\((.*)\)[\r\n\s]+@JoinColumn\((.*)\)[\r\n\s]+public Entity getEntity\(\)
    

    替换为

    @ManyToOne($1, cascade=javax.persistence.CascadeType.ALL)\r\n\t@JoinColumn($2, updatable=false, insertable=false)\r\n\tpublic Entity getEntity()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 2016-06-18
      • 1970-01-01
      • 2021-05-14
      • 2015-09-27
      • 1970-01-01
      • 2011-03-13
      • 2012-03-10
      相关资源
      最近更新 更多