Author | Message |
---|---|
abhishek_sharma
Posts: 79
|
Posted 15:49 Jul 19, 2012 |
Does any one use MySQL with Hibernate?
I can create a another table named nextval and generate a value but not sure if it will b thread safe. Any idea or help with this issue ?? |
cysun
Posts: 2935
|
Posted 09:57 Jul 20, 2012 |
As you already found out, MySQL does not support sequence - instead, it uses AUTO_INCREMENT to generate primary keys, and if that's all you need (i.e. auto-generate primary keys), Hibernate would take care of it when you specify the dialect to be MySQL and the id generation strategy to be AUTO. If you need the database to generate unique numbers for something else, what you did should be fine. Basically you create a dummy table with an AUOT_INCREMENT primary key, and every time you want a new unique value, just do an insert then get the value of the primary key. You can do this in Hibernate by creating a class that is mapped to the table, then you can simply do: new object -> save object -> getId(). And yes, this is thread safe. |
abhishek_sharma
Posts: 79
|
Posted 11:27 Jul 20, 2012 |
Thanks for the reply strategy = GenerationType.AUTO doesn't work so I used strategy = GenerationType.IDENTITY and I don't think i need any other table as this strategy works fine, is it necessary to use any such table? |
cysun
Posts: 2935
|
Posted 16:24 Jul 20, 2012 |
I don't think you need to specify strategy - just @GeneratedValue should work. And yes, you don't need to create the additional table if you just need it to generate primary keys. |