You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Laird Nelson <lj...@gmail.com> on 2009/12/07 17:29:11 UTC

MappedSuperclasses?

Hello; I asked this question before and got crickets.

What is the best way to customize a ReverseMappingTool run such that for
every table mapped I create a MappedSuperclass that it inherits from?

I am using OpenJPA 1.2.1 and generating annotations, not XML.

So if I have a database table named Person, then I want a
PersonMappedSuperclass.java and a Person.java class that inherits from it.

I am so close it is maddening, but cannot cross the final chasm.

The first thing I do is to set my ReverseCustomizer to change the incoming
ClassMapping to setEmbeddedOnly(true), and rename it to
xyzMappedSuperclass.  Then I generate a new class, set the MappedSuperclass
as its parent, and install the new mapping directly into the repository,
like so (inside customize(ClassMapping)):

    mapping.setEmbeddedOnly(true);

    final Class<?> mappingClass = mapping.getDescribedType();
    final String mappingClassName = mappingClass.getName();

    final String newClassName = mappingClassName.substring(0,
mappingClassName.length() - "MappedSuperclass".length());
    final Class<?> newClass = this.tool.generateClass(newClassName,
mappingClass);

    final MappingRepository mappingRepository = this.tool.getRepository();

    final ClassMapping newMapping =
(ClassMapping)mappingRepository.addMetaData(newClass);
    newMapping.setPCSuperclass(mappingClass);
    newMapping.setTable(mapping.getTable()); // ? maybe?
    newMapping.setEmbeddedOnly(false);

This does more or less what I want: at the end of this run, I get a mapped
superclass and an entity class that extends from it.  However, none of the
mapped superclass field information is filled in.  That is, I get things
like:

    @Basic

Instead of

    @Basic
    @Column(name="pmt_no", length=16)

Bug 1360 seems to be related to this (
http://issues.apache.org/jira/browse/OPENJPA-1360).  It would appear that in
the 1.2.x line, anyhow, it is impossible to create a MappedSuperclass whose
field annotations are properly filled in, thus of course defeating the
entire purpose of a MappedSuperclass.  I would be interested to learn if
ANYONE has ever generated MappedSuperclasses using this tool.

Best,
Laird

Re: MappedSuperclasses?

Posted by ljnelson <lj...@gmail.com>.
On Wed, Dec 9, 2009 at 11:00 PM, Pinaki Poddar [via OpenJPA] <
ml-node+4143599-1967080018@n2.nabble.com<ml...@n2.nabble.com>
> wrote:

> > It is used both to determine whether a class should be an @Embedded, and
> it is also used to determine > whether a class should be a @MappedSuperclass
> (!).
>
> You are exactly right. And that is one of the reasons why in OpenJPA 2.0
> there is a distinction via ClassMetaData.isAbstract() -- which in
> persistence world implies @MappedSuperclass.
>

OK; thanks.  Can't move to 2.0 just yet.  In the meantime I've got it
working.


> I am not familiar with the connotation of the term 'cricket'  (being born
> in a place where major activity is to be glued on a TV set to watch a game
> similar to baseball and loved by englishmen during past century, the word
> 'cricket' means some protracted, useless activity to me:), but from your
> usage I see you were not pleased with the responses on your earlier quest on
> the similar topic.
>

"Hearing crickets" is what happens when you're in a big, empty, otherwise
silent place, far from any sign of group activity.  :-) :-) :-)  But now
there is activity....


> But with reverse mapping tool and pre-OpenJPA 2.0 and MappedSuperclass you
> may be in that difficult place where there is only 'cricket' -- in my sense
> of the term ;)
>

:-) Thanks for the help.

Best,
Laird

-- 
View this message in context: http://n2.nabble.com/MappedSuperclasses-tp4126924p4145197.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: MappedSuperclasses?

Posted by Pinaki Poddar <pp...@apache.org>.
> It is used both to determine whether a class should be an @Embedded, and it
is also used to determine > whether a class should be a @MappedSuperclass
(!). 

