In the previous blog, mention was made of a note @generatedvalue, which is used to annotate a policy that generates a primary key, one of which is the table.
Note: This method is not commonly used and is only used when encountering special business requirements.
For annotations, refer to the JPA Learning notes (3)--JPA note How to use the table policy to generate primary keys
With this strategy, you need to create a table
Table Name: Id_generator
Field name |
Description |
Id |
ID of this record |
Pk_name |
Primary Key Name |
Pk_value |
Primary key value |
See here, it's going to be unclear, so. I'll explain later.
Insert a few data into this chart
ID |
Pk_name |
Pk_value |
1 |
customer_id |
1 |
2 |
student_id |
10 |
3 |
order_id |
100 |
Pk_name , customer_id represents the customer this table generates ID, its value is pk_value=1, this value does not represent the customer ID is 1, but an algorithm to automatically generate the ID, And this algorithm is related to this pk_value.
The same, student_id represents the STUDENT table generation ID, and the value is ten then how does JPA know to generate IDs for the other three tables based on this table?
Locate the Pk_value according to the horizontal ordinate.
Use annotations to tell JPA horizontal ordinate
Package Com.jpa.helloworld;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.ManyToOne;
Import javax.persistence.Table;
Import Javax.persistence.TableGenerator; @Table (name= "T_order") @Entity the public class Order {@Column (name= "ID") @TableGenerator (name= "Id_generator",//Build
The name table= "Id_generator",//The table pkcolumnname= "Pk_name" used by the generator,//the corresponding field name in the table Pkcolumnvalue= "order_id",///The value of the above field valuecolumnname= "Pk_value",///value on Described three properties, you can navigate to the value of the pk_value in the table, such as: 1,10,100 allocationsize=10)//indicates that the primary key increases by one @GeneratedValue (Strategy=gener
Ationtype.table, generator= "Id_generator")//The generator here corresponds to the name of the generator above @Id private Integer ID; @Column (name= "Order_name") Private STring Ordername;
@JoinColumn (name= "user_id") @ManyToOne private user user;
Public Integer GetId () {return id;
The public void SetId (Integer id) {this.id = ID;
Public String Getordername () {return ordername;
} public void Setordername (String ordername) {this.ordername = Ordername;
Public user GetUser () {return user;
public void SetUser (user user) {this.user = user;
@Override public String toString () {return "order [id=" + ID + ", ordername=" + Ordername + ", user="
+ user + "]";
}
}
Each time the order table adds a record, the primary key increases by 10, so that the corresponding Pk_value value in the Id_generator table is increased by 1. But we don't need to ignore this value.
At this stage, it's all done.