Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Incrementing generated primary key in @PrePersist(According to JPA, is it valid and portable to manually increase a JPA-generated primary key in @PrePersists?)
Incrementing generated primary key in @PrePersist [message #1053103] Thu, 02 May 2013 13:02 Go to next message
Markus KARG is currently offline Markus KARGFriend
Messages: 28
Registered: June 2011
Junior Member
As my JPA-based program and a third-party non-Java software need to share the same ID Generator, I had to do a trick: The third-party Software expects the value of the generator to be "next id", while JPA expects it to be "last id", so I simply did this:

* Set increment of @TableGenerator to 1, so JPA must SELECT again before each use.
* Added @PostPersist listener to simple switch from "last id" semantics to "next id semantics":

@PrePersist private void prePersist() {
    this.pk.id--;
}


This actually seems to work pretty well, but I like to know whether this is considered harmful in any particular situation, and whether it is actually a portable solution?

Thanks!
-Markus

[Updated on: Fri, 03 May 2013 06:18]

Report message to a moderator

Re: Incrementing generated primary key in @PrePersist [message #1053744 is a reply to message #1053103] Tue, 07 May 2013 13:13 Go to previous messageGo to next message
Markus KARG is currently offline Markus KARGFriend
Messages: 28
Registered: June 2011
Junior Member
Not a single comment on this? So I can assume the EclipseLink Team does not see any negative impact? Great! Smile
Re: Incrementing generated primary key in @PrePersist [message #1053784 is a reply to message #1053744] Tue, 07 May 2013 14:57 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You could also create your own Sequence object that does "last id" semantics.
Just subclass the existing TableSequence.


James : Wiki : Book : Blog : Twitter
Re: Incrementing generated primary key in @PrePersist [message #1053786 is a reply to message #1053784] Tue, 07 May 2013 15:01 Go to previous message
Markus KARG is currently offline Markus KARGFriend
Messages: 28
Registered: June 2011
Junior Member
Sounds much more complex than simply adding a callback. Smile
Previous Topic:Unique Constraints and Delete and Insert Order - Constraint Violation
Next Topic:create a function via nativeQuery
Goto Forum:
  


Current Time: Fri Apr 26 14:32:13 GMT 2024

Powered by FUDForum. Page generated in 0.02727 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top