FAQ
try again


-------- Weitergeleitete Nachricht --------
Betreff: a setter for addToManyTarget ?
Datum: Sat, 13 Dec 2014 07:36:55 +0100
Von: Johannes <jotpe.osm@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

Search Discussions

  • Michael Gentry at Dec 15, 2014 at 2:23 pm
    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 wrote:

    try again


    -------- Weitergeleitete Nachricht --------
    Betreff: a setter for addToManyTarget ?
    Datum: Sat, 13 Dec 2014 07:36:55 +0100
    Von: Johannes <jotpe.osm@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




  • Dollj at Dec 16, 2014 at 7:48 am
    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 wrote:

    try again


    -------- Weitergeleitete Nachricht --------
    Betreff: a setter for addToManyTarget ?
    Datum: Sat, 13 Dec 2014 07:36:55 +0100
    Von: Johannes <jotpe.osm@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




  • Mike Kienenberger at Dec 16, 2014 at 2:17 pm
    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, 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 wrote:


    try again


    -------- Weitergeleitete Nachricht --------
    Betreff: a setter for addToManyTarget ?
    Datum: Sat, 13 Dec 2014 07:36:55 +0100
    Von: Johannes <jotpe.osm@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




  • Michael Gentry at Dec 16, 2014 at 2:48 pm
    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 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, 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 wrote:


    try again


    -------- Weitergeleitete Nachricht --------
    Betreff: a setter for addToManyTarget ?
    Datum: Sat, 13 Dec 2014 07:36:55 +0100
    Von: Johannes <jotpe.osm@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




Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedDec 13, '14 at 6:39a
activeDec 16, '14 at 2:48p
posts5
users4
websitecayenne.apache.org

People

Translate

site design / logo © 2021 Grokbase