You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Alexander Saint Croix <sa...@gmail.com> on 2008/01/13 03:04:51 UTC

Pinaki's blog entry on persisting a generic fields using @Type annotations

Pinaki,

Before anything else, I'm very sorry about the typo in your name in my last
post.  :-(

I pulled down the source for OpenJPA and dug through looking for the
@interface Type, and found it.

The file org.apache.openjpa.persistence.Type in the defines the annotation
@interface that was seen in Pinaki's blog about generic persistence in
OpenJPA.  Specifically this part:

@Entity
> @DataStoreId
> public class Edge<T> implements Serializable {
>   @OneToOne
>   @Type(Entity.class)
>   private T source;
>
>   @OneToOne
>   @Type(Entity.class)
>   private T target;
>
>   @PersistentMap
>   private Map<String,String> attributes;
>

Although @Type is not a spec-compliant annotation, the ability to persist
generic field types is utterly irresistable, and I'm willing to make OpenJPA
the only persistence provider for my project in the short to medium term.

The reason is pretty simple.  I _originally_ implemented these classes to
use generics, then had to back out of those APIs when I sat down to try to
persist everything.  OpenJPA lets me actually write my code the way I
originally intended, and so, far from being a sort of lock-in nuisance, it's
very liberating.  (I'd still like to see generic field persistence in future
versions of the JPA spec!)

This is such a promising feature.  My app will benefit immensely from the
ability to persist generic fields, so I thank you. (Now I just need to get
the proof-of-concept persisting!)

Hope you have a pleasant weekend, sir.

Best regards,
--
Alexander R. Saint Croix












On Jan 12, 2008 6:07 PM, Alexander Saint Croix <sa...@gmail.com>
wrote:
> Panaki,
>
> I read your blog entry on persisting a generic graph and am very
interested in using something similar to this for some of my entities.
>
> The pattern I'm looking to do is something like the following:
>
>
> > @MappedSuperclass
> > public abstract class Foo implements Serializable {
> >     @Id
> >     private long id;
> >
> >     public long getId() { return id; }
> >     public void setId(long id) { this.id = id; }
> >
> >     public void doStuff() {
> >         // stuff.  very abstract, to be sure.
> >     }
> > }
> >
> > @Entity
> > public class ConcreteFoo extends Foo {
> >     public void doConcreteStuff() {
> >         // more concrete stuff
> >     }
> > }
> >
> > @Entity
> > public class OtherFoo extends Foo {
> >     public void doOtherStuff() {
> >         // more concrete stuff
> >     }
> > }
> >
> > @Entity
> > public class Funk<F extends Foo> {
> >     private F foo;
> >
> >     public F getFoo() { return foo; }
> >
> >     public void setFoo(F foo) { this.foo = foo; }
> > }
> >
>
> The idea would be to allow client code to create "new Funk<ConcreteFoo>()"
and work with the class in a simpler fashion.  (think Foo == Party,
ConcreteFoo == Person, OtherFoo == Organization and Funk ==
PartyRelationship for more of a sense what I'm going for here.)
>
> Does this make sense and is it possible?  In your blog entry you mention
the @Type annotation.  Can't find references to it anywhere so thought I'd
ask the expert.
>
> Cheers!
> --
> Alexander R. Saint Croix
>
>

Re: Pinaki's blog entry on persisting a generic fields using @Type annotations

Posted by Alexander Saint Croix <sa...@gmail.com>.
Thanks, man.
--
Alex


On Jan 13, 2008 1:47 PM, Pinaki Poddar <pp...@apache.org> wrote:

>
>
> > First, I found I had to use LEFT JOIN FETCH for some of the objects in
> > order
> > to get collection references in another class.
>
> Multi-cardinality relations are LAZY by default. Check @FetchGroup
> capability of OpenJPA to see how you can control sophisticated fetch
> strategies.
>
> > By using OpenJPA specific annotations, will I need to run
> post-compilation
> enhancement of my entity classes?
>
> No.
>
> > Is there a plugin for IDEA that would obviate this?
> Don't know.
>
> --
> View this message in context:
> http://www.nabble.com/Pinaki%27s-blog-entry-on-persisting-a-generic-fields-using-%40Type-annotations-tp14781436p14790116.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Pinaki's blog entry on persisting a generic fields using @Type annotations

Posted by Pinaki Poddar <pp...@apache.org>.

> First, I found I had to use LEFT JOIN FETCH for some of the objects in
> order
> to get collection references in another class.  

Multi-cardinality relations are LAZY by default. Check @FetchGroup
capability of OpenJPA to see how you can control sophisticated fetch
strategies.

> By using OpenJPA specific annotations, will I need to run post-compilation
enhancement of my entity classes?

No.

> Is there a plugin for IDEA that would obviate this?
Don't know.

-- 
View this message in context: http://www.nabble.com/Pinaki%27s-blog-entry-on-persisting-a-generic-fields-using-%40Type-annotations-tp14781436p14790116.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Pinaki's blog entry on persisting a generic fields using @Type annotations

Posted by Alexander Saint Croix <sa...@gmail.com>.
Thanks for the reply, Pinaki!

That is the idea behind 'transparent' persistence where domain models are
> not impacted in no (or minimal) way by the fact that the model instances
> are
> persistent.
>


> @Type annotation will let you model without strong-typing the relations.
> But
> the caveat is query on the relations' path will now be restricted.
> However,
> the programming paradigm of JPA-like technology *should* discourage query
> on
> relations path in favor of navigational traversal from 'root' instances
> located by EntityManager.find(). And if that practice is followed, @Type
> can
> be a powerful mechanics for generic model.
>

Right now I'm doing my CRUD and cascade tests using embedded OpenEJB.  I
have three questions--one general question and two about using the @Type
annotation as you did in your awesome blog entry.

First, I found I had to use LEFT JOIN FETCH for some of the objects in order
to get collection references in another class.  However, I was under the
impression that eager loading was the default setting.  Is this a mistaken
assumption?

Second, I am not presently required to run a post-compilation enhancement of
my entities because of (I believe) the java agent that OpenEJB provides.
This might be a question for them, so I'll cc the openejb-users list.  By
using OpenJPA specific annotations, will I need to run post-compilation
enhancement of my entity classes and thus complicate my build somewhat?

Third, the non-spec compliant annotations are (obviously) not recognized by
my IDE, IntelliJ IDEA, which messes up the IDE a bit.  I'd like to keep the
IDE hints about JPA aspects, but.  Is there a plugin for IDEA that would
obviate this?  I'll play around with it.  I actually run my builds from M2,
so this is a minor issue compared with #2.




>
> > I read your blog entry on persisting a generic graph and am very
> > interested
> > in using something similar to this for some of my entities.
>
>   Glad that my blog ramblings could point to some ideas for your
> application design.
>
> > Before anything else, I'm very sorry about the typo in your name in my
> > last post.  :-(
>
>   my name has experienced being morphed in very many ways :)  One that
> really broke my heart was 'Pinki' :(
> --
> View this message in context:
> http://www.nabble.com/Pinaki%27s-blog-entry-on-persisting-a-generic-fields-using-%40Type-annotations-tp14781436p14789844.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Pinaki's blog entry on persisting a generic fields using @Type annotations

Posted by Pinaki Poddar <pp...@apache.org>.
Hi Alexander,

> OpenJPA lets me actually write my code the way I originally intended

That is the idea behind 'transparent' persistence where domain models are
not impacted in no (or minimal) way by the fact that the model instances are
persistent.   

@Type annotation will let you model without strong-typing the relations. But
the caveat is query on the relations' path will now be restricted. However,
the programming paradigm of JPA-like technology *should* discourage query on
relations path in favor of navigational traversal from 'root' instances
located by EntityManager.find(). And if that practice is followed, @Type can
be a powerful mechanics for generic model.

> I read your blog entry on persisting a generic graph and am very
> interested
> in using something similar to this for some of my entities. 

   Glad that my blog ramblings could point to some ideas for your
application design. 

> Before anything else, I'm very sorry about the typo in your name in my
> last post.  :-(

   my name has experienced being morphed in very many ways :)  One that
really broke my heart was 'Pinki' :(
-- 
View this message in context: http://www.nabble.com/Pinaki%27s-blog-entry-on-persisting-a-generic-fields-using-%40Type-annotations-tp14781436p14789844.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.