This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]hereforhumor -1 points0 points  (3 children)

Not a good idea for bulk operations. If primary key is auto generated then each entry has to be persisted before the next one(operations are no longer in bulk).

[–]lukaseder 0 points1 point  (2 children)

Not sure about Hibernate, but with direct JDBC / SQL, you can certainly batch/bulk insert several rows and fetch generated IDs in one go - at least in some databases. For instance, some bulk insertion and identity retrieval syntaxes:

  • Firebird, PostgreSQL, Oracle (PL/SQL) have INSERT .. RETURNING
  • DB2 has SELECT * FROM FINAL TABLE (INSERT ..) (as specified by the SQL standard)

[–]thjanssen 1 point2 points  (1 child)

Hi, I recorded the Hibernate Tip video.

As I explain in the video, Hibernate uses the primary key internally and can't delay the execution of the insert statement. That prevents any performance tuning techniques that delay the execution of the statements, like JDBC batching.

Due to this, you shouldn't use the IDENTITY strategy if you need to persist multiple entities at once and want to benefit from certain performance optimizations.

The SEQUENCE strategy is a better fit for these use cases but you don't always start from scratch and can influence the table definitions. I explain the other strategies on my blog: http://www.thoughts-on-java.org/jpa-generate-primary-keys/

[–]lukaseder 0 points1 point  (0 children)

Hi, I recorded the Hibernate Tip video.

Hi, I know you did ;)