FAQ
Hi,

I'm trying to implement a factory method pattern based on a cayenne data object.

Say for example i have a class (cayenne based) called Artist, i want to make a subclass of Artist, say ExpertArtist that implements some specific behavior.

Actually i have a big static Factory class that give me the all objects, i have a method like this:

public static Artist getArtist(String id) {

if (id == null || id.equals(""))
return null;

DataContext context = DataContext.getThreadDataContext();

Artist object = (Artist) DataObjectUtils.objectForPK(
context, Artist.class, Integer.parseInt(id));
return object;
}

Obviously i can declare ExpertArtist as an subclass of Artist.

package xxx.xxx;

public class EspertArtist extends Artist {
public String getName() {
return super.getName() + " i'am expert !!";
}
}

I've tried to instantiate an ExpertArtist, just modifying the Factory method, with no results. I don't know how to bouild the parent class calling super or something...

Obviously these are not the real classes, the actual classes are really big and this solution: just modifying the factory method is the best for me.


Thanks
Hans

Search Discussions

  • Michael Gentry at Mar 2, 2009 at 1:41 pm
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM, wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a cayenne data object.

    Say for example i have a class (cayenne based) called Artist, i want to make a subclass of Artist, say ExpertArtist that implements some specific behavior.

    Actually i have a big static Factory class that give me the all objects, i have a method like this:

    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context = DataContext.getThreadDataContext();

    Artist object = (Artist) DataObjectUtils.objectForPK(
    context, Artist.class, Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of Artist.

    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the Factory method, with no results. I don't know how to bouild the parent class calling super or something...

    Obviously these are not the real classes, the actual classes are really big and this solution: just modifying the factory method is the best for me.


    Thanks
    Hans

  • Hans at Mar 2, 2009 at 2:47 pm
    Michael,

    Thank you, i already saw it, but my intent was to make it entirely outside cayenne mappings, the problem i need to solve is just behavioral, not data based... by the way, it's not clear how to try the example using the modeler: creating an empty class with no attributes or something.

    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM, wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a cayenne
    data object.
    Say for example i have a class (cayenne based) called Artist, i want
    to make a subclass of Artist, say ExpertArtist that implements some
    specific behavior.
    Actually i have a big static Factory class that give me the all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of Artist.

    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the
    Factory method, with no results. I don't know how to bouild the parent
    class calling super or something...
    Obviously these are not the real classes, the actual classes are
    really big and this solution: just modifying the factory method is the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
  • Dave Lamy at Mar 2, 2009 at 5:27 pm
    Hey Hans--

    While I'm certain that the inheritance structure isn't data based to you, I
    imagine that Cayenne is going to HAVE to have a data value to know which
    subclass to instantiate. It's effectively going to get back a row from the
    Artist table and be asked to transform that row into an Artist object. How
    can it determine which kind? Through some sort of data analysis. Either a
    particular attribute value (ARTIST_TYPE) or via a linked table structure
    (don't think Cayenne supports that yet?). That data attribute should not be
    of particular importance to your application, however. It's just an ORM
    crutch.

    Dave
    On Mon, Mar 2, 2009 at 8:41 AM, wrote:

    Michael,

    Thank you, i already saw it, but my intent was to make it entirely outside
    cayenne mappings, the problem i need to solve is just behavioral, not data
    based... by the way, it's not clear how to try the example using the
    modeler: creating an empty class with no attributes or something.

    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM, wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a cayenne
    data object.
    Say for example i have a class (cayenne based) called Artist, i want
    to make a subclass of Artist, say ExpertArtist that implements some
    specific behavior.
    Actually i have a big static Factory class that give me the all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of Artist.

    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the
    Factory method, with no results. I don't know how to bouild the parent
    class calling super or something...
    Obviously these are not the real classes, the actual classes are
    really big and this solution: just modifying the factory method is the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
  • Hans at Mar 2, 2009 at 9:46 pm
    Dave,

    I've implemented the inheritance as the tutorial say, it worked fine, but the factory method still need to get the initial artist class to know the type, then ask cayenne again to give the right class to instantiate.

    I think there may be another way to get the object without doing this double call to objectForPK.

    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context = DataContext.getThreadDataContext();

    Artist c = (Artist) DataObjectUtils.objectForPK(context,
    Artist.class, Integer.parseInt(id));

    if (c.getTipoEnum() == EnumTipoArtist.EXPERT)
    return (Artist) DataObjectUtils.objectForPK(context,
    ExpertArtist.class, Integer.parseInt(id));
    else
    return c;
    }

    Thanks
    Hans

    ----- "Dave Lamy" <davelamy@gmail.com> escribió:
    Hey Hans--

    While I'm certain that the inheritance structure isn't data based to
    you, I
    imagine that Cayenne is going to HAVE to have a data value to know
    which
    subclass to instantiate. It's effectively going to get back a row
    from the
    Artist table and be asked to transform that row into an Artist object.
    How
    can it determine which kind? Through some sort of data analysis.
    Either a
    particular attribute value (ARTIST_TYPE) or via a linked table
    structure
    (don't think Cayenne supports that yet?). That data attribute should
    not be
    of particular importance to your application, however. It's just an
    ORM
    crutch.

    Dave
    On Mon, Mar 2, 2009 at 8:41 AM, wrote:

    Michael,

    Thank you, i already saw it, but my intent was to make it entirely outside
    cayenne mappings, the problem i need to solve is just behavioral, not data
    based... by the way, it's not clear how to try the example using the
    modeler: creating an empty class with no attributes or something.

    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM, wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a
    cayenne
    data object.
    Say for example i have a class (cayenne based) called Artist, i
    want
    to make a subclass of Artist, say ExpertArtist that implements
    some
    specific behavior.
    Actually i have a big static Factory class that give me the all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of Artist.

    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the
    Factory method, with no results. I don't know how to bouild the
    parent
    class calling super or something...
    Obviously these are not the real classes, the actual classes
    are
    really big and this solution: just modifying the factory method is
    the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
  • Hans at Mar 13, 2009 at 2:05 pm
    List,

    I'm answering myself, but may be of help to somebody.

    Finally i have found the problem: The Qualifier (or Discriminator) has to be of type character and not Integer.

    ...my first intent was to use integer in order to follow our local policies for enumerations.

    Hans


    ----- hans@welinux.cl escribió:
    Dave,

    I've implemented the inheritance as the tutorial say, it worked fine,
    but the factory method still need to get the initial artist class to
    know the type, then ask cayenne again to give the right class to
    instantiate.

    I think there may be another way to get the object without doing this
    double call to objectForPK.

    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context = DataContext.getThreadDataContext();

    Artist c = (Artist) DataObjectUtils.objectForPK(context,
    Artist.class, Integer.parseInt(id));

    if (c.getTipoEnum() == EnumTipoArtist.EXPERT)
    return (Artist) DataObjectUtils.objectForPK(context,
    ExpertArtist.class, Integer.parseInt(id));
    else
    return c;
    }

    Thanks
    Hans

    ----- "Dave Lamy" <davelamy@gmail.com> escribió:
    Hey Hans--

    While I'm certain that the inheritance structure isn't data based to
    you, I
    imagine that Cayenne is going to HAVE to have a data value to know
    which
    subclass to instantiate. It's effectively going to get back a row
    from the
    Artist table and be asked to transform that row into an Artist object.
    How
    can it determine which kind? Through some sort of data analysis.
    Either a
    particular attribute value (ARTIST_TYPE) or via a linked table
    structure
    (don't think Cayenne supports that yet?). That data attribute should
    not be
    of particular importance to your application, however. It's just an
    ORM
    crutch.

    Dave
    On Mon, Mar 2, 2009 at 8:41 AM, wrote:

    Michael,

    Thank you, i already saw it, but my intent was to make it
    entirely
    outside
    cayenne mappings, the problem i need to solve is just behavioral, not data
    based... by the way, it's not clear how to try the example using the
    modeler: creating an empty class with no attributes or something.

    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM, wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a
    cayenne
    data object.
    Say for example i have a class (cayenne based) called Artist,
    i
    want
    to make a subclass of Artist, say ExpertArtist that implements
    some
    specific behavior.
    Actually i have a big static Factory class that give me the
    all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of
    Artist.
    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the
    Factory method, with no results. I don't know how to bouild the
    parent
    class calling super or something...
    Obviously these are not the real classes, the actual classes
    are
    really big and this solution: just modifying the factory method
    is
    the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
  • Michael Gentry at Mar 13, 2009 at 2:33 pm
    That doesn't sound right to me ... I'm pretty sure you can use an
    integer and I also believe you can use an enum, too. I vaguely recall
    testing that at one point.

    On Fri, Mar 13, 2009 at 9:59 AM, wrote:
    List,

    I'm answering myself, but may be of help to somebody.

    Finally i have found the problem: The Qualifier (or Discriminator) has to be of type character and not Integer.

    ...my first intent was to use integer in order to follow our local policies for enumerations.

    Hans


    ----- hans@welinux.cl escribió:
    Dave,

    I've implemented the inheritance as the tutorial say, it worked fine,
    but the factory method still need to get the initial artist class to
    know the type, then ask cayenne again to give the right class to
    instantiate.

    I think there may be another way to get the object without doing this
    double call to objectForPK.

    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context = DataContext.getThreadDataContext();

    Artist c = (Artist) DataObjectUtils.objectForPK(context,
    Artist.class, Integer.parseInt(id));

    if (c.getTipoEnum() == EnumTipoArtist.EXPERT)
    return (Artist) DataObjectUtils.objectForPK(context,
    ExpertArtist.class, Integer.parseInt(id));
    else
    return c;
    }

    Thanks
    Hans

    ----- "Dave Lamy" <davelamy@gmail.com> escribió:
    Hey Hans--

    While I'm certain that the inheritance structure isn't data based to
    you, I
    imagine that Cayenne is going to HAVE to have a data value to know
    which
    subclass to instantiate.  It's effectively going to get back a row
    from the
    Artist table and be asked to transform that row into an Artist object.
    How
    can it determine which kind?  Through some sort of data analysis.
    Either a
    particular attribute value (ARTIST_TYPE) or via a linked table
    structure
    (don't think Cayenne supports that yet?).  That data attribute should
    not be
    of particular importance to your application, however.  It's just an
    ORM
    crutch.

    Dave
    On Mon, Mar 2, 2009 at 8:41 AM, wrote:

    Michael,

    Thank you, i already saw it, but my intent was to make it
    entirely
    outside
    cayenne mappings, the problem i need to solve is just behavioral, not data
    based... by the way, it's not clear how to try the example using the
    modeler: creating an empty class with no attributes or something.

    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM,  wrote:
    Hi,

    I'm trying to implement a factory method pattern based on a
    cayenne
    data object.
    Say for example i have a class (cayenne based) called Artist,
    i
    want
    to make a subclass of Artist, say ExpertArtist that implements
    some
    specific behavior.
    Actually i have a big static Factory class that give me the
    all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of
    Artist.
    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying the
    Factory method, with no results. I don't know how to bouild the
    parent
    class calling super or something...
    Obviously these are not the real classes, the actual classes
    are
    really big and this solution: just modifying the factory method
    is
    the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
  • Hans at Mar 13, 2009 at 2:49 pm
    Michael,

    May be the cayenne version, i forgot to mention i'm using cayenne-server-3.0M4.jar, because i needed the characteristics of this version.

    The fact is that making the change from integer to char made it work.

    Thanks
    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    That doesn't sound right to me ... I'm pretty sure you can use an
    integer and I also believe you can use an enum, too. I vaguely
    recall
    testing that at one point.

    On Fri, Mar 13, 2009 at 9:59 AM, wrote:
    List,

    I'm answering myself, but may be of help to somebody.

    Finally i have found the problem: The Qualifier (or Discriminator)
    has to be of type character and not Integer.
    ...my first intent was to use integer in order to follow our local
    policies for enumerations.
    Hans


    ----- hans@welinux.cl escribió:
    Dave,

    I've implemented the inheritance as the tutorial say, it worked
    fine,
    but the factory method still need to get the initial artist class
    to
    know the type, then ask cayenne again to give the right class to
    instantiate.

    I think there may be another way to get the object without doing
    this
    double call to objectForPK.

    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist c = (Artist)
    DataObjectUtils.objectForPK(context,
    Artist.class, Integer.parseInt(id));

    if (c.getTipoEnum() == EnumTipoArtist.EXPERT)
    return (Artist)
    DataObjectUtils.objectForPK(context,
    ExpertArtist.class,
    Integer.parseInt(id));
    else
    return c;
    }

    Thanks
    Hans

    ----- "Dave Lamy" <davelamy@gmail.com> escribió:
    Hey Hans--

    While I'm certain that the inheritance structure isn't data
    based
    to
    you, I
    imagine that Cayenne is going to HAVE to have a data value to
    know
    which
    subclass to instantiate.  It's effectively going to get back a
    row
    from the
    Artist table and be asked to transform that row into an Artist object.
    How
    can it determine which kind?  Through some sort of data
    analysis.
    Either a
    particular attribute value (ARTIST_TYPE) or via a linked table
    structure
    (don't think Cayenne supports that yet?).  That data attribute should
    not be
    of particular importance to your application, however.  It's
    just
    an
    ORM
    crutch.

    Dave
    On Mon, Mar 2, 2009 at 8:41 AM, wrote:

    Michael,

    Thank you, i already saw it, but my intent was to make it
    entirely
    outside
    cayenne mappings, the problem i need to solve is just
    behavioral,
    not data
    based... by the way, it's not clear how to try the example
    using
    the
    modeler: creating an empty class with no attributes or
    something.
    Hans

    ----- "Michael Gentry" <mgentry@masslight.net> escribió:
    Is this what you are after?

    http://cayenne.apache.org/doc/handling-inheritance.html

    On Sun, Mar 1, 2009 at 7:46 PM,  wrote:
    Hi,

    I'm trying to implement a factory method pattern based on
    a
    cayenne
    data object.
    Say for example i have a class (cayenne based) called
    Artist,
    i
    want
    to make a subclass of Artist, say ExpertArtist that
    implements
    some
    specific behavior.
    Actually i have a big static Factory class that give me
    the
    all
    objects, i have a method like this:
    public static Artist getArtist(String id) {

    if (id == null || id.equals(""))
    return null;

    DataContext context =
    DataContext.getThreadDataContext();
    Artist object = (Artist)
    DataObjectUtils.objectForPK(
    context, Artist.class,
    Integer.parseInt(id));
    return object;
    }

    Obviously i can declare ExpertArtist as an subclass of
    Artist.
    package xxx.xxx;

    public class EspertArtist extends Artist {
    public String getName() {
    return super.getName() + " i'am expert !!";
    }
    }

    I've tried to instantiate an ExpertArtist, just modifying
    the
    Factory method, with no results. I don't know how to bouild
    the
    parent
    class calling super or something...
    Obviously these are not the real classes, the actual
    classes
    are
    really big and this solution: just modifying the factory
    method
    is
    the
    best for me.

    Thanks
    Hans

    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago
    --
    Hans Poo, WeLinux S.A.
    Oficina: 697.25.42, Celular: 09-319.93.05
    Bombero Ossa # 1010, Santiago

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedMar 2, '09 at 12:52a
activeMar 13, '09 at 2:49p
posts8
users3
websitecayenne.apache.org

3 users in discussion

Hans: 5 posts Michael Gentry: 2 posts Dave Lamy: 1 post

People

Translate

site design / logo © 2022 Grokbase