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)); 
       } 
     } 
    } 
}