简体   繁体   中英

Hibernate/JPA mixing ID generation strategies

Suppose I have the following situation: I have 5 entities (reduced for the sake of simplicity) of which I unequivocally know that 3 of them will have few records in the database (15-20 or so each). The remaining 2 entities will have a lot of records (thousands) and will also grow rapidly.

Using the default Hibernate ID generation strategy ( AUTO ), this means that all records will have a unique ID across all entities (because there's only 1 sequence). I'm reluctant towards this, because this means that ID's will grow rapidly because of the large number of records in some tables.

Question #1 : can I redefine the ID stragey for the 2 big entities, so that each of those will have its own sequence, and keep the others on AUTO ? Is it considered good/bad practice to mix and match ID strategies? Any reasons why I should/shouldn't do this?

Question #2 : considering an Oracle 10g database, are there any pros and cons concerning ID generation strategies? For instance; is it better to use TABLE instead of SEQUENCE ?

Thanks in advance!

Question #1: There's no problem at all. Each entity class can have it's own ID strategy.

Question #2: Sequences exists mainly to generate surrogate keys. So, use it without fear :-)

with oracle DBMS I think that you should consider using a sequence for all your entities

  • id generation is so reliable done by DB
  • you have no special case which make your code easier to unterstand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM