You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Hans J. Prueller" <ha...@gmx.net> on 2007/04/12 17:43:52 UTC

state-of-the art attribute initialization in persistent pojos?

hi,

 

as you know I am switching from EJB2.1 CMP to JPA (OpenJPA). I'd like to
know the preferred way to perform attribute initialisation

of newly created entity instances, e.g. 

 

we initialized our EJB2.1 CMP entity beans with

 

ejbCreate(String cid, Integer someOtherValue)

{

setabc();

setdef();

}

//and ejbPostCreate optionally

 

as far as I know, the JPA spec requires a no-arg constructor - so when
providing a constructor with the same args that we

have used in ejbCreate before, we cannot ensure that some developer calls
the no-arg constructor by default.

 

is there any other-more elegant way to force new entity instance
initialization?

 

regards,

HANS

 

=========================== 
virtually hanzz...

 

 <http://hanzz.zapto.org> http://hanzz.zapto.org (personal)
 <http://www.cse.dmu.ac.uk/~hansp> http://www.cse.dmu.ac.uk/~hansp
(research)

 


Re: state-of-the art attribute initialization in persistent pojos?

Posted by "Hans J. Prueller" <ha...@gmx.net>.
Am Donnerstag, den 12.04.2007, 08:57 -0700 schrieb Craig L Russell:

> The no-arg constructor can be private. 


fine - is this an OpenJPA specific feature or part of the JPA spec? You
have to know: in our current EJB2.1 CMP EB implementation,
we - unconsciously - used a lot of not-spec-compliant stuff which
inhibits deployment of our .ear project to another J2EE1.4 container
- that's one of the reasons we're migrating to JPA.... but in this case,
I'd like to stay spec conformant if possible.

anyway - thanks for the tip!

regards,
HANS

Re: state-of-the art attribute initialization in persistent pojos?

Posted by Craig L Russell <Cr...@Sun.COM>.
I stand corrected. From the spec, 2.1:

The entity class must have a no-argconstructor. The entity class may  
have other constructors as well.
The no-arg constructor must be public or protected.

Adding a protected no-arg constructor if your class is migrating from  
CMP to EJB 3 seems to be best practice if you want to encourage use  
of other constructors.

Craig

On Apr 12, 2007, at 10:29 AM, Thomas Risberg wrote:

> I thought the no-arg constructor had to be public or protected - see
> "2.1 Requirements on the Entity Class" section where this is  
> mentioned.
> If that's not the case so much better, but I have been adding a
> protected constructor in my entities.
>
> Cheers,
> Thomas
>
>
> Craig L Russell wrote:
>> Hi Hans,
>>
>> On Apr 12, 2007, at 8:43 AM, Hans J. Prueller wrote:
>>
>>> hi,
>>> as you know I am switching from EJB2.1 CMP to JPA (OpenJPA). I'd  
>>> like
>>> to know the preferred way to perform attribute initialisation of
>>> newly created entity instances, e.g. we initialized our EJB2.1 CMP
>>> entity beans with
>>>
>>> ejbCreate(String cid, Integer someOtherValue)
>>> {
>>> setabc();
>>> setdef();
>>> }
>>>
>>> //and ejbPostCreate optionally
>>>
>>> as far as I know, the JPA spec requires a no-arg constructor - so
>>> when providing a constructor with the same args that we have used in
>>> ejbCreate before, we cannot ensure that some developer calls the
>>> no-arg constructor by default.
>>
>> The no-arg constructor can be private. The reason it's required is so
>> the implementation doesn't have to guess what the right values are  
>> for
>> final variables. But there's no requirement to make this constructor
>> available to your applications.
>>>
>>> is there any other-more elegant way to force new entity instance
>>> initialization?
>>
>> There's no requirement for an ejbCreate and a separate ejbPostCreate
>> for relationships. So you are really free to do whatever you like for
>> the application contract. Have as many constructors as you like and
>> put whatever initialization into them.
>>
>> Just don't initialize the values of generated fields...
>>
>> Craig
>>>
>>> regards,
>>>
>>> HANS
>>>
>>> ===========================
>>> virtually hanzz...
>>>
>>>  <http://hanzz.zapto.org> http://hanzz.zapto.org (personal)
>>>  <http://www.cse.dmu.ac.uk/~hansp> http://www.cse.dmu.ac.uk/~hansp
>>> (research)
>>
>> Craig Russell
>> Architect, Sun Java Enterprise System http://java.sun.com/products/ 
>> jdo
>> 408 276-5638 mailto:Craig.Russell@sun.com
>> P.S. A good JDO? O, Gasp!
>>
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: state-of-the art attribute initialization in persistent pojos?