You are exactly right. And that is one of the reasons why in OpenJPA 2.0
there is a distinction via ClassMetaData.isAbstract() -- which in
persistence world implies @MappedSuperclass.

I am not familiar with the connotation of the term 'cricket'  (being born in
a place where major activity is to be glued on a TV set to watch a game
similar to baseball and loved by englishmen during past century, the word
'cricket' means some protracted, useless activity to me:), but from your
usage I see you were not pleased with the responses on your earlier quest on
the similar topic.  

But with reverse mapping tool and pre-OpenJPA 2.0 and MappedSuperclass you
may be in that difficult place where there is only 'cricket' -- in my sense
of the term ;)
  


ljnelson wrote:
> 
> Sure, Kevin; that's a fair answer.
> 
> I know that the root issue lies in the double-purposing of the
> ClassMetaData.isEmbeddedOnly() method.  It is used both to determine
> whether
> a class should be an @Embedded, and it is also used to determine whether a
> class should be a @MappedSuperclass (!).  I understand that the latter
> condition has been solved by the addition of the isAbstract() method in
> OpenJPA 2.0.x.
> 
> Anyhow, this isn't by itself all that bad.
> 
> What may be bad is that there are several calls made to
> AnnotationPersistenceMappingSerializer#isMappingMode(ClassMetaData), and
> its
> default behavior is to consider whether the passed in ClassMetaData is
> embedded or not.  This drives, among other things, whether field mappings
> retain all their attributes (like name), but unfortunately it also drives
> whether mapped superclasses get an @Entity or a @MappedSuperclass tag.
> 
> So if you set a ClassMapping#isEmbedded() one way, you get mapped
> superclasses correctly annotated as @MappedSuperclass, but all the field
> annotations are empty.  If you set it the other way, you get all the field
> annotations populated, but the class gets an @Entity tag.  See bug 1360
> for
> more details there.
> 
> The way that I've almost solved the problem--but boy does it feel
> wrong!--is
> to install my own subclass of AnnotationPersistenceMappingSerializer so
> that
> isEmbeddedOnly() is simply not considered in the implementation of
> isMappingMode(ClassMetaData).  This is just fine--the only remaining
> hurdle
> that I think I'm hearing is probably not going to be fixed anytime soon
> unless I wade in with a hammer and try to fix it myself :-) is that the
> @MappedSuperclass gets a @Table annotation.
> 
> Thanks for the non-silence :-)
> 
> Best,
> Laird
> 
> On Mon, Dec 7, 2009 at 12:45 PM, Kevin Sutter [via OpenJPA] <
> ml-node+4127373-1333037552@n2.nabble.com<ml...@n2.nabble.com>
>> wrote:
> 
>> Hi Laird,
>> I'm not much better than a cricket with this particular question, but I
>> have
>> a couple of observations...
>>
>> Since you are attempting to affect the execution of the
>> ReverseMappingTool,
>>
>> you may be a little beyond basic usage questions.  Thus, I'm going to
>> cross-copy this question over the dev mailing list.  Not guaranteeing
>> activity because of this since most of us monitor both mailing lists. 
>> But,
>>
>> it won't hurt in this case...
>>
>> One thing to keep in mind is that the ReverseMappingTool is not meant to
>> be
>>
>> the end-all tool for generating the Entity classes.  It is expected that
>> tweaking of the generated code will be necessary to make your Entities
>> "production ready".  Now, if the generated code is not syntactically
>> correct
>> or complete, then that's another issue.  But, personally, I wouldn't
>> expect
>>
>> the generated code to be "complete".
>>
>> If you continue to only get crickets responding to the request, then this
>> may indicate a lack of resource or even a lack of expertise on your
>> particular question.  It may also be an excellent opportunity for you to
>> roll up your sleeves, debug the problem, and contribute a solution back
>> to
>> the community.  We are always looking for new blood.  :-)
>>
>> Good luck,
>> Kevin
>>
>>
>> On Mon, Dec 7, 2009 at 10:29 AM, Laird Nelson <[hidden
>> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=4127373&i=0>>
>> wrote:
>>
>> > Hello; I asked this question before and got crickets.
>> >
>> > What is the best way to customize a ReverseMappingTool run such that
>> for
>> > every table mapped I create a MappedSuperclass that it inherits from?
>> >
>> > I am using OpenJPA 1.2.1 and generating annotations, not XML.
>> >
>> > So if I have a database table named Person, then I want a
>> > PersonMappedSuperclass.java and a Person.java class that inherits from
>> it.
>> >
>> > I am so close it is maddening, but cannot cross the final chasm.
>> >
>> > The first thing I do is to set my ReverseCustomizer to change the
>> incoming
>> > ClassMapping to setEmbeddedOnly(true), and rename it to
>> > xyzMappedSuperclass.  Then I generate a new class, set the
>> MappedSuperclass
>> > as its parent, and install the new mapping directly into the
>> repository,
>> > like so (inside customize(ClassMapping)):
>> >
>> >    mapping.setEmbeddedOnly(true);
>> >
>> >    final Class<?> mappingClass = mapping.getDescribedType();
>> >    final String mappingClassName = mappingClass.getName();
>> >
>> >    final String newClassName = mappingClassName.substring(0,
>> > mappingClassName.length() - "MappedSuperclass".length());
>> >    final Class<?> newClass = this.tool.generateClass(newClassName,
>> > mappingClass);
>> >
>> >    final MappingRepository mappingRepository =
>> this.tool.getRepository();
>>
>> >
>> >    final ClassMapping newMapping =
>> > (ClassMapping)mappingRepository.addMetaData(newClass);
>> >    newMapping.setPCSuperclass(mappingClass);
>> >    newMapping.setTable(mapping.getTable()); // ? maybe?
>> >    newMapping.setEmbeddedOnly(false);
>> >
>> > This does more or less what I want: at the end of this run, I get a
>> mapped
>> > superclass and an entity class that extends from it.  However, none of
>> the
>> > mapped superclass field information is filled in.  That is, I get
>> things
>> > like:
>> >
>> >    @Basic
>> >
>> > Instead of
>> >
>> >    @Basic
>> >    @Column(name="pmt_no", length=16)
>> >
>> > Bug 1360 seems to be related to this (
>> > http://issues.apache.org/jira/browse/OPENJPA-1360).  It would appear
>> that
>> > in
>> > the 1.2.x line, anyhow, it is impossible to create a MappedSuperclass
>> whose
>> > field annotations are properly filled in, thus of course defeating the
>> > entire purpose of a MappedSuperclass.  I would be interested to learn
>> if
>> > ANYONE has ever generated MappedSuperclasses using this tool.
>> >
>> > Best,
>> > Laird
>> >
>>
>>
>> ------------------------------
>>  View message @
>> http://n2.nabble.com/MappedSuperclasses-tp4126924p4127373.html
>> To start a new topic under OpenJPA Users, email
>> ml-node+208411-553807638@n2.nabble.com<ml...@n2.nabble.com>
>> To unsubscribe from OpenJPA Users, click here< (link removed) =>.
>>
>>
>>
> 
> 


-----
Pinaki 
-- 
View this message in context: http://n2.nabble.com/MappedSuperclasses-tp4126924p4143599.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: MappedSuperclasses?

Posted by ljnelson <lj...@gmail.com>.
Sure, Kevin; that's a fair answer.

I know that the root issue lies in the double-purposing of the
ClassMetaData.isEmbeddedOnly() method.  It is used both to determine whether
a class should be an @Embedded, and it is also used to determine whether a
class should be a @MappedSuperclass (!).  I understand that the latter
condition has been solved by the addition of the isAbstract() method in
OpenJPA 2.0.x.

Anyhow, this isn't by itself all that bad.

What may be bad is that there are several calls made to
AnnotationPersistenceMappingSerializer#isMappingMode(ClassMetaData), and its
default behavior is to consider whether the passed in ClassMetaData is
embedded or not.  This drives, among other things, whether field mappings
retain all their attributes (like name), but unfortunately it also drives
whether mapped superclasses get an @Entity or a @MappedSuperclass tag.

So if you set a ClassMapping#isEmbedded() one way, you get mapped
superclasses correctly annotated as @MappedSuperclass, but all the field
annotations are empty.  If you set it the other way, you get all the field
annotations populated, but the class gets an @Entity tag.  See bug 1360 for
more details there.

The way that I've almost solved the problem--but boy does it feel wrong!--is
to install my own subclass of AnnotationPersistenceMappingSerializer so that
isEmbeddedOnly() is simply not considered in the implementation of
isMappingMode(ClassMetaData).  This is just fine--the only remaining hurdle
that I think I'm hearing is probably not going to be fixed anytime soon
unless I wade in with a hammer and try to fix it myself :-) is that the
@MappedSuperclass gets a @Table annotation.

Thanks for the non-silence :-)

