Detailed explanation of hibernate annotations

/**

* @author liuguangyi
* The API of the @content ejb3 annotation is defined in the javax.persistence.* package.

* Annotation description:
* @Entity - declares a class as an entity bean (ie, a persistent POJO class)
* @Id - annotation declares the identity property of the entity bean (corresponding to the primary key in the table).
* @Table - The annotation declares the name of the table, catalog and schema specified by the entity bean mapping
* @Column - The annotation declares the mapping of attributes to columns. The annotation has the following attributes
* name optional, column name (the default value is the attribute name)
* unique optional, whether to set a unique constraint on the column (default value false)
* nullable optional, whether to set the value of the column can be empty (default value false)
* insertable optional, whether the column is used as a column in the generated insert statement (default value true)
* updatable optional, whether the column is used as a column in the generated update statement (default value true) )
* columnDefinition optional, override the sql ddl fragment for this specific column (this may lead to inability to portability between different databases)
* table optional, define the corresponding table (default main table)
* length optional, column length (default value) 255)
* precision is optional, column decimal precision (default value 0)
* scale is optional, if column decimal scale is available, set it here (default value 0)
* @GeneratedValue - the annotation declares the primary key generation strategy. The annotation has the following attributes
* strategy Specifies the generated strategy (defined by JPA), which is a GenerationType. The default is GenerationType. AUTO
* GenerationType.AUTO The primary key is controlled by the program
* GenerationType.TABLE uses a specific database table to save the primary key
* GenerationType.IDENTITY The primary key is automatically generated by the database (mainly auto-growth type)
* GenerationType.SEQUENCE According to the underlying database sequence to generate the primary key, provided that the database supports sequences. (This value is to be used with generator)
* generator specifies the generator used to generate the primary key (may be a sequence in orcale).
* @SequenceGenerator - The annotation declares a database sequence. This annotation has the following attributes
* name indicates the name of the primary key generation strategy for the table, which is referenced in the "gernerator" value set in @GeneratedValue
* sequenceName indicates the database sequence name used for the generation strategy.
* initialValue represents the initial value of the primary key, the default is 0.
* allocationSize The size of the primary key value increase each time, for example, if set to 1, it means that it will automatically increase by 1 every time a new record is created, and the default is 50.
* @GenericGenerator - The annotation declares a primary key generation strategy for hibernate. Thirteen strategies are supported. The annotation has the following attributes
* name specifies the generator name
* strategy specifies the class name of the specific generator (specifies the generation strategy).
* parameters Get the parameters used by the specific generator specified by strategy.
* The thirteen strategies (values ​​of the strategy attribute) are as follows:
* 1. native uses Sequence for orcale, and identity (situational primary key generation mechanism) for MySQL and SQL Server,
* native means that the primary key generation will be done by the database, Hibernate doesn't care (very commonly used)
* Example: @GeneratedValue(generator = "paymentableGenerator")    
* @GenericGenerator(name = "paymentableGenerator", strategy = "native")
* 2.uuid uses a 128-bit uuid algorithm to generate the primary key, and the uuid is encoded is a string of 32 hexadecimal digits. Takes up a lot of 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 integer type, and the name is next_hi (rarely used )
* Example: @GeneratedValue(generator = "paymentableGenerator")    
* @GenericGenerator(name = "paymentableGenerator", strategy = "hilo")
* 4.assigned The primary key is handled by the program when inserting data (very common), which is < The default generation strategy when the generator> element is not specified. Equivalent to AUTO in JPA.
* Example: @GeneratedValue(generator = "paymentableGenerator")    
* @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")
* 5.identity uses the self-incrementing fields of SQL Server and MySQL, this method cannot be placed in Oracle, Oracle does not support self-incrementing fields, and you need to set the sequence (commonly used in MySQL and SQL Server). Equivalent to IDENTITY in JPA
* Example: @GeneratedValue(generator = "paymentableGenerator")    
* @GenericGenerator(name = "paymentableGenerator", strategy = "identity")
* 6.select use triggers to generate primary keys (mainly used in the early database primary key generation mechanism, use less )
* Example: @GeneratedValue(generator = "paymentableGenerator")    
* @GenericGenerator(name = "paymentableGenerator", strategy = "select")
* 7.sequence call the sequence of the cautious database to generate the primary key, set the sequence name, otherwise hibernate unable to find.
* Example: @GeneratedValue(generator = "paymentableGenerator")   
* @GenericGenerator(name = "paymentableGenerator", strategy = "sequence",    
* parameters = { @Parameter(name = "sequence", value = "seq_payablemoney"

* Example: @GeneratedValue(generator = "paymentableGenerator")   
* @GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo",    
* parameters = { @Parameter(name = "max_lo", value = "5") })
* 9.increnment When inserting data, hibernate will add an auto-incrementing primary key to the primary key, but a 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 use another related object's primary key. Usually used in conjunction with <one-to-one>.
* Example: @Id    
* @GeneratedValue(generator = "idGenerator")   
* @GenericGenerator(name = "idGenerator", strategy = "    
* parameters = { @Parameter(name = "property", value = "info") })    
* Integer id;
* @OneToOne  
* EmployeeInfo info;
* 11.guid uses the underlying guid algorithm mechanism of the database, corresponding to MySQL's 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 Look at uudi, it is recommended to replace it with uuid
* Example: @GeneratedValue(generator = "paymentableGenerator")     
* @GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")
* 13.sequence-identity sequence strategy Extension, using the immediate retrieval strategy to obtain the sequence value, requires JDBC3.0 and JDK4 or above (including 1.4)
* Example: @GeneratedValue(generator = "paymentableGenerator")   
* @GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity",    
* parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") } )  
*      
* @OneToOne Sets a one-to-one association. The cascade attribute has five values ​​(only CascadeType.ALL is easy to use? It's strange), namely CascadeType.PERSIST (cascade new), CascadeType.REMOVE (cascade delete), CascadeType.REFRESH (cascade refresh), CascadeType.MERGE (cascade update), CascadeType.ALL (all four)
* method one
* main table: ?@OneToOne(cascade = CascadeType.
ALL) * @PrimaryKeyJoinColumn
* public from table class get from table class(){return from table object}
* from table: no primary table class.
* Note: This method requires the primary table to correspond to the primary key value of the secondary table.
* Method 2
* Main table: ?@OneToOne(cascade = CascadeType.ALL)
* @JoinColumn(name="primary table foreign key") //The foreign key field in the database is specified here.
* public get from table class from table class(){return from table class}
* From table: @OneToOne(mappedBy = "Sub-table attribute in main table class")//Example main table User has a sub-table attribute which is Heart type heart, fill in heart here
* public main table class get main table class (){return main table object}
* Note: @JoinColumn is optional. The default value is the variable name of the slave table + "_" + the primary key of the slave table (note that the primary key is added here. Instead of the variable corresponding to the primary key).
* Method 3
* Main table: @OneToOne(cascade=CascadeType.ALL)
* @JoinTable( name="Associated table name",
* joinColumns = @JoinColumn(name="Main table foreign key"),
* inverseJoinColumns = @JoinColumns( name="Slave table foreign key")
* )
* From table: @OneToOne(mappedBy = "Slave table attribute in the main table class"


* Method one       
* @ManyToOne(cascade={CasCadeType.PERSIST,CascadeType.MERGE})
* @JoinColumn(name="foreign key")
* public main table class get main table class(){return main table object}
* method two
* @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE})
* @JoinTable(name="Related table name",
* joinColumns = @JoinColumn(name="foreign key to primary table"),
* inverseJoinColumns = @JoinColumns( name="Foreign key from table")
* )
* @OneToMany Set up a one-to-many association. The cascade attribute specifies the association level, refer to the description in @OneToOne. fetch specifies whether to delay loading. The value is FetchType.LAZY, which means delay, and FetchType.EAGER, which means immediate loading
. * Method 1 With this configuration, when adding "multi-terminal" to "one-side", the foreign key of "multi-terminal" will not be modified. When loading in "one end", you don't get "many end". If lazy loading is used, an exception will occur when reading the "many end" list, and the immediate load will be an empty collection (the collection element is 0) when the multiple end is obtained.
* "one end" configuration
* @OneToMany(mappedBy=""many end" attribute"
* public List<"multi-terminal" class> get "multi-terminal" list(){return "multi-terminal" list}
* "multi-terminal" configuration reference @ManyToOne.     
* Method 2
* "one-side" configuration
* @OneToMany(mappedBy=""multi-terminal" ")
* @MapKey(name=""multi-terminal" as the attribute of the Key")
* public Map<"multi-terminal" as the attribute of the Key, main table class> get "multi-terminal" list () {return " "Multi- terminal" list}
* "Multi-terminal" configuration reference @ManyToOne. 
* Method 3 Using this configuration, when adding "Multi-terminal" to "One-Side", you can modify the foreign key of "Multi-terminal".
* "one-end" configuration
* @OneToMany
* @JoinColumn(name=""multi-end" foreign key") 
* public List<"multi-end" class> get "multi-end" list(){return "multi-end" list}
* "multi-end" configuration Reference @ManyToOne.     
*    
*   
*/ 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326755415&siteId=291194637