You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Guillaume Chauvet <gu...@qualiformed.com> on 2013/05/15 18:51:03 UTC

Persist detached entities into a memory database

Dear OpenJPA Users,

We face a particular use case: we want to persist some entities that were
previously detached into a virtual database (derby memory mode).
Unfortunately, if we try to persist these entities that already have Ids
annoted with @GeneratedValue[1], OpenJPA refuses to persist these (expected
default behaviour).

Is there a way in order to recreate a memory-based database with previously
detached entities ?

[1] We use a Timestamp generator for ID values.

Regards,
Guillaume



--
View this message in context: http://openjpa.208410.n2.nabble.com/Persist-detached-entities-into-a-memory-database-tp7583921.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: Persist detached entities into a memory database

Posted by Guillaume CHAUVET <gu...@qualiformed.com>.
Hello John, 

Yes Ids are important in our case because we create new entities that references these entitites.
These new entities are serialized (in  a file) and persisted when the software restart with the main database.

Guillaume

RE: Persist detached entities into a memory database

Posted by Boblitz John <jo...@bertschi.com>.
Hello,

Why not just create a new instance of the entity and set everything except the id & possibly the version attributes 
with the values from the detached entity?   Or are the id's important to maintain across the DB's?

John

> -----Original Message-----
> From: Guillaume CHAUVET [mailto:guillaume.chauvet@qualiformed.com]
> Sent: Friday, May 17, 2013 10:35 AM
> To: 'users@openjpa.apache.org'
> Subject: RE: Persist detached entities into a memory database
> 
> Thank you for your answer, but maintaining two entities graph just for some
> GeneratedValue annotations is an huge workload. We have a fallback
> solution, but not very user friendly; Instead of writing this :
> @Id
> @GeneratedValue // with <property name="openjpa.Sequence"
> value="aaaa.bbbb.MyTimestampGenerator"/>
> private long id;
> 
> We wrote :
> @Id
> private long id = MyTimestampGenerator.next(); // Synchronized statical
> method to get a new Id.
> 
> However, a most proper solution will may be to provide an new OpenJPA
> property that  allows to bypass or not the presence of an existing ID value for
> an annoted field with GeneratedValue.
> We can reflect on this option, except if there is another way.
> 
> Regards

RE: Persist detached entities into a memory database

Posted by Guillaume CHAUVET <gu...@qualiformed.com>.
Thank you for your answer, but maintaining two entities graph just for some GeneratedValue annotations is an huge workload. We have a fallback solution, but not very user friendly; 
Instead of writing this :
@Id
@GeneratedValue // with <property name="openjpa.Sequence" value="aaaa.bbbb.MyTimestampGenerator"/>
private long id;

We wrote :
@Id
private long id = MyTimestampGenerator.next(); // Synchronized statical method to get a new Id.

However, a most proper solution will may be to provide an new OpenJPA property that  allows to bypass or not the presence of an existing ID value for an annoted field with GeneratedValue.
We can reflect on this option, except if there is another way.

Regards

Re: Persist detached entities into a memory database

Posted by Kevin Sutter <kw...@gmail.com>.
Sounds like you need different JPA Entity definitions.  If you used
timestamp generation when you first created/persisted them, but now you
want to persist these in a different datasource, then you'll need a
modified JPA Entity definition that no longer used timestamp id generation.

Kevin


On Thu, May 16, 2013 at 2:41 AM, Guillaume Chauvet <
guillaume.chauvet@qualiformed.com> wrote:

> Hello,
>
> If I use "persist" operation, OpenJPA throws :
> <openjpa-2.2.2-QFD-r422266:1446687M nonfatal store error>
> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
> detached object "xxxx.xxxxx.xxxx.InternalUser@26f50154".  If this is a new
> instance, make sure any version and/or auto-generated primary key fields
> are
> null/default when persisting.
> FailedObject: xxxx.xxxxx.xxxx.InternalUser@26f50154
>
> If I use "merge" operation, OpenJPA throws :
> <openjpa-2.2.2-QFD-r422266:1446687M fatal store error>
> org.apache.openjpa.persistence.OptimisticLockException: Attempted to attach
> deleted instance type "class xxxx.xxxxx.xxxx.InternalUser" with oid
> "1359392486037000524".  If the instance is new, the version field should be
> left to its default value.
> FailedObject: xxxx.xxxxx.xxxx.InternalUser@165f5a4
>
> Regards,
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Persist-detached-entities-into-a-memory-database-tp7583921p7583923.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Persist detached entities into a memory database

Posted by Guillaume Chauvet <gu...@qualiformed.com>.
Hello,

If I use "persist" operation, OpenJPA throws :
<openjpa-2.2.2-QFD-r422266:1446687M nonfatal store error>
org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
detached object "xxxx.xxxxx.xxxx.InternalUser@26f50154".  If this is a new
instance, make sure any version and/or auto-generated primary key fields are
null/default when persisting.
FailedObject: xxxx.xxxxx.xxxx.InternalUser@26f50154

If I use "merge" operation, OpenJPA throws :
<openjpa-2.2.2-QFD-r422266:1446687M fatal store error>
org.apache.openjpa.persistence.OptimisticLockException: Attempted to attach
deleted instance type "class xxxx.xxxxx.xxxx.InternalUser" with oid
"1359392486037000524".  If the instance is new, the version field should be
left to its default value.
FailedObject: xxxx.xxxxx.xxxx.InternalUser@165f5a4

Regards,



--
View this message in context: http://openjpa.208410.n2.nabble.com/Persist-detached-entities-into-a-memory-database-tp7583921p7583923.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Persist detached entities into a memory database

Posted by José Luis Cetina <ma...@gmail.com>.
Why you dont use merge?


2013/5/15 Guillaume Chauvet <gu...@qualiformed.com>

> Dear OpenJPA Users,
>
> We face a particular use case: we want to persist some entities that were
> previously detached into a virtual database (derby memory mode).
> Unfortunately, if we try to persist these entities that already have Ids
> annoted with @GeneratedValue[1], OpenJPA refuses to persist these (expected
> default behaviour).
>
> Is there a way in order to recreate a memory-based database with previously
> detached entities ?
>
> [1] We use a Timestamp generator for ID values.
>
> Regards,
> Guillaume
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Persist-detached-entities-into-a-memory-database-tp7583921.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------