3

I have a DB table with many, many fields that is causing an issue when I generate a POJO for that table using a Hibernate .hbm file. The issue is the full constructor being generated produces too many params for Java, which throws compiler error:

Too many parameters, parameter xxxx is exceeding the limit of 255 words eligible for method parameters

I'd like to get around this by suppressing the generation of the full constructor by Hibernate. My question is

  1. Would Hibernate break at runtime if I don't have the full constructor?
  2. How can I tell my hbm not to produce the full constructor?

Thanks in advance for any answers.

WhyGeeEx
  • 459
  • 1
  • 5
  • 19
  • What I ended up doing - I took a bunch of fields that could be logically grouped together as a separate entity, and labeled them as a component within my hbm. It still feels a little hacky, but it's an easy way to just reduce the number of params to the Java class method so I'm under 255. – WhyGeeEx Sep 12 '11 at 16:15

3 Answers3

7

With Hibernate 3.6 (may work with earlier versions as well, but I've not tested that), you can customise the hibernatetool code generation to skip creating constructors if they'd have more than 255 parameters, by creating the following file:

${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>  

this overwrites the PojoConstructors.ftl in the hibernate-tools.jar.

If you're using Ant to build, you'll need to ensure that the ${hibernate-cust-src} is in the classpath for the hibernate-tools task.

<path id="toolslib">
    <pathelement location="${hibernate-cust-src}"/>
    ... [other locations for hibernate-tools and dependencies]
</path>

<taskdef name="hibernatetool" 
         classname="org.hibernate.tool.ant.HibernateToolTask" 
         classpathref="toolslib"/>

Note, IMHO it's a bug in hibernate tools to create a constructor with >255 params...

beny23
  • 34,390
  • 5
  • 82
  • 85
2

for 1.: Hibernate needs only a empty private constructor

Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
0

In Java you can't define more than 255 pararmeters for a method or constructor. This is the restriction in Java.And Hibernate also following same strategy.

As Hibernate always use default constructor,then it better to remove full constructor generation in the PojoConstructors template.

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

<#--  /** default constructor */ -->
    public ${pojo.getDeclarationName()}() {
    }
Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38