Hibernate 生成的完整构造函数包含太多参数

Hibernate 生成的完整构造函数包含太多参数,hibernate,hbm,Hibernate,Hbm,我有一个包含很多字段的DB表,当我使用Hibernate.hbm文件为该表生成POJO时,它会导致一个问题。问题是生成的完整构造函数为Java生成的参数太多,这会引发编译器错误: 参数太多,参数xxxx超出了方法参数的255个字的限制 我想通过抑制Hibernate生成完整构造函数来解决这个问题。我的问题是 如果我没有完整的构造函数,Hibernate会在运行时中断吗 我如何告诉我的hbm不要生成完整的构造函数 提前感谢您的回答。对于1:使用Hibernate 3.6(可能也适用于早期版本,但我

我有一个包含很多字段的DB表,当我使用Hibernate.hbm文件为该表生成POJO时,它会导致一个问题。问题是生成的完整构造函数为Java生成的参数太多,这会引发编译器错误:

参数太多,参数xxxx超出了方法参数的255个字的限制

我想通过抑制Hibernate生成完整构造函数来解决这个问题。我的问题是

  • 如果我没有完整的构造函数,Hibernate会在运行时中断吗
  • 我如何告诉我的hbm不要生成完整的构造函数
  • 提前感谢您的回答。

    对于1:

    使用Hibernate 3.6(可能也适用于早期版本,但我没有测试过),您可以通过创建以下文件,自定义hibernatetool代码生成以跳过创建构造函数(如果构造函数的参数超过255个):

    ${hibernate cust src}/pojo/PojoConstructors.ftl

    <#--  /** default constructor */ -->
        public ${pojo.getDeclarationName()}() {
        }
    
    <#if pojo.needsMinimalConstructor() && pojo.getPropertiesForMinimalConstructor().size() lte 255>    <#-- /** minimal constructor */ -->
        public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForMinimalConstructor(), jdk5, pojo)}) {
    <#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty() >
            super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});        
    </#if>
    <#foreach field in pojo.getPropertiesForMinimalConstructor()>
            this.${field.name} = ${field.name};
    </#foreach>
        }
    </#if>    
    <#if pojo.needsFullConstructor() && pojo.getPropertiesForFullConstructor().size() lte 255>
    <#-- /** full constructor */ -->
        public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
    <#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
            super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});        
    </#if>
    <#foreach field in pojo.getPropertiesForFullConstructor()> 
           this.${field.name} = ${field.name};
    </#foreach>
        }
    </#if>  
    
    <#--  /** default constructor */ -->
        public ${pojo.getDeclarationName()}() {
        }
    

    注意,在hibernate工具中,创建参数>255的构造函数是一个错误…

    在Java中,一个方法或构造函数不能定义超过255个参数。这是Java中的限制,Hibernate也遵循同样的策略

    由于Hibernate始终使用默认构造函数,因此最好在PojoConstructors模板中删除完整的构造函数生成

    ${hibernate cust src}/pojo/PojoConstructors.ftl

    <#--  /** default constructor */ -->
        public ${pojo.getDeclarationName()}() {
        }
    
    <#if pojo.needsMinimalConstructor() && pojo.getPropertiesForMinimalConstructor().size() lte 255>    <#-- /** minimal constructor */ -->
        public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForMinimalConstructor(), jdk5, pojo)}) {
    <#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassMinimalConstructor().isEmpty() >
            super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassMinimalConstructor())});        
    </#if>
    <#foreach field in pojo.getPropertiesForMinimalConstructor()>
            this.${field.name} = ${field.name};
    </#foreach>
        }
    </#if>    
    <#if pojo.needsFullConstructor() && pojo.getPropertiesForFullConstructor().size() lte 255>
    <#-- /** full constructor */ -->
        public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
    <#if pojo.isSubclass() && !pojo.getPropertyClosureForSuperclassFullConstructor().isEmpty()>
            super(${c2j.asArgumentList(pojo.getPropertyClosureForSuperclassFullConstructor())});        
    </#if>
    <#foreach field in pojo.getPropertiesForFullConstructor()> 
           this.${field.name} = ${field.name};
    </#foreach>
        }
    </#if>  
    
    <#--  /** default constructor */ -->
        public ${pojo.getDeclarationName()}() {
        }
    
    
    public${pojo.getDeclarationName()}(){
    }
    
    我最后做了什么-我取了一堆字段,这些字段可以作为一个单独的实体进行逻辑分组,并将它们标记为我的hbm中的一个组件。这仍然让人感觉有点不舒服,但这是一种简单的方法,可以减少Java类方法的参数数量,这样我就不到255了。