Annotation mappings must meet two major conditions: Hibernate3.2 above and Jsee 5.
1. Standard Notes
@Entity class annotation, all classes to be persisted have
@Entity
public class Org implements Java.io.Serializable {
}
@Id PRIMARY Key
@Column (name= "...") this property corresponds to the field in the table and does not have a name representation
@Table Objects and table mappings
@UniqueConstraint UNIQUE Constraint
@Version method and field level, optimistic lock usage, returning numbers and timestamp, numbers as preferred
@Transient transient properties, indicating that no processing is required
@Basic the most basic comment. There are two properties: fetch whether delay loading, optional allow null
@Enumerated Enumeration Type
@Temporal date conversion. Default conversion timestamp
@Lob are usually used concurrently with @basic to improve access speed.
@Embeddable class level, the table can be embedded
@Embedded Method field level, the table is used with the embedded object and @embeddable
@AttributeOverrides Property Overrides
@AttributeOverride the contents of the property overrides are nested with @attributeoverrides
@SecondaryTables Multiple table mappings
@SecondaryTable Define the secondary table mappings and @secondarytables nested with
@GeneratedValue identifier generation policy, default auto
Table-Table Relationship Mapping
@OneToOne: One-to-one mapping. It contains five properties:
Targetentity: Associated Target class
Cascade: cascading operation when persisted, default No
Fetch: Gets the way of the object, default eager
Optional: Whether the target object is allowed to NULL, default allow
Mappedby: Defines the subordinate classes in a two-way association.
There are several types of 2.Hibernate annotation relational mappings:
1 One-to-one foreign Key Association mapping (one-way)
@OneToOne (Cascade=cascadetype.all)
@JoinColumn (name= "userid", Unique=true)
One-to-one foreign Key Association, using @onetoone, and setting cascading operations. @JoinColum the name of the foreign key is userid (the database field name), and if not set, the default is the property name + _id for the other class, and the value of the foreign key is unique (unique).
2 One-to-one foreign Key Association mapping (bidirectional)
In the Class1, like the above,
CLASS2:
@OneToOne (mappedby= "Class2", Cascade=cascadetype.all)
One-to-one two-way association relationship, using @onetoone
Note: the need to add mappedby= "Class2", if not added, Class2 will also generate a foreign key (class1_id), mappedby= "Class2" need to point to the object associated with an attribute, the two-way association relationship, There is and only one end exists as the principal (owner) end, and the principal side is responsible for maintaining the join column, declaring from the table that the relationship is not needed to be maintained by the Mappedby property, and the Mappedby value pointing to the principal's associated property
Law: Only two-way relationship, plus mappedby
3) (embedded object) component mapping
Mapping another class as part of the entity class;
Note: 1. Classes that become part of other entity classes are not annotated as @entity entity classes.
2. Use @embedded to annotate the class;
3. Component property name to ensure that there is no conflict with entity class property names, you can use the following annotation:
3.1 uses the following form: 1 2 3 4 5 6 7 @AttributeOverrides ({@AttributeOverride (name= "xx", column= @Column (n Ame= "xxx"), @AttributeOverride (name= "xx2", column= @Column (name= "xxx2"))
3.2 In an embedded object, set its properties using @column
4) One-to-many correlation mapping (one-way)
@OneToMany
@JoinColumn (name= "OrgID")
/**
* One-to-many annotation @onetomany (one-way)
* If only write @onetomany, hibernate will build a middle table to
* To maintain the relationship between them,
* Plus @joincolumn (name= "OrgID"), will not build the middle table, he will be in
* Multi-end plus foreign key orgid, to maintain their relationship
*/
5 One-to-many Correlation Mapping (bidirectional)
End:
@OneToMany (mappedby= "org")
@JoinColumn (name= "OrgID")
/**
* One-to-many bi-directional, set mappedby at one end
* Description of the one end of the dominant
* If a foreign key field name is specified, the multiple end also needs to specify the same field name
*/
Multi -:
@ManyToOne
@JoinColumn (name= "OrgID")
/**
* One-to-many bi-directional
* You need to specify that the foreign key is consistent with the foreign key name given to one end, @JoinColumn (name= "OrgID")
* Also can not specify, if at the end of a number of unspecified, then one end can not specify
* Otherwise generate two foreign keys
*/
6) Multi-pair correlation mapping
On one end of the configuration:
@ManyToOne (Targetentity=organization.class)
@JoinColumn (name= "OrgID")
Multi-pair Annotation @manytoone
targetentity Specifies the associated object
@JoinColumn (name= "OrgID") specifies the field name of the foreign key that is produced, by default org_id
7 Multi-pair multi-correlation mapping (one-way)
@ManyToMany
/**
* Many-to-many Mapping: annotation @manytomany (one-way)
* By default, Hibernate automatically creates an intermediate table,
* To maintain the many-to-many relationship
* Default Intermediate table name: User_role middle table, field name user_id role_id
* If you want to replace the table name and field name, note the following:
*/
@JoinTable (name= "T_u_r",
joincolumns={@JoinColumn (name= "u_id")},
inversejoincolumns={@JoinColumn (name= "r_id")}
)
8 Multi-pair multi-correlation mapping (bidirectional) User end
@ManyToMany
/**
* Many-to-many Mapping: annotation @manytomany (one-way)
* By default, Hibernate automatically creates an intermediate table,
* To maintain the many-to-many relationship
* Default Intermediate table name: User_role middle table, field name user_id role_id
* If you want to replace the table name and field name, note the following:
*/
@JoinTable (name= "T_u_r",
joincolumns={@JoinColumn (name= "u_id")},
inversejoincolumns={@JoinColumn (name= "r_id")}
)
/**
* @JoinTable (name= "T_u_r",
* Specifies the table name of the intermediate table
* joincolumns={@JoinColumn (name= "u_id")},
* Specifies the foreign key of the current object
* inversejoincolumns={@JoinColumn (name= "r_id")}
* Specifies the foreign key of the associated object
*/
Role End
@ManyToMany (mappedby= "role")
/**
* Many-to-many, bidirectional correlation mapping
*/