> -----Original Message----- > From: Michael Dick [mailto:[email protected]] > Sent: Friday, December 04, 2009 1:54 PM > To: [email protected] > Subject: Re: Why does the enhancer create a PROTECTE no-args > constructor by default? > > 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.
Nope, not a word. The "enhanceClass()" generates the constructor as public if it's externalizable, private if it's final (which is odd, as I thought you couldn't have final persistent classes), and protected otherwise. No indication why. There was a comment about making it public if the class was externalizable, but nothing else. > On Fri, Dec 4, 2009 at 2:55 PM, KARR, DAVID (ATTCINW) > <[email protected]>wrote: > > > > -----Original Message----- > > > From: Rick Curtis [mailto:[email protected]] > > > Sent: Friday, December 04, 2009 12:31 PM > > > To: [email protected] > > > 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) > > > <[email protected]>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. > > > > > >