Posted by Thomas Risberg <th...@tridb.com>.
I thought the no-arg constructor had to be public or protected - see
"2.1 Requirements on the Entity Class" section where this is mentioned. 
If that's not the case so much better, but I have been adding a
protected constructor in my entities.

Cheers,
Thomas


Craig L Russell wrote:
> Hi Hans,
>
> On Apr 12, 2007, at 8:43 AM, Hans J. Prueller wrote:
>
>> hi,
>> as you know I am switching from EJB2.1 CMP to JPA (OpenJPA). I'd like
>> to know the preferred way to perform attribute initialisation of
>> newly created entity instances, e.g. we initialized our EJB2.1 CMP
>> entity beans with
>>
>> ejbCreate(String cid, Integer someOtherValue)
>> {
>> setabc();
>> setdef();
>> }
>>
>> //and ejbPostCreate optionally
>>
>> as far as I know, the JPA spec requires a no-arg constructor - so
>> when providing a constructor with the same args that we have used in
>> ejbCreate before, we cannot ensure that some developer calls the
>> no-arg constructor by default.
>
> The no-arg constructor can be private. The reason it's required is so
> the implementation doesn't have to guess what the right values are for
> final variables. But there's no requirement to make this constructor
> available to your applications.
>>
>> is there any other-more elegant way to force new entity instance
>> initialization?
>
> There's no requirement for an ejbCreate and a separate ejbPostCreate
> for relationships. So you are really free to do whatever you like for
> the application contract. Have as many constructors as you like and
> put whatever initialization into them.
>
> Just don't initialize the values of generated fields...
>
> Craig
>>
>> regards,
>>
>> HANS
>>
>> ===========================
>> virtually hanzz...
>>
>>  <http://hanzz.zapto.org> http://hanzz.zapto.org (personal)
>>  <http://www.cse.dmu.ac.uk/~hansp> http://www.cse.dmu.ac.uk/~hansp
>> (research)
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>


Re: state-of-the art attribute initialization in persistent pojos?

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Hans,

On Apr 12, 2007, at 8:43 AM, Hans J. Prueller wrote:

> hi,
> as you know I am switching from EJB2.1 CMP to JPA (OpenJPA). I'd  
> like to know the preferred way to perform attribute initialisation  
> of newly created entity instances, e.g. we initialized our EJB2.1  
> CMP entity beans with
>
> ejbCreate(String cid, Integer someOtherValue)
> {
> setabc();
> setdef();
> }
>
> //and ejbPostCreate optionally
>
> as far as I know, the JPA spec requires a no-arg constructor - so  
> when providing a constructor with the same args that we have used  
> in ejbCreate before, we cannot ensure that some developer calls the  
> no-arg constructor by default.

The no-arg constructor can be private. The reason it's required is so  
the implementation doesn't have to guess what the right values are  
for final variables. But there's no requirement to make this  
constructor available to your applications.
>
> is there any other-more elegant way to force new entity instance  
> initialization?

There's no requirement for an ejbCreate and a separate ejbPostCreate  
for relationships. So you are really free to do whatever you like for  
the application contract. Have as many constructors as you like and  
put whatever initialization into them.

Just don't initialize the values of generated fields...

Craig
>
> regards,
>
> HANS
>
> ===========================
> virtually hanzz...
>
>  <http://hanzz.zapto.org> http://hanzz.zapto.org (personal)
>  <http://www.cse.dmu.ac.uk/~hansp> http://www.cse.dmu.ac.uk/~hansp
> (research)

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!