In the spirit of sharing, here's my association management freemarker template (PojoPropertyAccessors.ftl). It is a bit specific to my implementation, but it might lend a few tricks.
Code:
// Property accessors
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#if pojo.hasFieldJavaDoc(property)>
/**
${pojo.getFieldJavaDoc(property, 4)}
*/
</#if>
<#include "Ejb3PropertyGetAnnotation.ftl"/>
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
return this.${property.name};
}
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
this.${property.name} = ${property.name};
}
<#if c2h.isOneToMany(property)>
<#assign inversePojo = c2j.getPOJOClass(property.value.element.associatedClass)>
<#assign inverseProperty = "">
<#if property.name?starts_with(c2j.simplePluralize(inversePojo.declarationName)?uncap_first + "For")>
<#assign potentialMatch = pojo.declarationName?uncap_first + "By" + (property.name)?substring((c2j.simplePluralize(inversePojo.declarationName) + "For")?length)>
<#else>
<#assign potentialMatch = pojo.declarationName?uncap_first>
</#if>
<#foreach field in inversePojo.getAllPropertiesIterator()>
<#if potentialMatch = field.name>
<#assign inverseProperty = field.name>
</#if>
</#foreach>
<#assign addMethodName = "addTo" + property.name?cap_first>
<#assign removeMethodName = "removeFrom" + property.name?cap_first>
<#assign inverseClassName = pojo.importType(inversePojo.qualifiedDeclarationName)>
<#assign inverseInstanceName = inverseClassName?uncap_first>
<#if inverseProperty != ""><#assign inverseSetterName = "set" + inverseProperty?cap_first></#if>
// <#if inverseProperty = "">Unidirectional<#else>Bidirectional</#if> collection management for ${property.name}
/**
* Add a ${inverseClassName} to the ${pojo.getPropertyName(property)} collection
* @param ${inverseInstanceName} the instance of ${inverseClassName} to add to the ${pojo.getPropertyName(property)} collection
* @return this ${pojo.declarationName} for method chaining
*/
${pojo.getPropertySetModifiers(property)} ${pojo.declarationName} ${addMethodName}(${inverseClassName} ${inverseInstanceName}) {
if (${inverseInstanceName} != null) {
if (${pojo.getGetterSignature(property)}() == null) {
set${pojo.getPropertyName(property)}(${pojo.getFieldInitialization(property, jdk5)});
}
${pojo.getGetterSignature(property)}().add(${inverseInstanceName});<#if inverseProperty != "">
${inverseInstanceName}.${inverseSetterName}(this);</#if>
} else {
throw new NullPointerException("Illegal attempt to add a null ${inverseClassName} to ${pojo.declarationName}");
}
return this;
}
/**
* Remove a ${inverseClassName} from the ${pojo.getPropertyName(property)} collection
* @param ${inverseInstanceName} the instance of ${inverseClassName} to remove from the ${pojo.getPropertyName(property)} collection
* @return this ${pojo.declarationName} for method chaining
*/
${pojo.getPropertyGetModifiers(property)} ${pojo.declarationName} ${removeMethodName}(${inverseClassName} ${inverseInstanceName}) {
if (${inverseInstanceName} != null) {
if (${pojo.getGetterSignature(property)}() != null) {
${pojo.getGetterSignature(property)}().remove(${inverseInstanceName});<#if inverseProperty != "">
${inverseInstanceName}.${inverseSetterName}(null);</#if>
}
} else {
throw new NullPointerException("Illegal attempt to remove a null ${inverseClassName} from ${pojo.declarationName}");
}
return this;
}
</#if>
</#if>
</#foreach>
For unidirectional associations, it generates:
Code:
// Unidirectional collection management for academicEvaluationFlags
/**
* Add a AcademicEvaluationFlag to the AcademicEvaluationFlags collection
* @param academicEvaluationFlag the instance of AcademicEvaluationFlag to add to the AcademicEvaluationFlags collection
* @return this AcademicEvaluation for method chaining
*/
public AcademicEvaluation addToAcademicEvaluationFlags(AcademicEvaluationFlag academicEvaluationFlag) {
if (academicEvaluationFlag != null) {
if (getAcademicEvaluationFlags() == null) {
setAcademicEvaluationFlags(new HashSet<AcademicEvaluationFlag>(0));
}
getAcademicEvaluationFlags().add(academicEvaluationFlag);
} else {
throw new NullPointerException("Illegal attempt to add a null AcademicEvaluationFlag to AcademicEvaluation");
}
return this;
}
/**
* Remove a AcademicEvaluationFlag from the AcademicEvaluationFlags collection
* @param academicEvaluationFlag the instance of AcademicEvaluationFlag to remove from the AcademicEvaluationFlags collection
* @return this AcademicEvaluation for method chaining
*/
public AcademicEvaluation removeFromAcademicEvaluationFlags(AcademicEvaluationFlag academicEvaluationFlag) {
if (academicEvaluationFlag != null) {
if (getAcademicEvaluationFlags() != null) {
getAcademicEvaluationFlags().remove(academicEvaluationFlag);
}
} else {
throw new NullPointerException("Illegal attempt to remove a null AcademicEvaluationFlag from AcademicEvaluation");
}
return this;
}
And for bidirectional:
Code:
// Bidirectional collection management for agencyCertifications
/**
* Add a AgencyCertification to the AgencyCertifications collection
* @param agencyCertification the instance of AgencyCertification to add to the AgencyCertifications collection
* @return this Agency for method chaining
*/
public Agency addToAgencyCertifications(AgencyCertification agencyCertification) {
if (agencyCertification != null) {
if (getAgencyCertifications() == null) {
setAgencyCertifications(new HashSet<AgencyCertification>(0));
}
getAgencyCertifications().add(agencyCertification);
agencyCertification.setAgency(this);
} else {
throw new NullPointerException("Illegal attempt to add a null AgencyCertification to Agency");
}
return this;
}
/**
* Remove a AgencyCertification from the AgencyCertifications collection
* @param agencyCertification the instance of AgencyCertification to remove from the AgencyCertifications collection
* @return this Agency for method chaining
*/
public Agency removeFromAgencyCertifications(AgencyCertification agencyCertification) {
if (agencyCertification != null) {
if (getAgencyCertifications() != null) {
getAgencyCertifications().remove(agencyCertification);
agencyCertification.setAgency(null);
}
} else {
throw new NullPointerException("Illegal attempt to remove a null AgencyCertification from Agency");
}
return this;
}