Annotation-based primary key settings for hibernate

Source: Internet
Author: User

Annotation-based hibernate primary key setting: @ ID.
So what are its generation rules? It is specified by @ generatedvalue.

Let's take a look at how it is defined:

Java Code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
  1. @ Target({Method, field })
  2. @ Retention(Runtime)
  3. Public @ InterfaceGeneratedvalue {
  4. Generationtype strategy ()DefaultAuto;
  5. String generator ()Default "";
  6. }
 
@ Target ({method, field}) @ retention (runtime) Public @ interface generatedvalue {generationtype strategy () default auto; string generator () Default "";}

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
    1. Public EnumGenerationtype {
    2. Table,
    3. Sequence,
    4. Identity,
    5. Auto
    6. }
 
Public Enum generationtype {table, sequence, identity, auto}

Now we can see that it provides four generation policies:
Table: uses a specific database table to store the sequence of identifiers.
Sequence: generate a serialization identifier.
Identity: The identifier is automatically generated by the database (mainly the automatic growth type)
Auto: hibernate automatically processes the generation of identifiers. It is not recommended for actual project development.
Note: When the primary key is int and the database is not automatically increasing, @ generatedvalue cannot work normally.

We can also use the following method to specify our primary key value:

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
    1. @ generatedvalue (generator = " C-assigned " )
    2. @ genericgenerator (name = " C-assigned " , strategy = " assigned " )
    3. private string employeeid;
@ Generatedvalue (generator = "C-assigned") @ genericgenerator (name = "C-assigned", strategy = "assigned") Private string employeeid;

Or do not define @ generatedvalue directly. The effect of defining only @ ID is the same.

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.