Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate with MySQL : Auto-Generate Id : Equivalent of Sequence (Oracle) in MySQL

As far as i Understand, when 'Native' class is used for auto id generation in Oracle, a single hibernate sequence is created, from where all the IDs are supplied to whichever table needed.

But i dont see this happen with MySQL. Instead, id's for each table start with 1.

Please correct me if am wrong.

Also, if i want the same tu happen on MySQL, what should be done.

Thanks All.

Raj.

like image 524
Raj Avatar asked Jul 23 '10 14:07

Raj


1 Answers

As far as I Understand, when 'Native' class is used for auto id generation in Oracle, a single hibernate sequence is created, from where all the IDs are supplied to whichever table needed.

This occurs if you don't specify any sequence name, unlike this:

<generator class="sequence">
  <param name="sequence">employer_id_seq</param>
</generator>

And actually most of time, people do NOT want a unique sequence, they prefer one sequence per table.

But I don't see this happen with MySQL. Instead, id's for each table start with 1.

With MySQL, the native generator will default to an identity strategy i.e. will use identity columns, which are per table.

Also, if I want the same to happen on MySQL, what should be done.

By same, I guess you mean using sequential IDs for all your entities. Give the table generator a try :

<generator class="table">
</generator>

But this won't perform as well as sequence or identity columns. Also, people usually tend to prefer not sharing IDs between entities as I already wrote. I would think about this again.

like image 98
Pascal Thivent Avatar answered Nov 06 '22 09:11

Pascal Thivent