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/# ">
-
- @ Target({Method, field })
-
- @ Retention(Runtime)
-
- Public @ InterfaceGeneratedvalue {
- Generationtype strategy ()DefaultAuto;
-
- String generator ()Default "";
-
- }
@ 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/# ">
- Public EnumGenerationtype {
- Table,
- Sequence,
- Identity,
- Auto
- }
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/# ">
- @ generatedvalue (generator = " C-assigned " )
- @ genericgenerator (name = " C-assigned " , strategy = " assigned " )
- 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.