1

我無法找到任何有關如何配置Hibernate逆向工程工具(在Eclipse上)以定義域類屬性的解決方案fetch = FetchType.EAGERHIbernate逆向工程配置急切加載

PS:默認情況下,所有外鍵定義取= 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> 

我測試了這段代碼,它沒有工作,它拋出了一個錯誤。誰能幫忙?謝謝。

回答

1

Hibnerate逆向工程過程中使用Ejb3PropertyGetAnnotation.ftl模板來生成對POJO干將註解。 如果您看到此文件,它看起來像

<#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類。 你的情況,你也可以再延長兩班,寫自己的註解實現。

0

我以發現解決了這個問題,並更換task.Below是將取代懶惰渴望逆向工程process.I所有關係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)); 
       } 
     } 
    } 
}