I am refreshing this thread with some additional code. I am left with the problem of detecting if I should output a set or a add method for the other object (explanation below).
Problem is that there are cases when you have many-to-many associations and many-to-one associations. Difference between those two is that you want to generate for the first one:
Code:
public void addBar(Foo object) {
object.addFoo(this);
this.bars.add(object);
}
And for the other one:
Code:
public void addBar(Foo object) {
object.setFoo(this); // Difference is here
this.bars.add(object);
}
Any idea on how to accomplish this detection in Freemarker?
Here is my current template. The code is just improved in order to get rid of the trailing 's' of the collection name.
Code:
<#if c2h.isCollection(property)>
<#assign propertyName = pojo.getPropertyName(property)>
<#assign javaType = pojo.getJavaTypeName(property, jdk5)>
<#assign start = javaType?index_of("<") + 1>
<#assign end = javaType?last_index_of(">")>
<#assign genericType = javaType?substring(start,end)>
<#if propertyName.endsWith("s")>
<#assign singularName = pojo.getPropertyName(property)?substring(0,propertyName.length()-1)>
<#else>
<#assign singularName = propertyName>
</#if>
${pojo.getPropertySetModifiers(property)} void add${singularName}(${genericType} object)
{
object.set${pojo.getDeclarationName()}(this);
this.${property.name}.add(object);
}
</#if>
[/code]