Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Use Sequence In Hibernate As A Property In XML Mapping

How do I use a sequence in Hibernate XML mappings?

The documentation mentions the <generator> element. However, I want the sequence to be a column instead of an ID.

like image 955
grassbl8d Avatar asked Feb 22 '11 18:02

grassbl8d


1 Answers

I know when using Hibernate with Oracle the id in the mapping file is defined something like:

<id name="id" column="item_id">
    <generator class="sequence">
        <param name="sequence">NAME_OF_YOUR_SEQUENCE</param>
    </generator>
</id>

You can also specify the generator class as "native", which is handy if you then switch to an auto incrementing RDMS such as MySQL. The sequence bit is then ignored in MySQL.

Edit: Just re-read your question. I don't think hibernate handles sequences on non-id columns. The general approach I have seen is adding triggers to the table, but it's not a nice solution.

like image 112
Karl Walsh Avatar answered Oct 04 '22 02:10

Karl Walsh