cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@welinux.cl
Subject Re: Factory method subclassing cayenne data objects
Date Mon, 02 Mar 2009 21:40:09 GMT
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, <hans@welinux.cl> 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,  <hans@welinux.cl> 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


Mime
View raw message