Thanks for the reply, I'm using MySQL which I believe does allow this.
Does this mean my understanding of Session.replicate() is correct and makes sense in this case ?
Would I be wrong in suggesting that Hibernate should throw an exception if it was not able to honour keeping the current ID value. From a programmers point of view this was the intended behaviour, otherwise he would have called Session.save().
If the programmer does not actually care then maybe a Session.replicateOrSave() could be introduced to let him choose his case.
Once its throwing exceptions in cases where I can't honer the assigned id (or does not know if it can), then have a configurable override per class in the XML config that allows the programmer to know best and enable running of the INSERT with primary key assigned from Session.replicate(). This would be enough to get me out of the situation.
Then maybe in the future it would be possible to use database dialect and runtime driver/server versioning to auto-configure those cases that were able to honour assigned ID value even in IDENTITY tables.
Is there anything alternative with Hibernate I could use, or is JDBC my way out here ?
Code:
mysql> create table foobar ( id BIGINT AUTO_INCREMENT, value INT, PRIMARY KEY(id));
Query OK, 0 rows affected (0.14 sec)
mysql> insert into foobar(id, value) VALUES('999999', '42');
Query OK, 1 row affected (0.14 sec)
mysql> insert into foobar(value) VALUES('43');
Query OK, 1 row affected (0.00 sec)
mysql> select * from foobar;
+---------+-------+
| id | value |
+---------+-------+
| 999999 | 42 |
| 1000000 | 43 |
+---------+-------+
2 rows in set (0.00 sec)
mysql>
The above is exactly what I want to do.
I did try to work around the problem by defining two mappings to two different POJO classes, but one with ASSIGNED key generation. And simply make the assigned POJO a super-class of the native generated one (class MyClassAssigned extends MyClassNative) This works during the population process, but with both maps loaded into the the Hibernate Session my HQL query of the sub-class:
FROM MyClassNative AS rec WHERE rec.columnOnUniqueKey = ?
Does two SELECTs one for each type and my application starts to see everything in double vision. I'm sure mapping the same table in two different ways must do bad things, so I've stopped that idea.