cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
Subject Re: a setter for addToManyTarget ?
Date Tue, 16 Dec 2014 07:46:59 GMT
Hi Johannes

Although this functionality isn't built into Cayenne there is a possible way 
to do it.
I must first say though that I've not done this myself yet, and so this is 
merely theory from my side.

Cayenne uses templates to generate the classes, so the idea is to modify the 
template so that a method like *setCategories(List<Category>)* is generated.
It'll do this for ALL your relationships though.

The process involves 3 steps:

1.  Modify the template (this is the most complicated and time consuming 
     a.  Look inside the CayenneModeler.jar file, go to the /templates/v1_2 
folder, and copy the appropriate superclass vm file from there.
     b.  Rename the vm file you copied.
     c.  Open your vm file and edit the section that handles relationships 
to also generate the method you want. You'll start with something like:

public void set${stringUtils.capitalized($rel.Name)}( 
changeList )
    //  and then put your code here

2.  Add the template to Cayenne Modeler:
     a.  Open Cayenne Modeler and select Tools from the menu, and then 
     b.  Select Templates on the left, and then click Add Template on the 

3.  Using the template in the Modeler:
     a.  Select Tools from the menu, and then Generate Classses.
     b.  Change Type to Advanced.
     c.  Change Superclass Template to use yours.

Should be fun :-)

-----Original Message----- 
From: Michael Gentry
Sent: Monday, December 15, 2014 4:22 PM
To: Cayenne Users
Subject: Re: a setter for addToManyTarget ?

Hi Johannes,

I'm not aware of one built into Cayenne that does this yet.  I know I've
had to write similar functionality a couple times in the past, too.


On Sat, Dec 13, 2014 at 1:41 AM, Johannes <> wrote:
> try again
> -------- Weitergeleitete Nachricht --------
> Betreff: a setter for addToManyTarget ?
> Datum: Sat, 13 Dec 2014 07:36:55 +0100
> Von: Johannes <>
> An: cayenne >>
> Hello,
> my object model has 2 toMany relationships. Cayenne Modeler generated
> next to addToManyTarget(String, DataObject, boolean) methods like
> addToCategories(Category) or addToTags(Tag) for me. Fine.
> In some use cases it would be wonderful to call a method like
> *setCategories(List<Category>)*, which would add only new and remove
> only missing Categories. Existing Categories aren't touch of course.
> This would save much work, because the setter is written by me multiple
> times in different projects.
> Or does such a method exist today?
> Best regards Johannes

View raw message