* @author Xiao Zheng
1 * @content EJB3 Annotated API is defined in the javax.persistence.* package.
2 * Note Description:
3 * @Entity-Declare a class as an entity bean (that is, a persisted Pojo class)
4 * @Id--the annotation declares the identity attribute of the entity bean (the primary key in the corresponding table).
5 * @Table--The annotations declare the tables (table) specified by the Entity bean Mapping, the catalog, and the schema name
6 * @Column-Annotations Declare attribute-to-column mappings. This annotation has the following properties
* Name optional, column name (default value is property name)
* Unique optional, whether to set a unique constraint on this column (default value false)
* Nullable optional, setting the value of this column can be null (default value false)
* Insertable Optional, whether the column is a column in the resulting INSERT statement (the default is true)
* Updatable optional, whether the column is a column in the generated UPDATE statement (default value true)
* ColumnDefinition Optional, overwriting the SQL DDL fragment for this specific column (this may result in a failure to migrate between different databases)
* Table optional, define the corresponding tables (default is the primary table)
* Length optional, column lengths (default value 255)
* Precision optional, column decimal precision (decimal precision) (default value 0)
* Scale is optional, if the column decimal value range is available, this setting (default value 0)
7 * @GeneratedValue-the annotation declares the generation strategy of the primary key. The annotation has the following properties
* Strategy Specifies the generated policy (JPA-defined), which is a generationtype. The default is GenerationType. AUTO
* Generationtype.auto primary key by program control
* Generationtype.table use a specific database table to save the primary key
* Generationtype.identity primary key is automatically generated by the database (mainly auto-growth type)
* Generationtype.sequence generates a primary key based on the sequence of the underlying database, provided the database supports the sequence. (This value is to be used with generator)
* Generator Specifies the generator used to generate the primary key (possibly a sequence in orcale).
* @SequenceGenerator--The annotations declare a database sequence. The annotation has the following properties
* Name indicates the table primary key generation policy name, which is referenced in the "Gernerator" value set in @generatedvalue
* Sequencename indicates the name of the database sequence used to generate the policy.
* InitialValue represents the primary key initial value, which defaults to 0.
* Allocationsize each time the primary key value is increased, for example, set to 1, it will automatically add 1 after each new record is created, and the default is 50.
8 * @GenericGenerator--the annotation declares a hibernate primary key generation strategy. Supports 13 strategies. The annotation has the following properties
* Name Specifies the generator name
* Strategy Specifies the class name of the specific generator (specifies the build policy).
* Parameters gets the parameters used by the specific generator specified by strategy.
9 * Its 13 strategies (the value of the Strategy attribute) are as follows:
* 1. native for Orcale in sequence way, for MySQL and SQL Server with identity (in the case of primary key generation mechanism),
Ten * native is the creation of the primary key will be completed by the database, hibernate regardless (very common)
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* Genericgenerator (name = "Paymentablegenerator", strategy = "native")
Each * 2.uuid uses a 128-bit UUID algorithm to generate a primary key, and the UUID is encoded as a 32-bit 16-digit string. Occupies large space (string type).
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "uuid")
*3.hilo to create an additional table in the database, the default table name is Hibernate_unque_key, the default field is the integer type, and the name is Next_hi (less)
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "Hilo")
*4.assigned when inserting data, the primary key is handled by the program (very common), which is the default generation policy when the <generator> element is not specified. is equivalent to auto in JPA.
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "assigned")
*5.identity using the SQL Server and MySQL self-increment fields, this method can not be placed in Oracle, Oracle does not support the self-increment field, to set the sequence (MySQL and SQL Server is very common). Equivalent to the identity in JPA
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "identity")
* 6.select using triggers to generate primary keys (primarily for early database primary key generation mechanisms, less)
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "SELECT")
* 7.sequence invokes a sequence of cautious databases to generate a primary key, and to set the sequence name, hibernate cannot find it.
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "sequence",
*parameters = {@Parameter (name = "Sequence", value = "Seq_payablemoney")})
The *8.seqhilo is implemented through the Hilo algorithm, but the primary key history is saved in sequence and is applicable to databases that support sequence, such as orcale (less useful)
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "Seqhilo",
* Parameters = {@Parameter (name = "Max_lo", value = "5")})
*9.increnment when inserting data hibernate will add a self-incremented primary key to the primary key, but one hibernate instance maintains a counter, so this method cannot be used when multiple instances are running.
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "increnment")
*10.foreign uses the primary key of another related object. Usually used in conjunction with <one-to-one>.
* Example: @Id
* @GeneratedValue (Generator = "Idgenerator")
* @GenericGenerator (name = "Idgenerator", strategy = "foreign",
*parameters = {@Parameter (name = "Property", value = "info")})
*integer ID;
* @OneToOne
* EmployeeInfo info;
* 11.guid uses the database underlying GUID algorithm mechanism, corresponding to the MySQL uuid () function, SQL Server's NEWID () function, Orcale's Rawtohex (Sys_guid ()) function, etc.
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "GUID")
* 12.uuid.hex See Uudi, suggest to replace with UUID
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "Uuid.hex")
*13.sequence-identity sequence strategy extension, using an immediate retrieval strategy to get sequence value, need JDBC3.0 and JDK4 above (including 1.4) version
* Example: @GeneratedValue (generator = "Paymentablegenerator")
* @GenericGenerator (name = "Paymentablegenerator", strategy = "sequence-identity",
*parameters = {@Parameter (name = "Sequence", value = "Seq_payablemoney")})
* @OneToOne set a pair of associations. The Cascade attribute has five values (only Cascadetype.all good?). Very strange), Cascadetype.persist (Cascade New), Cascadetype.remove (cascade Delete), Cascadetype.refresh (cascade refresh), Cascadetype.merge (cascade Update), Cascadetype.all (all four items)
23 * Method One
24 * Main Table: [email protected] (cascade = cascadetype.all)
* @PrimaryKeyJoinColumn
* Public from table class get from table Class () {return from Table object}
27 * FROM table: no Main Table class.
28 * Note: This method requires the primary table to correspond to the primary key value from the table.
29 * Method Two
30 * Main Table: [email protected] (cascade = cascadetype.all)
* @JoinColumn (name= "Main Table foreign Key")//The Foreign key field in the database is specified here.
* Public from table class get from table Class () {return from table class}
33 * FROM table: @OneToOne (Mappedby = "from table attribute in primary table class")//Example Main Table user has a heart type heart from the Table property, which is filled in here heart
* Public Main Table class get Main Table Class () {return Main Table Object}
35 * Note: @JoinColumn is optional. The default value is from the table variable name + "_" + the primary key from the table (note that this Riga is the primary key.) Instead of the primary key).
36 * Method Three
37 * Main Table: @OneToOne (Cascade=cascadetype.all)
* @JoinTable (name= "Association table name",
Joincolumns = @JoinColumn (name= "Main Table foreign key"),
Inversejoincolumns = @JoinColumns (name= "foreign key from table")
41 *)
42 * FROM table: @OneToOne (Mappedby = "from table attribute in primary table class")//Example Main Table user has a heart type heart from the Table property, which is filled in here heart
* Public Main Table class get Main Table Class () {return Main Table Object}
* @ManyToOne set many-to-one association
45 * Method One
* @ManyToOne (Cascade={cascadetype.persist,cascadetype.merge})
* @JoinColumn (name= "foreign Key")
* Public Main Table class get Main Table Class () {return Main Table Object}
49 * Method Two
* @ManyToOne (Cascade={cascadetype.persist,cascadetype.merge})
Wuyi * @JoinTable (name= "Association table name",
* Joincolumns = @JoinColumn (name= "Main Table foreign key"),
* Inversejoincolumns = @JoinColumns (name= "From table foreign Key")
54 *)
* @OneToMany set a one-to-many association. The Cascade property specifies the association level, referring to the description in @onetoone. FETCH specifies whether to delay loading, a value of fetchtype.lazy for delay, and Fetchtype.eager for immediate loading
56 * Method One using this configuration, the "multi-port" foreign key is not modified when "multi-port" is added for the "end". When the "one end" is loaded, it will not get "multi-terminal". If lazy loading is used, an exception is thrown when the "multi-terminal" list is read, and immediately loaded at the time of the multi-port, is an empty collection (the collection element is 0).
57 * "One End" configuration
mappedby= * @OneToMany ("multi" Property ")
* Public list< "Multi-terminal" Class > get "multi-terminal" list () {return ' multi-terminal ' list}
60 * "Multi-terminal" configuration reference @manytoone.
61 * Method Two
62 * "One End" configuration
mappedby= * @OneToMany ("multi" attribute ")
* @MapKey (name= "" multi-port "as the key Attribute")
* Public map< "multi-port" as the property of the key class, the Main Table class > get "multi-terminal" list () {return "multi-terminal" list}
66 * "Multi-terminal" configuration reference @manytoone.
67 * Method Three using this configuration, you can modify a "multi-port" foreign key when you add a "multi-side" to "one end".
68 * "one end" configuration
* @OneToMany
* @JoinColumn (name= "" multi-port "foreign key")
* Public list< "Multi-terminal" Class > get "multi-terminal" list () {return ' multi-terminal ' list}
72 * "Multi-terminal" configuration reference @manytoone.
@Column (name = "Remark")
@ColumnDescriptor (name = "description information")
For a description of a column
@JoinColumn (name= "Service_provider")
@JoinColumn tag to specify the configuration to save the entity relationship for the fields in the comment table
@JoinColumn is distinguished from @column:
@JoinColumn Note is the field that holds the relationship between the table and the table, which you want to label on the entity properties.
The @column label is a field in the table that does not contain a table relationship
@ManyToOne (Cascade=cascadetype. DETACH, Fetch=fetchtype. LAZY)
@ManyToMany (cascade = {Cascadetype. DETACH}, Fetch = Fetchtype. LAZY)
@JoinTable (name = "V_device_group_link", Joincolumns = {@JoinColumn (name = "Devgroupid")}, Inversejoincolumns = {@JoinC Olumn (name = "DevId")})
About Spring annotations