openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dick <michael.d.d...@gmail.com>
Subject Re: Why does the enhancer create a PROTECTE no-args constructor by default?
Date Fri, 04 Dec 2009 21:54:19 GMT
Hi David,

The JVM will create a no-arg constructor if you have no other constructors.
The spec requires a no-arg constructor so that the persistence provider can
create an empty instance. OpenJPA uses it via some tricky generated code
(bytecode insertion).

As for why it's protected instead of public I presume the original devs
thought it was safer to restrict access to the generated constructor. It
isn't documented in the manual, but you could go diving in PCEnhancer.java
(which generates the constructor) and see if there are any comments that
shed some light on the rationale.

-mike

On Fri, Dec 4, 2009 at 2:55 PM, KARR, DAVID (ATTCINW) <dk068x@att.com>wrote:

> > -----Original Message-----
> > From: Rick Curtis [mailto:curtisr7@gmail.com]
> > Sent: Friday, December 04, 2009 12:31 PM
> > To: users@openjpa.apache.org
> > Subject: Re: Why does the enhancer create a PROTECTE no-args
> > constructor by default?
> >
> > David-
> >
> > Per the JPA spec --
> >
> > The entity class must have a no-arg constructor. The entity class may
> > have
> > other constructors as well.
> > The no-arg constructor must be public or protected.
> >
> > ...so if your Entity doesn't have one, OpenJPA will do it for you.
>
> Ok, I'll phrase the question again, and perhaps I can make it more
> obvious this time.
>
> Why does the enhancer create a PROTECTED no-args constructor, and not a
> PUBLIC one?
>
> If it was going to create a PUBLIC constructor, there'd be absolutely no
> point to this, because that's exactly what the JVM does.  If the
> enhancer had a good reason to create a PROTECTED constructor instead of
> a PUBLIC one, then I can see why the enhancer would do this, but I still
> see no obvious reason to create a PROTECTED constructor.
>
> Is that clearer now?
>

> > --
> > Thanks,
> > Rick
> >
> > On Fri, Dec 4, 2009 at 1:29 PM, KARR, DAVID (ATTCINW)
> > <dk068x@att.com>wrote:
> >
> > > I'm reading the OpenJPA doc, and I see that the enhancer creates a
> > > protected no-args constructor if there is no public one.  Why
> exactly
> > > does it do that?  What is the point of creating a protected
> > constructor
> > > instead of letting the JVM create the public one?  The doc only says
> > > that it does this, not why.
> > >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message