Hibernate -- use annotations to configure mappings, and hibernate -- Annotations
Preface:
You can configure the ing between object classes and databases in either of the following ways:
1. Use *. hbm. xml;
2. Use @ Annotation
I. annotation method:
1. @ Entity
Before the class, declare the class as a persistent class.
2. @ Table
Before the class, declare the associated table name for the class. If the table name and class name are the same, you can omit them.
Eg: @ Table (name = "tbl_role ")
3. @ Proxy
Before the class, modify the category-level Indexing policy. The default value is delayed loading.
Eg: @ Proxy (lazy = false) // change to load now
4. @ ID
Add it before the get/set Method of the primary key, or add it before the property to supplement the property:
@ GeneratedValue-primary key auto-increment policy,
@ GeneratedValue (Strategy = GenerationType. Auto). The default value is Auto.
Generally used with @ GenericGenerator:
Eg: @ GenaratedValue (generator = "native_name ")
@ GenericGenerator (name = "native_name", strategy = "native") // The primary key generation policy is native.
Note that the value of generator is consistent with that in name.
5. @ Column
If the attribute name and the corresponding database table column name are different, or the attribute name corresponding to the get/set method is not the column name, you need to add it before the get/set method, or add it before the attribute
Eg: @ Column (name = "RoleName ")
Association:
===
6. @ ManyToOne -------- multi-to-one (foreign key) Association
Eg: @ ManyToOne (targetEntity = class_name.class)
@ JoinColumn (name = "c_name") // multiple pairs of foreign key column names
7. @ onetoworkflow -------- one-to-Multiple Association attributes
Eg: @ onetoty (targetEntity = class_name.class, mappedBy = "property_name", cascade = {CascadeType. ALL })
If the Set is not generic, you need to add targetEntity mappedBy to the attribute associated with targetEntity to maintain the foreign key relationship.
8. @ manytoyun-many-to-many
Eg: @ manytoty (targetEntity = Function. class)
@ JoinTable (name = "role_function_r", joinColumns = @ JoinColumn (name = "roleId"), inverseJoinColumns = @ JoinColumn (name = "functionId "))
In the figure (1), it indicates the name of the intermediate table with many-to-many relationships, and (2) it indicates the foreign key relationship between the intermediate table and the current object-class table. (3) indicates the foreign key relationship between the intermediate table and another table.
===
9. @ Transcient
Add it before the get/set method, or add it before the property to declare the property does not need to be persisted.