Generated full constructor contains too many params
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 eli开发者_StackOverflow社区gible for method parameters
I'd like to get around this by suppressing the generation of the full constructor by Hibernate. My question is
- Would Hibernate break at runtime if I don't have the full constructor?
- How can I tell my hbm not to produce the full constructor?
Thanks in advance for any answers.
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...
for 1.: Hibernate needs only a empty private constructor
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()}() {
}
精彩评论