-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Generate add method for collections
PostPosted: Tue Aug 26, 2008 5:30 pm 
Newbie

Joined: Fri Jun 02, 2006 2:49 pm
Posts: 13
I am trying to get hibernate tools to generate an addXXX method for collections.

The method is suppose to look like this, with bar being the parent(this) and food the child(collection). The following would be in the Bar class

Code:
public void addFoo(Foo foo)
{
     foo.setBar(this);
     this.foos.add(foo);
}


So I got this in the PojoPropertyAccessrs.ftl template

Code:
${pojo.getPropertySetModifiers(property)} void add${pojo.getPropertyName(property)}(Object object)
{
    object.set${pojo.getDeclarationName()}(this);
    this.${property.name}.add(object);
}


Now I am stuck on how to get the rest of the information.

1) how do I get the generic type of the collection so I can replace Object with Foo.


Top
 Profile  
 
 Post subject: Re: Generate add method for collections
PostPosted: Tue Aug 26, 2008 5:47 pm 
Newbie

Joined: Fri Jun 02, 2006 2:49 pm
Posts: 13
After some looking at freemarker documentation I managed to get the generic type by parsing the string of the java type. It's not pretty but it works. I Really would like something that does not rely on string parsing. I am worried about gotchas, as I just picked this up today and know nothing about freemarker or hibernate tools.

For anyone interested this is what I came up with. add this below the setter in PojoPropertyAccessor.ftl

Code:
<#if c2h.isCollection(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)>
    ${pojo.getPropertySetModifiers(property)} void add${pojo.getPropertyName(property)}(${genericType} object)
    {
        object.set${pojo.getDeclarationName()}(this);
       this.${property.name}.add(object);
     }
</#if>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 04, 2008 12:26 pm 
Newbie

Joined: Thu Sep 04, 2008 11:08 am
Posts: 4
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]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 9:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well depends on what you will use to decide which side should be in control.

You can look at cascade or the isInverse values.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.