You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Johannes <jo...@gmail.com> on 2014/12/13 07:41:27 UTC

Fwd: a setter for addToManyTarget ?

try again


-------- Weitergeleitete Nachricht --------
Betreff: a setter for addToManyTarget ?
Datum: Sat, 13 Dec 2014 07:36:55 +0100
Von: Johannes <jo...@gmail.com>
An: cayenne >> user@cayenne.apache.org

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






Re: a setter for addToManyTarget ?

Posted by Michael Gentry <mg...@masslight.net>.
In our projects, which deal with a lot of XML->Java (Cayenne Objects)
conversion, we added set* methods to the template, but they throw an
exception if data already exists.  This would allow us to catch issues in
the conversion process with trying to set related objects too many times.
I guess I'm just trying to say requirements for set* can differ, but the
template approach works well.

mrg


On Tue, Dec 16, 2014 at 9:14 AM, Mike Kienenberger <mk...@gmail.com>
wrote:
>
> Jurgen described the correct process if you want to make this change
> for all entities.
>
> I generally use ant to generate my custom templates.
>
> If you only want it for a single relationship, just add the method to
> your entity subclass directly.
>
> For set*(), I'm pretty sure you're going to want to implement it by
> calling addTo*() and removeFrom(() internally until the relationship
> list matches your argument list.
>
> Feel free to ask about any part of this if it's not clear.
>
> On Tue, Dec 16, 2014 at 2:46 AM,  <do...@xsinet.co.za> wrote:
> > 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
> > step)
> >     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)}(
> > List<$importUtils.formatJavaType(${rel.TargetEntity.ClassName})>
> 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
> > Preferences.
> >     b.  Select Templates on the left, and then click Add Template on the
> > right.
> >
> > 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 :-)
> > Regards
> > Jurgen
> >
> >
> >
> > -----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.
> >
> > mrg
> >
> >
> > On Sat, Dec 13, 2014 at 1:41 AM, Johannes <jo...@gmail.com> wrote:
> >>
> >>
> >> try again
> >>
> >>
> >> -------- Weitergeleitete Nachricht --------
> >> Betreff: a setter for addToManyTarget ?
> >> Datum: Sat, 13 Dec 2014 07:36:55 +0100
> >> Von: Johannes <jo...@gmail.com>
> >> An: cayenne >> user@cayenne.apache.org
> >>
> >> 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
> >>
> >>
> >>
> >>
> >>
> >>
> >
>

Re: a setter for addToManyTarget ?

Posted by Mike Kienenberger <mk...@gmail.com>.
Jurgen described the correct process if you want to make this change
for all entities.

I generally use ant to generate my custom templates.

If you only want it for a single relationship, just add the method to
your entity subclass directly.

For set*(), I'm pretty sure you're going to want to implement it by
calling addTo*() and removeFrom(() internally until the relationship
list matches your argument list.

Feel free to ask about any part of this if it's not clear.

On Tue, Dec 16, 2014 at 2:46 AM,  <do...@xsinet.co.za> wrote:
> 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
> step)
>     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)}(
> List<$importUtils.formatJavaType(${rel.TargetEntity.ClassName})> 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
> Preferences.
>     b.  Select Templates on the left, and then click Add Template on the
> right.
>
> 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 :-)
> Regards
> Jurgen
>
>
>
> -----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.
>
> mrg
>
>
> On Sat, Dec 13, 2014 at 1:41 AM, Johannes <jo...@gmail.com> wrote:
>>
>>
>> try again
>>
>>
>> -------- Weitergeleitete Nachricht --------
>> Betreff: a setter for addToManyTarget ?
>> Datum: Sat, 13 Dec 2014 07:36:55 +0100
>> Von: Johannes <jo...@gmail.com>
>> An: cayenne >> user@cayenne.apache.org
>>
>> 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
>>
>>
>>
>>
>>
>>
>

Re: a setter for addToManyTarget ?

Posted by do...@xsinet.co.za.
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 
step)
     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)}( 
List<$importUtils.formatJavaType(${rel.TargetEntity.ClassName})> 
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 
Preferences.
     b.  Select Templates on the left, and then click Add Template on the 
right.

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 :-)
Regards
Jurgen



-----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.

mrg


On Sat, Dec 13, 2014 at 1:41 AM, Johannes <jo...@gmail.com> wrote:
>
> try again
>
>
> -------- Weitergeleitete Nachricht --------
> Betreff: a setter for addToManyTarget ?
> Datum: Sat, 13 Dec 2014 07:36:55 +0100
> Von: Johannes <jo...@gmail.com>
> An: cayenne >> user@cayenne.apache.org
>
> 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
>
>
>
>
>
> 


Re: a setter for addToManyTarget ?

Posted by Michael Gentry <mg...@masslight.net>.
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.

mrg


On Sat, Dec 13, 2014 at 1:41 AM, Johannes <jo...@gmail.com> wrote:
>
> try again
>
>
> -------- Weitergeleitete Nachricht --------
> Betreff: a setter for addToManyTarget ?
> Datum: Sat, 13 Dec 2014 07:36:55 +0100
> Von: Johannes <jo...@gmail.com>
> An: cayenne >> user@cayenne.apache.org
>
> 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
>
>
>
>
>
>