Hibernate and hibernate annotations

Source: Internet
Author: User

Hibernate and hibernate annotations

1 * @ Entity -- declare a class as an Entity bean (that is, a persistent POJO Class 2) 3 * @ Id -- the annotation declares the identity attribute of the object bean (corresponding to the primary 4 key in the table ). 5 * @ Table -- the annotation declares the specified table mapped to the object bean, and the directory 6 (catalog) and schema name 7 * @ Column -- Annotation declares the ing between attributes and columns. This annotation has the following octal 9 * name options, column name (default value: attribute name) 10 * unique options, whether to set unique constraints on this column (default value: false) 11 * nullable (optional) whether to set the value of this column to null (default value: false) 12 * insertable (optional) whether the column is used as a column in the generated insert Statement (default value: 13: true) 14 * updatable (optional) whether the column is used as a column in the generated update Statement (default value: 15 true) 16 * columnDefinition (optional, overwrite the SQL ddl fragment for this specific column (this may cause 17 to be unable to be transplanted between different databases) 18 * table (optional) define the corresponding table (default as the main table) 19 * length (optional, column Length (255 by default) 20 * precision (optional) decimal precision (default) Value: 21 0) 22 * scale (optional) if the column's decimal value range (decimal scale) is available, set this value to 23 (default value: 0) 24 * @ GeneratedValue -- Annotation declares the primary key generation policy. This annotation is similar to a subordinate 25 * strategy that specifies the generated policy (defined by JPA). This is a GenerationType. 27 The default value is GenerationType. AUTO 28 * GenerationType. the AUTO primary key is controlled by the program 29 * GenerationType. TABLE uses a specific database TABLE to save the primary key 30 * GenerationType. the IDENTITY primary key is automatically generated by the database (mainly for Automatically increasing type 31) 32 * GenerationType. SEQUENCE generates the primary key based on the SEQUENCE of the underlying database, with the condition that 33 databases support the SEQUENCE. (This value must be used with generator) 34 * generator specifies the generator used to generate the primary key (probably the 35 columns in the orcale sequence ). 36 * @ SequenceGenerator -- Annotation declares a database sequence. This annotation is shown in the 38 * name attribute under 37, indicating the name of the primary key generation policy of the table, it is referenced by 40 * sequenceName in the "gernerator" value set to 39 in @ GeneratedValue to indicate the database sequence name used to generate the policy. 41 * initialValue indicates the initial value of the primary key. The default value is 0. 42 * allocationSize: the size of each increase in the primary key value. For example, if it is set to 1, it indicates that 1 is automatically added after 43 new records are created each time. The default value is 50. 44 * @ GenericGenerator -- Annotation declares a primary key generation policy of hibernate. 45. Thirteen policies are supported. This annotation has the following attributes: 46 * name: Specify the generator name 47 * strategy: Specify the class name of a specific generator (specify the generation policy ). 48 * parameters: Obtain the parameters used by the specific generator specified by strategy. 49 * the values of the thirteen strategies (the value of the strategy attribute) are as follows: 50*1. native uses Sequence for orcale, and identity for MySQL and 51 SQL Server. 52 * native is to generate the primary key by the database, hibernate is not 53 (very commonly used) 54 * example: 55 @ GeneratedValue (generator = "paymentableGenerator") 56 * @ GenericGenerator (name = "paymentableGenera 57 tor ", strategy = "native") 58*2. the uuid uses the 128-bit uuid algorithm to generate a primary key. The uuid is encoded as 59 32-bit hexadecimal numbers. The occupied space is large (string type ). 60 * example: 61 @ GeneratedValue (generator = "paymentableGenerator") 62 * @ GenericGenerator (name = "paymentableGenera 63 tor", strategy = "uuid") 64*3. hilo needs to create an additional table in the database. The default table name is 65 hibernate_unque_key. The default field is of the integer type and the name is next_hi (less than 66). 67 * example: 68 @ GeneratedValue (generator = "paymentableGenerator") 69 * @ GenericGenerator (name = "paymentableGen 70 erator", strategy = "hilo") 71*4. assigned Key is processed by a program (frequently used). 72 This is the default generation policy when the <generator> element is not specified. It is equivalent to 73 AUTO in JPA. 74 * example: 75 @ GeneratedValue (generator = "paymentableGenerator") 76 * @ GenericGenerator (name = "paymentableGenera 77 tor", strategy = "assigned") 78*5. identity uses the auto-increment fields of SQL Server and MySQL. This 79 method cannot be placed in Oracle. Oracle does not support auto-increment fields. You need to set sequence (MySQL 80 and SQL Server are commonly used ). Equivalent to IDENTITY 81 in JPA * example: 82 @ GeneratedValue (generator = "paymentableGenerator") 83 * @ GenericGenerator (name = "paymentableGenera 84 tor", strategy = "identity ") 85*6. select uses the trigger to generate the primary key (mainly used for the early generation of the database master 86 key, rarely used) 87 * example: 88 @ GeneratedValue (generator = "paymentableGenerator ") 89 * @ GenericGenerator (name = "paymentableGenera 90 tor", strategy = "select") 91*7. sequence calls the sequence of the cautious database to generate the primary key. You must set the 92 column name in sequence. Otherwise Hibernate cannot be found. 93 * example: 94 @ GeneratedValue (generator = "paymentableGenerator") 95 * @ GenericGenerator (name = "paymentableGenerator", strat 96 egy = "sequence ", 97 * parameters = {@ Parameter (name = "sequence", value 98 = "seq_payablemoney")}) 99*8. seqhilo is implemented through the hilo algorithm, but the primary key history is stored in Sequence. 100 is applicable to databases that support Sequence, such as Orcale (rarely used). Example 101: 102 @ GeneratedValue (generator = "paymentableGenerator") 103 * @ GenericGenerato R (name = "paymentableGenerator", strat104 egy = "seqhilo", 105 * parameters = {@ Parameter (name = "max_lo", value = 106 "5 ")}) 107*9. when increnment inserts data, hibernate adds a 108 auto-increment primary key to the primary key, but a hibernate instance maintains a counter. Therefore, this method cannot be used when multiple instances run 109. 110 * example: 111 @ GeneratedValue (generator = "paymentableGenerator") 112 * @ GenericGenerator (name = "paymentableGenera113 tor", strategy = "increnment") 114*10. foreign uses the primary key of another related object. Usually used together with <one-115 to-one>. 116 * example: @ Id117 * @ GeneratedValue (generator = "idGenerator") 118 * @ GenericGenerator (name = "idGenerator", strategy = "119 foreign ", 120 * parameters = {@ Parameter (name = "property121", value = "info")}) 122 * Integer id; 123 * @ OneToOne124 * EmployeeInfo info; 125*11. guid uses the guid algorithm at the bottom of the database. It corresponds to the uuid () 126 function of MySQL, newid () function of SQL Server, and rawtohex (sys_guid () function of ORCALE ()) function 127 and other 128 * examples: 129 @ GeneratedValu E (generator = "paymentableGenerator") 130 * @ GenericGenerator (name = "paymentableGenerator", strat131 egy = "guid") 132*12. uuid. hex looks at Udi. We recommend that you replace 133 * With uuid. Example: 134 @ GeneratedValue (generator = "paymentableGenerator") 135 * @ GenericGenerator (name = "paymentableGenerator", strat136 egy = "uuid. hex ") 137*13. sequence-identity sequence policy extension, uses the instant retrieval policy 138 to obtain the sequence value. JDBC3.0 and JDK4 (including 1.4) versions 139 * example: 140 @ GeneratedValue (generator = "paymentableGenerator") 141 * @ GenericGenerator (name = "paymentableGenerator", strat142 egy = "sequence-identity ", 143 * parameters = {@ Parameter (name = "sequence144", value = "seq_payablemoney")}) 145*146 * @ OneToOne sets one-to-one association. The cascade attribute has five values (only 147 CascadeType. ALL is easy to use? It's strange), which is CascadeType. PERSIST (cascade new), 148 CascadeType. REMOVE (cascade delete), CascadeType. REFRESH (cascade REFRESH), 149 CascadeType. MERGE (cascade update), CascadeType. ALL (ALL four items) 150 * method 1 151 * master table :? @ OneToOne (cascade = CascadeType. ALL) 152 * @ PrimaryKeyJoinColumn153 * public slave table class get slave table class () {return slave table object} 154 * slave table: no master table class. 155 * Note: This method requires that the primary table correspond to the primary key value of the slave table. 156 * method 2 157 * main table :? @ OneToOne (cascade = CascadeType. ALL) 158 * @ JoinColumn (name = "") // specifies the foreign key field in the 159 database. 160 * public slave table class get slave table class () {return slave table 161 class} 162 * slave table: @ OneToOne (mappedBy = "") // For example, in the master table of 163, the User has a Heart-type slave table attribute. Set this parameter to 164 heart165 * public master table class get master table class () {return primary table for 166 objects} 167 * Note: @ JoinColumn is optional. The default value is the primary key from the table variable name + "_" + from the 168 table (note that the primary key is added here. Instead of the variable corresponding to the primary key ). 169 * method 3 170 * main table: @ OneToOne (cascade = CascadeType. ALL) 171 * @ JoinTable (name = "Join table name", 172 * joinColumns = @ JoinColumn (name = ""), 173 * inverseJoinColumns = @ JoinColumns (name = "foreign key from Table 174") 175 *) 176 * slave table: @ OneToOne (mappedBy =" ") // In the first example, the User in the primary table has a Heart-type slave table attribute. Enter 177 heart179 * public Primary table class get primary table class () {return primary table for 180 objects} 181 * @ ManyToOne set multiple-to-one associations 182 * method 1 183 * @ ManyToOne (cascade = {CasCadeType. P ERSIST, CascadeType. MERGE184}) 185 * @ JoinColumn (name = "foreign key") 186 * public Primary table class get primary table class () {return primary table object} 187 * method 2 188 * @ ManyToOne (cascade = {CascadeType. PERSIST, CascadeType. MERGE189}) 190 * @ JoinTable (name = "Join table name", 191 * joinColumns = @ JoinColumn (name = ""), 192 * inverseJoinColumns = @ JoinColumns (name = "foreign key from table") 193 *) 194 * @ onetoworkflow sets one-to-multiple associations. Specify the association level for the cascade attribute. Refer to the description in 195 @ OneToOne. Fetch specifies whether to delay loading. The value is FetchType. LAZY indicates a 196 delay, which is FetchType. the EAGER means to immediately load 197 * method 1 using this configuration. When "Multi-terminal" is added to "one end", 198 does not modify the "Multi-terminal" foreign key. When "one end" is loaded, there is no "Multi-terminal ". If 199 is loaded with a delay, an exception occurs when reading the "Multi-terminal" list. Loading immediately results in a 200 empty set (the Set element is 0 ). 201 * "one end" configuration 202 * @ onetovel (mappedBy = "" Multi-terminal "attribute") 203 * public List <"Multi-terminal" Class> get "Multi-terminal" List () {return "more than 204 ends" list} 205 * "Multi-terminal" configuration reference @ ManyToOne.206 * method 2 207 * "one end" configuration 208 * @ onetoworkflow (mappedBy = "" Multi-terminal "attributes ") 209 * @ MapKey (name = "" Multi-terminal "as the Key attribute") 210 * public Map <"Multi-terminal" as the Key attribute class, main table class> get "Multi-terminal" 211 list () {return "Multi-terminal" list} 212 * "Multi-terminal" configuration reference @ ManyToOne.213 * method 3 use this configuration, when adding "Multi-terminal" for "one end", you can modify the "Multi-terminal" foreign key by 214. 215 * "one end" configuration 216 * @ OneToMany217 * @ JoinColumn (name = "" multi-port "foreign key ") 218 * public List <"multiple terminals"> get "multiple terminals" List () {return "more than 219 terminals" List}

 

Related Article

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.