Best,
Laird

On Mon, Dec 7, 2009 at 12:45 PM, Kevin Sutter [via OpenJPA] <
ml-node+4127373-1333037552@n2.nabble.com<ml...@n2.nabble.com>
> wrote:

> Hi Laird,
> I'm not much better than a cricket with this particular question, but I
> have
> a couple of observations...
>
> Since you are attempting to affect the execution of the ReverseMappingTool,
>
> you may be a little beyond basic usage questions.  Thus, I'm going to
> cross-copy this question over the dev mailing list.  Not guaranteeing
> activity because of this since most of us monitor both mailing lists.  But,
>
> it won't hurt in this case...
>
> One thing to keep in mind is that the ReverseMappingTool is not meant to be
>
> the end-all tool for generating the Entity classes.  It is expected that
> tweaking of the generated code will be necessary to make your Entities
> "production ready".  Now, if the generated code is not syntactically
> correct
> or complete, then that's another issue.  But, personally, I wouldn't expect
>
> the generated code to be "complete".
>
> If you continue to only get crickets responding to the request, then this
> may indicate a lack of resource or even a lack of expertise on your
> particular question.  It may also be an excellent opportunity for you to
> roll up your sleeves, debug the problem, and contribute a solution back to
> the community.  We are always looking for new blood.  :-)
>
> Good luck,
> Kevin
>
>
> On Mon, Dec 7, 2009 at 10:29 AM, Laird Nelson <[hidden email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=4127373&i=0>>
> wrote:
>
> > Hello; I asked this question before and got crickets.
> >
> > What is the best way to customize a ReverseMappingTool run such that for
> > every table mapped I create a MappedSuperclass that it inherits from?
> >
> > I am using OpenJPA 1.2.1 and generating annotations, not XML.
> >
> > So if I have a database table named Person, then I want a
> > PersonMappedSuperclass.java and a Person.java class that inherits from
> it.
> >
> > I am so close it is maddening, but cannot cross the final chasm.
> >
> > The first thing I do is to set my ReverseCustomizer to change the
> incoming
> > ClassMapping to setEmbeddedOnly(true), and rename it to
> > xyzMappedSuperclass.  Then I generate a new class, set the
> MappedSuperclass
> > as its parent, and install the new mapping directly into the repository,
> > like so (inside customize(ClassMapping)):
> >
> >    mapping.setEmbeddedOnly(true);
> >
> >    final Class<?> mappingClass = mapping.getDescribedType();
> >    final String mappingClassName = mappingClass.getName();
> >
> >    final String newClassName = mappingClassName.substring(0,
> > mappingClassName.length() - "MappedSuperclass".length());
> >    final Class<?> newClass = this.tool.generateClass(newClassName,
> > mappingClass);
> >
> >    final MappingRepository mappingRepository = this.tool.getRepository();
>
> >
> >    final ClassMapping newMapping =
> > (ClassMapping)mappingRepository.addMetaData(newClass);
> >    newMapping.setPCSuperclass(mappingClass);
> >    newMapping.setTable(mapping.getTable()); // ? maybe?
> >    newMapping.setEmbeddedOnly(false);
> >
> > This does more or less what I want: at the end of this run, I get a
> mapped
> > superclass and an entity class that extends from it.  However, none of
> the
> > mapped superclass field information is filled in.  That is, I get things
> > like:
> >
> >    @Basic
> >
> > Instead of
> >
> >    @Basic
> >    @Column(name="pmt_no", length=16)
> >
> > Bug 1360 seems to be related to this (
> > http://issues.apache.org/jira/browse/OPENJPA-1360).  It would appear
> that
> > in
> > the 1.2.x line, anyhow, it is impossible to create a MappedSuperclass
> whose
> > field annotations are properly filled in, thus of course defeating the
> > entire purpose of a MappedSuperclass.  I would be interested to learn if
> > ANYONE has ever generated MappedSuperclasses using this tool.
> >
> > Best,
> > Laird
> >
>
>
> ------------------------------
>  View message @
> http://n2.nabble.com/MappedSuperclasses-tp4126924p4127373.html
> To start a new topic under OpenJPA Users, email
> ml-node+208411-553807638@n2.nabble.com<ml...@n2.nabble.com>
> To unsubscribe from OpenJPA Users, click here< (link removed) =>.
>
>
>

-- 
View this message in context: http://n2.nabble.com/MappedSuperclasses-tp4126924p4127860.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: MappedSuperclasses?

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Laird,
I'm not much better than a cricket with this particular question, but I have
a couple of observations...

Since you are attempting to affect the execution of the ReverseMappingTool,
you may be a little beyond basic usage questions.  Thus, I'm going to
cross-copy this question over the dev mailing list.  Not guaranteeing
activity because of this since most of us monitor both mailing lists.  But,
it won't hurt in this case...

One thing to keep in mind is that the ReverseMappingTool is not meant to be
the end-all tool for generating the Entity classes.  It is expected that
tweaking of the generated code will be necessary to make your Entities
"production ready".  Now, if the generated code is not syntactically correct
or complete, then that's another issue.  But, personally, I wouldn't expect
the generated code to be "complete".

If you continue to only get crickets responding to the request, then this
may indicate a lack of resource or even a lack of expertise on your
particular question.  It may also be an excellent opportunity for you to
roll up your sleeves, debug the problem, and contribute a solution back to
the community.  We are always looking for new blood.  :-)

Good luck,
Kevin


On Mon, Dec 7, 2009 at 10:29 AM, Laird Nelson <lj...@gmail.com> wrote:

> Hello; I asked this question before and got crickets.
>
> What is the best way to customize a ReverseMappingTool run such that for
> every table mapped I create a MappedSuperclass that it inherits from?
>
> I am using OpenJPA 1.2.1 and generating annotations, not XML.
>
> So if I have a database table named Person, then I want a
> PersonMappedSuperclass.java and a Person.java class that inherits from it.
>
> I am so close it is maddening, but cannot cross the final chasm.
>
> The first thing I do is to set my ReverseCustomizer to change the incoming
> ClassMapping to setEmbeddedOnly(true), and rename it to
> xyzMappedSuperclass.  Then I generate a new class, set the MappedSuperclass
> as its parent, and install the new mapping directly into the repository,
> like so (inside customize(ClassMapping)):
>
>    mapping.setEmbeddedOnly(true);
>
>    final Class<?> mappingClass = mapping.getDescribedType();
>    final String mappingClassName = mappingClass.getName();
>
>    final String newClassName = mappingClassName.substring(0,
> mappingClassName.length() - "MappedSuperclass".length());
>    final Class<?> newClass = this.tool.generateClass(newClassName,
> mappingClass);
>
>    final MappingRepository mappingRepository = this.tool.getRepository();
>
>    final ClassMapping newMapping =
> (ClassMapping)mappingRepository.addMetaData(newClass);
>    newMapping.setPCSuperclass(mappingClass);
>    newMapping.setTable(mapping.getTable()); // ? maybe?
>    newMapping.setEmbeddedOnly(false);
>
> This does more or less what I want: at the end of this run, I get a mapped
> superclass and an entity class that extends from it.  However, none of the
> mapped superclass field information is filled in.  That is, I get things
> like:
>
>    @Basic
>
> Instead of
>
>    @Basic
>    @Column(name="pmt_no", length=16)
>
> Bug 1360 seems to be related to this (
> http://issues.apache.org/jira/browse/OPENJPA-1360).  It would appear that
> in
> the 1.2.x line, anyhow, it is impossible to create a MappedSuperclass whose
> field annotations are properly filled in, thus of course defeating the
> entire purpose of a MappedSuperclass.  I would be interested to learn if
> ANYONE has ever generated MappedSuperclasses using this tool.
>
> Best,
> Laird
>

Re: MappedSuperclasses?

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Laird,
I'm not much better than a cricket with this particular question, but I have
a couple of observations...

Since you are attempting to affect the execution of the ReverseMappingTool,
you may be a little beyond basic usage questions.  Thus, I'm going to
cross-copy this question over the dev mailing list.  Not guaranteeing
activity because of this since most of us monitor both mailing lists.  But,
it won't hurt in this case...

One thing to keep in mind is that the ReverseMappingTool is not meant to be
the end-all tool for generating the Entity classes.  It is expected that
tweaking of the generated code will be necessary to make your Entities
"production ready".  Now, if the generated code is not syntactically correct
or complete, then that's another issue.  But, personally, I wouldn't expect
the generated code to be "complete".

If you continue to only get crickets responding to the request, then this
may indicate a lack of resource or even a lack of expertise on your
particular question.  It may also be an excellent opportunity for you to
roll up your sleeves, debug the problem, and contribute a solution back to
the community.  We are always looking for new blood.  :-)

Good luck,
Kevin


On Mon, Dec 7, 2009 at 10:29 AM, Laird Nelson <lj...@gmail.com> wrote:

> Hello; I asked this question before and got crickets.
>
> What is the best way to customize a ReverseMappingTool run such that for
> every table mapped I create a MappedSuperclass that it inherits from?
>
> I am using OpenJPA 1.2.1 and generating annotations, not XML.
>
> So if I have a database table named Person, then I want a
> PersonMappedSuperclass.java and a Person.java class that inherits from it.
>
> I am so close it is maddening, but cannot cross the final chasm.
>
> The first thing I do is to set my ReverseCustomizer to change the incoming
> ClassMapping to setEmbeddedOnly(true), and rename it to
> xyzMappedSuperclass.  Then I generate a new class, set the MappedSuperclass
> as its parent, and install the new mapping directly into the repository,
> like so (inside customize(ClassMapping)):
>
>    mapping.setEmbeddedOnly(true);
>
>    final Class<?> mappingClass = mapping.getDescribedType();
>    final String mappingClassName = mappingClass.getName();
>
>    final String newClassName = mappingClassName.substring(0,
> mappingClassName.length() - "MappedSuperclass".length());
>    final Class<?> newClass = this.tool.generateClass(newClassName,
> mappingClass);
>
>    final MappingRepository mappingRepository = this.tool.getRepository();
>
>    final ClassMapping newMapping =
> (ClassMapping)mappingRepository.addMetaData(newClass);
>    newMapping.setPCSuperclass(mappingClass);
>    newMapping.setTable(mapping.getTable()); // ? maybe?
>    newMapping.setEmbeddedOnly(false);
>
> This does more or less what I want: at the end of this run, I get a mapped
> superclass and an entity class that extends from it.  However, none of the
> mapped superclass field information is filled in.  That is, I get things
> like:
>
>    @Basic
>
> Instead of
>
>    @Basic
>    @Column(name="pmt_no", length=16)
>
> Bug 1360 seems to be related to this (
> http://issues.apache.org/jira/browse/OPENJPA-1360).  It would appear that
> in
> the 1.2.x line, anyhow, it is impossible to create a MappedSuperclass whose
> field annotations are properly filled in, thus of course defeating the
> entire purpose of a MappedSuperclass.  I would be interested to learn if
> ANYONE has ever generated MappedSuperclasses using this tool.
>
> Best,
> Laird
>