【问题标题】:HIbernate reverse engineering configuration eager loadingHIbernate 逆向工程配置预加载
【发布时间】:2015-08-11 23:25:02
【问题描述】:

我找不到任何关于如何配置 Hibernate 逆向工程工具(在 Eclipse 上)以定义域类属性的解决方案fetch = FetchType.EAGER

PS:默认所有外键定义fetch = FetchType.LAZY

例如,我在 reveng.xml 中定义了它,它适用于主键:

<table  name="fin_expence">
    <primary-key>
        <generator class="identity"></generator>
    </primary-key>
</table>

我需要这样的解决方案。

<foreign-key constraint-name="rel_expence_to_tag">
    <set  lazy="false"></set>
</foreign-key>

我已经测试了这段代码,但它没有工作,它抛出了一个错误。任何人都可以帮忙吗?谢谢。

【问题讨论】:

    标签: reverse-engineering hibernate-mapping hibernate-4.x hibernate-tools hibernate-onetomany


    【解决方案1】:

    Hibnerate 逆向工程师流程使用 Ejb3PropertyGetAnnotation.ftl 模板为 POJO 中的 getter 生成注释。 如果你看到这个文件,它看起来像

    <#if ejb3>
    <#if pojo.hasIdentifierProperty()>
    <#if property.equals(clazz.identifierProperty)>
     ${pojo.generateAnnIdGenerator()}
    <#-- if this is the id property (getter)-->
    <#-- explicitly set the column name for this property-->
    </#if>
    </#if>
    
    <#if c2h.isOneToOne(property)>
    ${pojo.generateOneToOneAnnotation(property, cfg)}
    <#elseif c2h.isManyToOne(property)>
    ${pojo.generateManyToOneAnnotation(property)}
    <#--TODO support optional and targetEntity-->    
    ${pojo.generateJoinColumnsAnnotation(property, cfg)}
    <#elseif c2h.isCollection(property)>
    ${pojo.generateCollectionAnnotation(property, cfg)}
    <#else>
    ${pojo.generateBasicAnnotation(property)}
    ${pojo.generateAnnColumnAnnotation(property)}
    </#if>
    </#if>
    

    根据关系类型,调用实际Pojo类的api。

    注意:有两种类型的 Pojo 类,即实体和组件 Pojo 类。 在您的情况下,您可以扩展这两个类并编写自己的注释实现。

    【讨论】:

      【解决方案2】:

      我已经通过使用查找和替换任务解决了这个问题。下面是 beanvalidationtask,它将替换惰性到渴望在逆向工程过程中的所有关系。我认为这会对你有所帮助。

      public class BeanValidationTask {
      
          private static final Logger LOGGER = LoggerFactory
                  .getLogger(BeanValidationTask.class);
      
          private final String MANY_TO_ONE_EAGER = "ManyToOne(fetch=FetchType.EAGER";
          private final String MANY_TO_ONE_LAZY = "ManyToOne(fetch=FetchType.LAZY";
          private final String ONE_TO_ONE_LAZY_CHILD_REFERENCE = "@OneToOne(fetch=FetchType.LAZY)";
          private final String ONE_TO_ONE_EAGER_CHILD_REFERENCE = "@OneToOne(fetch=FetchType.EAGER) @PrimaryKeyJoinColumn";
          private final String ONE_TO_ONE_LAZY_PARENT_REFERENCE = "@OneToOne(fetch=FetchType.EAGER,";
          private final String ONE_TO_ONE_LAZY_TRANSIENT_PARENT_REFERENCE = "@Transient @OneToOne(fetch=FetchType.EAGER,";
          private final String ONE_TO_MANY = "OneToMany(fetch=FetchType.LAZY";
          private final String ONE_TO_MANY_CASCADE = "OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}";
          private final String PRIMARY_KEY_JOIN_COLUMN_IMPORT = "javax.persistence.PrimaryKeyJoinColumn;";
          private final String IMPORT = "import ";
          private File searchDirectory;
      
          public BeanValidationTask(File searchDirectory) {
              this.searchDirectory = searchDirectory;
          }
      
          /**
           * It changes the below fetch type condition to EAGER.
           *
           * @ManyToOne(fetch=FetchType.LAZY to @ManyToOne(fetch=FetchType.EAGER , cascade = {CascadeType.ALL}
           * @OneToOne(fetch=FetchType.LAZY to @OneToOne(fetch=FetchType.EAGER
           */
      
          private String changeLazyToEagerAndCascade(String content) {
              try {
                  content = StringUtils.replace(content, MANY_TO_ONE_LAZY, MANY_TO_ONE_EAGER);
                  content = addImportStatement(content,PRIMARY_KEY_JOIN_COLUMN_IMPORT);
                  content = StringUtils.replace(content, ONE_TO_ONE_LAZY_CHILD_REFERENCE, ONE_TO_ONE_EAGER_CHILD_REFERENCE);
                  content = StringUtils.replace(content, ONE_TO_ONE_LAZY_PARENT_REFERENCE, ONE_TO_ONE_LAZY_TRANSIENT_PARENT_REFERENCE);
                  if (!content.contains(ONE_TO_MANY_CASCADE)) {
                      content = StringUtils.replace(content, ONE_TO_MANY, ONE_TO_MANY_CASCADE);
                  }
                  return content;
              } catch (Exception ex) {
                  LOGGER.error("Change fetch type from LAZY to EAGER failed for relationship 1-1 and 1-M in file {} at path{}. Root cause {}", ExceptionUtils.getRootCauseMessage(ex));
                  return "";
              }
          }
          private String addImportStatement(String content, final String importStatement) {
              final StringBuffer  importString = new StringBuffer(IMPORT).append(importStatement).append("\n").append(IMPORT);
              content = StringUtils.replaceOnce(content, IMPORT, importString.toString());
              return content;
          }
      
          public void execute() {
              String[] extensions = new String[]{"java"};
              if (searchDirectory.exists()) {
                  Collection<File> filesInDir = FileUtils.listFiles(searchDirectory, extensions, false);
                  for (File file : filesInDir)
                      try {
                          String content = WMFileUtils.readFileToString(file);
                          content = this.changeLazyToEagerAndCascade(content);
                          WMFileUtils.writeStringToFile(file, content);
                      } catch (IOException e) {
                          LOGGER.error("Failed to add/update validation into java class file", file.getAbsolutePath(), ExceptionUtils.getRootCauseMessage(e));
                      }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-15
        • 2016-06-18
        • 1970-01-01
        • 2015-09-27
        • 2015-02-11
        • 2014-06-25
        • 2011-11-17
        • 2011-07-24
        相关资源
        最近更新 更多