Link to home
Start Free TrialLog in
Avatar of HappyEngineer
HappyEngineer

asked on

hibernate setId is ignored

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>
            </id>

If I do:
    forumRec.setId(forumId);
    ForumRecordDAO.getInstance().save(forumRec);
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:
                forumRec.setId(forumId);
                ForumRecordDAO.getInstance().update(forumRec);
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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial