Various explanations on the generator node in the hbm configuration file,
I made a mistake today.
The class attribute of the generator node has the following values (Primary Key Generation Policy Scheme ):
Assigned: primary key value specified by the user
Sequence: generated by the oracle sequence
Increment: for the primary keys of the long, short, and int types, the primary key value is generated in auto-increment mode. The increment is 1.
Identity: it is useless in oracle, because it can be used by databases that support flag Columns
Native: decided by hibernate (let's test it by yourself)
When we configure the node attribute as assigned, and the primary key is not assigned a value:
When the node attribute is increment, we run:
The query statement is executed. The maximum gradeid is queried, and my table is empty. Therefore, no value is found and no update or insert operations are performed.
You can ignore the transction error because no begintransaction () was performed during the previous test. You can solve this problem by adding session. beginTransaction () before saving.
Solution:
- Assign a value to the id before executing the save () operation.
- Change generation policy to sequence (with a sequence prerequisite)
- Use increment when the table is not empty