JPA- Hibernate - ID values generated from Select statement
I am having problems mapping a legacy database schema using JPA annotations.
The schema uses a table to generate unique IDs for most of the tables in the db - it has a structure of:
table_name
column_name
next_id
block_size
To get new unique ID for a record insert, you run a select like:
SELECT next_id FROM tbl_next_id WHERE table_name = 'tbl_pets' and column_name = 'pet_id'开发者_开发知识库
urrgh. I know I could do this in pure hibernate by using a 'select' id generator (unless I just dreamt that up) - can anyone recommend the best way of handling this with JPA annotations?
Thanks
There is nothing better than this:
@TableGenerator(name="petsgen",
table="tbl_next_id",
pkColumnName="table_name",
pkColumnValue="tbl_pets",
valueColumnName="next_id",
allocationSize=1
)
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="petsgen")
Will of course not support combination of table name + column name, but should work as long as there is only one generator for table.
TableGenerator does something similar, but not exactly. There is no support for column_name and block_size. Maybe you can extend that generator to take care of it.
精彩评论