3

I'm using hibernate to auto generate IDs for table, but i need to manually insert some rows (about 10k, just once) related to another table. I'm using Oracle DB. How can I do that? How does hibernate generete values? It is possible to use it?

@Id
@GeneratedValue
private Long id;
0

2 Answers 2

2

Of course that's possible, we're doing that all the time. Whether and how depends on the id generation strategy you use and how your database is set up.

We're using a (customized) table generator that generates positive ids so whenever we need to manually insert elements we use negative ids. That way those ids don't interfere with Hibernate's id generation and we are able to immediately identify manually inserted rows.

If you don't like negative ids you could use a different generation strateg, e.g.

  • a sequence on the id column that is used by Hibernate as well as manual inserts
  • a high-low table generator (that's what we're using) with the initial low value set to some higher value and thus essentially reserving the lower positive values for manual inserts)
  • an "assigned" id generator, i.e. your application defines the id (e.g. an employee's employee-id) and thus you'd know which ids can be added manually
Sign up to request clarification or add additional context in comments.

1 Comment

OK, i see now, i found that hibernate uses HIBERNATE_SEQUENCE so I have two options: - use HIBERNATE_SEQUENCE - insert with negative ids I think i will use HIBERNATE_SEQUENCE because we need to insert about 10k rows. Thanks!
0

See, @GeneratedValue will only work if You call hibernate API.

To use Autoincrement values, we don't need hibernate @GeneratedValue feature.

You can enable the auto to generate from Database itself. mark a column auto generate.

Refer to :

https://chartio.com/resources/tutorials/how-to-define-an-auto-increment-primary-key-in-oracle/

While inserting don't include the column name and values in your bulk insert Query for column marked as Auto increment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.