You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "roger.keays" <ro...@ninthavenue.com.au> on 2007/02/17 12:37:15 UTC

change primary key value?

Is there a way to change the primary key of an entity in OpenJPA? The
expection below doesn't fill me with hope...

Cheers,

Roger


 <4|false|0.9.6-incubating>
org.apache.openjpa.persistence.InvalidStateException: Attempt to change a
primary key field of an instance that already has a final object id. Only
new, unflushed instances whose id you have not retrieved can have their
primary keys changed. FailedObject:
figbird.lists.entities.Email-info@sunburnt.com.au

   
org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange(StateManagerImpl.java:1819)
   
org.apache.openjpa.kernel.StateManagerImpl.settingStringField(StateManagerImpl.java:1800)
    figbird.lists.entities.Email.pcSetemail(Email.java)
    figbird.lists.entities.Email.setEmail(Email.java:36)
   ...
-- 
View this message in context: http://www.nabble.com/change-primary-key-value--tf3244417.html#a9018988
Sent from the open-jpa-dev mailing list archive at Nabble.com.


RE: [VOTE] change primary key value?

Posted by Patrick Linskey <pl...@bea.com>.
> I guess that being able to do this is technically a bug in 
> OpenJPA, but please don't fix it!

To ensure that this doesn't change (at least without an option to remain
compatible), it'd be useful if you could create a test case that asserts
this capability.

Also, as with any bulk update, remember that the EntityManager-level
cache is bypassed in this scenario.

-Patrick

-- 
Patrick Linskey
BEA Systems, Inc. 

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it. 

> -----Original Message-----
> From: roger.keays [mailto:roger.keays@ninthavenue.com.au] 
> Sent: Tuesday, February 20, 2007 11:51 PM
> To: open-jpa-dev@incubator.apache.org
> Subject: Re: [VOTE] change primary key value?
> 
> 
> Hi Craig,
> 
> Thanks for the reply.
> 
> Craig L Russell wrote:
> > 
> >> Is there a way to change the primary key of an entity in 
> OpenJPA? The
> >> expection below doesn't fill me with hope...
> > 
> > The JPA specification 2.1.4 says that the application must 
> not change  
> > the value of a primary key, but the specific behavior is 
> not defined.  
> > So OpenJPA is spec compliant.
> > 
> That's what I was worried about. Actually, I was able to implement the
> behaviour I needed by using a bulk UPDATE to change the key, 
> and making sure
> the foreign keys were set up correctly (i.e. CASCADE). I 
> guess that being
> able to do this is technically a bug in OpenJPA, but please 
> don't fix it!
> 
> 
> > That said, there are cases I've seen where changing a 
> primary key is  
> > useful. So I would not oppose a proposal to add the feature 
> to OpenJPA.
> > 
> > By the way, JDO doesn't prohibit it; there is a specific optional  
> > feature to allow this. But an implementation has to choose 
> to support  
> > it.
> > 
> Adding it to OpenJPA would be a nice start, although adding 
> it to the JPA
> spec would be better (along with the rest of the JDO 2.0 that is!)
> 
> Cheers,
> 
> Roger
> 
> >
> >  <4|false|0.9.6-incubating>
> > org.apache.openjpa.persistence.InvalidStateException: Attempt to  
> > change a
> > primary key field of an instance that already has a final object  
> > id. Only
> > new, unflushed instances whose id you have not retrieved can have  
> > their
> > primary keys changed. FailedObject:
> > figbird.lists.entities.Email-info@sunburnt.com.au
> >
> >
> > org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange 
> > (StateManagerImpl.java:1819)
> >
> > org.apache.openjpa.kernel.StateManagerImpl.settingStringField 
> > (StateManagerImpl.java:1800)
> >     figbird.lists.entities.Email.pcSetemail(Email.java)
> >     figbird.lists.entities.Email.setEmail(Email.java:36)
> >    ...
> > -- 
> > View this message in context: http://www.nabble.com/change-primary- 
> > key-value--tf3244417.html#a9018988
> > Sent from the open-jpa-dev mailing list archive at Nabble.com.
> >
> 
> 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!
> 
> 
>  
> 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/change-primary-key-value--tf3244417.html
> #a9076663
> Sent from the open-jpa-dev mailing list archive at Nabble.com.
> 
> 

Re: [VOTE] change primary key value?

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

On Feb 20, 2007, at 11:51 PM, roger.keays wrote:

>
> Hi Craig,
>
> Thanks for the reply.
>
> Craig L Russell wrote:
>>
>>> Is there a way to change the primary key of an entity in OpenJPA?  
>>> The
>>> expection below doesn't fill me with hope...
>>
>> The JPA specification 2.1.4 says that the application must not change
>> the value of a primary key, but the specific behavior is not defined.
>> So OpenJPA is spec compliant.
>>
> That's what I was worried about. Actually, I was able to implement the
> behaviour I needed by using a bulk UPDATE to change the key

I'm pretty sure that this behavior is not defined in the 220  
specification and therefore not standard. Note to self: in the future  
revision to 220 should clarify this behavior.

> , and making sure
> the foreign keys were set up correctly (i.e. CASCADE). I guess that  
> being
> able to do this is technically a bug in OpenJPA, but please don't  
> fix it!

Even if this is eventually disallowed by the new specification, there  
would be room for a feature to allow it using a special flag if  
needed, or a standard flag like JDO has.

Craig
>
>> That said, there are cases I've seen where changing a primary key is
>> useful. So I would not oppose a proposal to add the feature to  
>> OpenJPA.
>>
>> By the way, JDO doesn't prohibit it; there is a specific optional
>> feature to allow this. But an implementation has to choose to support
>> it.
>>
> Adding it to OpenJPA would be a nice start, although adding it to  
> the JPA
> spec would be better (along with the rest of the JDO 2.0 that is!)
>
> Cheers,
>
> Roger
>
>>
>>  <4|false|0.9.6-incubating>
>> org.apache.openjpa.persistence.InvalidStateException: Attempt to
>> change a
>> primary key field of an instance that already has a final object
>> id. Only
>> new, unflushed instances whose id you have not retrieved can have
>> their
>> primary keys changed. FailedObject:
>> figbird.lists.entities.Email-info@sunburnt.com.au
>>
>>
>> org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange
>> (StateManagerImpl.java:1819)
>>
>> org.apache.openjpa.kernel.StateManagerImpl.settingStringField
>> (StateManagerImpl.java:1800)
>>     figbird.lists.entities.Email.pcSetemail(Email.java)
>>     figbird.lists.entities.Email.setEmail(Email.java:36)
>>    ...
>> -- 
>> View this message in context: http://www.nabble.com/change-primary-
>> key-value--tf3244417.html#a9018988
>> Sent from the open-jpa-dev mailing list archive at Nabble.com.
>>
>
> 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!
>
>
>
>
>
> -- 
> View this message in context: http://www.nabble.com/change-primary- 
> key-value--tf3244417.html#a9076663
> Sent from the open-jpa-dev mailing list archive at Nabble.com.
>

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: [VOTE] change primary key value?

Posted by "roger.keays" <ro...@ninthavenue.com.au>.
Hi Craig,

Thanks for the reply.

Craig L Russell wrote:
> 
>> Is there a way to change the primary key of an entity in OpenJPA? The
>> expection below doesn't fill me with hope...
> 
> The JPA specification 2.1.4 says that the application must not change  
> the value of a primary key, but the specific behavior is not defined.  
> So OpenJPA is spec compliant.
> 
That's what I was worried about. Actually, I was able to implement the
behaviour I needed by using a bulk UPDATE to change the key, and making sure
the foreign keys were set up correctly (i.e. CASCADE). I guess that being
able to do this is technically a bug in OpenJPA, but please don't fix it!


> That said, there are cases I've seen where changing a primary key is  
> useful. So I would not oppose a proposal to add the feature to OpenJPA.
> 
> By the way, JDO doesn't prohibit it; there is a specific optional  
> feature to allow this. But an implementation has to choose to support  
> it.
> 
Adding it to OpenJPA would be a nice start, although adding it to the JPA
spec would be better (along with the rest of the JDO 2.0 that is!)

Cheers,

Roger

>
>  <4|false|0.9.6-incubating>
> org.apache.openjpa.persistence.InvalidStateException: Attempt to  
> change a
> primary key field of an instance that already has a final object  
> id. Only
> new, unflushed instances whose id you have not retrieved can have  
> their
> primary keys changed. FailedObject:
> figbird.lists.entities.Email-info@sunburnt.com.au
>
>
> org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange 
> (StateManagerImpl.java:1819)
>
> org.apache.openjpa.kernel.StateManagerImpl.settingStringField 
> (StateManagerImpl.java:1800)
>     figbird.lists.entities.Email.pcSetemail(Email.java)
>     figbird.lists.entities.Email.setEmail(Email.java:36)
>    ...
> -- 
> View this message in context: http://www.nabble.com/change-primary- 
> key-value--tf3244417.html#a9018988
> Sent from the open-jpa-dev mailing list archive at Nabble.com.
>

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!


 


-- 
View this message in context: http://www.nabble.com/change-primary-key-value--tf3244417.html#a9076663
Sent from the open-jpa-dev mailing list archive at Nabble.com.


Re: change primary key value?

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

On Feb 17, 2007, at 3:37 AM, roger.keays wrote:

>
> Is there a way to change the primary key of an entity in OpenJPA? The
> expection below doesn't fill me with hope...

The JPA specification 2.1.4 says that the application must not change  
the value of a primary key, but the specific behavior is not defined.  
So OpenJPA is spec compliant.

That said, there are cases I've seen where changing a primary key is  
useful. So I would not oppose a proposal to add the feature to OpenJPA.

By the way, JDO doesn't prohibit it; there is a specific optional  
feature to allow this. But an implementation has to choose to support  
it.

Craig
>
> Cheers,
>
> Roger
>
>
>  <4|false|0.9.6-incubating>
> org.apache.openjpa.persistence.InvalidStateException: Attempt to  
> change a
> primary key field of an instance that already has a final object  
> id. Only
> new, unflushed instances whose id you have not retrieved can have  
> their
> primary keys changed. FailedObject:
> figbird.lists.entities.Email-info@sunburnt.com.au
>
>
> org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange 
> (StateManagerImpl.java:1819)
>
> org.apache.openjpa.kernel.StateManagerImpl.settingStringField 
> (StateManagerImpl.java:1800)
>     figbird.lists.entities.Email.pcSetemail(Email.java)
>     figbird.lists.entities.Email.setEmail(Email.java:36)
>    ...
> -- 
> View this message in context: http://www.nabble.com/change-primary- 
> key-value--tf3244417.html#a9018988
> Sent from the open-jpa-dev mailing list archive at Nabble.com.
>

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!