Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can you use Postgres SERIAL for generating PK in hibernate/JPA?

843833Oct 23 2006 — edited Dec 13 2006
Please don't tell me that JPA has ruled out using auto-incrementing or autogenerating functionalities of individual databases as a standard method for generating primary keys for new objects.

I am reading the Java EE Tutorial and have finally come across a section which has brought me great disappoinment:

In VendorPart, the following code specifies the settings for generating primary
key values:
    @TableGenerator(
       name="vendorPartGen",
       table="EJB_ORDER_SEQUENCE_GENERATOR",
       pkColumnName="GEN_KEY",
       valueColumnName="GEN_VALUE",
       pkColumnValue="VENDOR_PART_ID",
       allocationSize=10)
    @Id
    @GeneratedValue(strategy=GenerationType.TABLE,
       generator="vendorPartGen")
    public Long getVendorPartNumber() {
       return vendorPartNumber;
    }
The @TableGenerator annotation is used in conjunction with @Generated-
Value�s strategy=TABLE element. That is, the strategy used to generate the pri-
mary keys is use a table in the database.


What ever happened to leveraging the auto-incrementing or sequencing functionality of the various different databases? Does this mean I have to code all of this for each and every single table that contains a primary key? OUCH! I was doing so good with hibernate on SJSAS 9.x using Netbeans 5.5 until I run into this. Everything was flowing so easily and then out of nowhere, BAM! Complexity seemingly out of nowhere only to prove sudden inconsistency with the rest of the very nice and easy pathway (right-click->persistence->Create Entity beans from db, ->Create Session beans for entity beans, call EJB, etc). It's a shame to have to leave JPA, undoing everything only to go back to a more straight forward (but simple and effective), yet lengthy JDBC approach.

Please, tell me it isn't so, and that I am over-reacting and that I simply have not yet found the standard documentation to achieve this very thing without having to maintain primary keys in separate tables. Sure I can see how vendor independant this can prove to be, but holy! Why not let the JPA implementations deal with "how" the primary keys are generated (using tables as last resorts, otherwise leveraging whatever functionality/features each db has to offer) and have all of this transparent to the developer who simply wants to store a new object?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 10 2007
Added on Oct 23 2006
4 comments
1,198 views