Various explanations on the generator node in the hbm configuration file,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.