Indeed, the issue is with the “int“ vs “Integer” types. What Hibernate does to store the rank value is:
- insert the employee row
- select max(rank) + 1 from employee by the dept_id
- update the employee row inserted in 1) with the max+1 fetched in 2
Now if you have an “int” rank, the first employee to be inserted will have 0 rank (the default value for the int type in java). The max(rank) + 1 will return 1, and the updated value of the rank will be 1. Whereas, when Integer is used, the employee would have null as default rank. The max(rank) + 1 returned from DB will be NULL. And so the updated value of the rank will be 0. I don’t know if this is an intended behaviour or a bug. |