We help IT Professionals succeed at work.

hibernate setId is ignored

Last Modified: 2009-07-29
I'm using hibernate and most of the time I want it to set the ID for me. But, for one particular object I want to sometimes force it to use an ID of my choosing.

Keep in mind that 99% of the time I want this object to do what it's doing. But I'd like to know how to override this behavior in a few cases.

Here is the ID column specified in the hbm file:
            <id column="id" name="id" type="integer">
                  <generator class="sequence"><param name="sequence">Forum_id_seq</param></generator>

If I do:
then it will save a value to the DB which has a value in the ID that comes next in the Forum_id_seq sequence. It isn't the value I specify in the call to setId.

If I then immediately then do this:
then it throws an exception saying something like:
                identifier of an instance of com.knowlist.list.hh.ForumRecord altered from 1024 to 32

Given that this is relatively rare, I'm tempted to just revert to JDBC to change the ID to what I want it to be. But I'd first like to know if there's a way to do this within hibernate.
Watch Question

Java Developer
Top Expert 2010
This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.