hibernate - Does JPA @Id provide value if the table is auto-incrementing? -
this searched high , low in hibernate docs , surprisingly couldn't find answer for.
almost canonical examples of hibernate/jpa demonstrate configuring primary key via @id
annotation so:
// groovy pseudo-code! @entity class car { @id @column(name = "car_id") long id @column(name = "car_make") string make @column(name = "car_model") string model }
but, if have pre-existing database, , if plan on using hibernate merely validate database instead of create 1 me (via hibernate.hbm2ddl.auto = validate
), , if db table configured auto-incrementing primary key field, of value (at application layer) @id
provide me? meaning, given:
hibernate.hbm2ddl.auto = validate
; and- the table/entity has auto-incrementing pk (such mysql
auto_incrementing
)
...then matter application whether entity class has @id
in or not? why couldn't write like:
// groovy pseudo-code! @entity class car { @column(name = "car_id") long id @column(name = "car_make") string make @column(name = "car_model") string model }
or @id
still provide value app in way (if so, how)?
does jpa @id provide value if table auto-incrementing?
short answer no. annotation @id specifies primary key of entity. if want value auto-generated have annotate field @generatedvalue indicating appropriate generationtype strategy, i.e.: identity:
@entity class car { @id @generatedvalue(strategy = generationtype.identity) @column(name = "car_id") long id /... }
note auto-increment or serial types not supported rdbms, i.e: oracle not have auto-increment type can create , use sequence instead.
Comments
Post a Comment