So our project use PostgreSQL database and we use JPA for operating the database. We have created the entities from the database with automatic creator in Netbeans 7.1.2.
After small changes our primary key values are described as:
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Basic(optional = false) @NotNull @Column(name = "idwebuser", nullable = false) private Integer idwebuser;
The problem is that now the application is not flexible, because when we modify the database directly (using SQL or another tool) instead of going thru the Java app - the Generated Value is lower than actual database ID value - and so we get error during the creation of new entities.
Is there a possibility that the JPA could just let the database generate the ID automatically and then obtain it after the creation process? Or what could be a better solution? Thanks.
EDIT More specifically: We have a table of users and my problem is that using any type of strategy generationtype, the JPA is inserting a new entity with a specified by it's generator id. Which is wrong for me, because if I make changes to the table on my own, by adding new entries, the GeneratedValue for application is lower than the current ID - what leads us to exception with duplicated ID. Can we fix it ;)?
a short note on the answer There was a little lie from my side because we've used a PG Admin -> View first 100 Rows and edited rows from there instead of using select. ANYWAY, it turns out that this editor somehow skips the process of updating the ID and so even in DB when we write a proper INSERT it is EXECUTED with improper ID! So it was basically more a problem of the editor we used than the database and application...
now it even works using @GeneratedValue(strategy=GenerationType.IDENTITY)
GeneratedValueJPA annotationProvides for the specification of generation strategies for the values of primary keys.
The easiest way to do that is to annotate your primary key attribute with a @GeneratedValue annotation. Hibernate will then pick a strategy based on the database-specific dialect.
@Id annotation is the JPA is used for making specific variable primary key.
Given the table definition:
CREATE TABLE webuser( idwebuser SERIAL PRIMARY KEY, ... )
Use the mapping:
@Entity @Table(name="webuser") class Webuser { @Id @SequenceGenerator(name="webuser_idwebuser_seq", sequenceName="webuser_idwebuser_seq", allocationSize=1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="webuser_idwebuser_seq") @Column(name = "idwebuser", updatable=false) private Integer id; // .... }
The naming tablename_columname_seq
is the PostgreSQL default sequence naming for SERIAL
and I recommend that you stick to it.
The allocationSize=1
is important if you need Hibernate to co-operate with other clients to the database.
Note that this sequence will have "gaps" in it if transactions roll back. Transactions can roll back for all sorts of reasons. Your application should be designed to cope with this.
n
there is an id n-1
or n+1
n
was added or committed before an id less than n
or after an id greater than n
. If you're really careful with how you use sequences you can do this, but you should never try; record a timestamp in your table instead.See the PostgreSQL documentation for sequences and the serial data types.
They explain that the table definition above is basically a shortcut for:
CREATE SEQUENCE idwebuser_id_seq; CREATE TABLE webuser( idwebuser integer primary key default nextval('idwebuser_id_seq'), ... ) ALTER SEQUENCE idwebuser_id_seq OWNED BY webuser.idwebuser;
... which should help explain why we have added a @SequenceGenerator
annotation to describe the sequence.
If you really must have a gap-less sequence (for example, cheque or invoice numbering) see gapless sequences but seriously, avoid this design, and never use it for a primary key.
Note: If your table definition looks like this instead:
CREATE TABLE webuser( idwebuser integer primary key, ... )
and you're inserting into it using the (unsafe, do not use):
INSERT INTO webuser(idwebuser, ...) VALUES ( (SELECT max(idwebuser) FROM webuser)+1, ... );
or (unsafe, never do this):
INSERT INTO webuser(idwebuser, ...) VALUES ( (SELECT count(idwebuser) FROM webuser), ... );
then you're doing it wrong and should switch to a sequence (as shown above) or to a correct gapless sequence implementation using a locked counter table (again, see above and see "gapless sequence postgresql" in Google). Both the above do the wrong thing if there's ever more than one connection working on the database.
It seems you have to use the sequence generator like:
@GeneratedValue(generator="YOUR_SEQ",strategy=GenerationType.SEQUENCE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With