Using annotation for hibernate to complete o/R Mapping

Source: Internet
Author: User

I. Environment setup and basic ing
1) Add an annotation package:
Hibernate-annotations.jar, ejb3-persistence.jar, hibernate-commons-annotations.jar
2) try to use the JPA standard annotation in the object class for object link ing. Annotations can be added to attributes or the getxxx () method.
A) @ entity ing an object class
@ Table (name = "table name") specifies the associated table
B) @ ID OID ing OID
C) @ generatedvalue (Strategy = Generation Policy) specifies the oId generation policy.
The default value is generationtype. Auto, which is equivalent to native in XML.
If you do not use this annotation to define the ID, the oId value is manually specified. By default, the JPA annotation does not provide the uuid method.
D) @ version ing version attributes (optimistic lock)
E) @ column (name = "column name", nullable = true, unique = true) specifies the information of the column corresponding to the attribute.
F) @ temporal (temporaltype. timestamp) specifies the date and time type. (Timestamp, date, time)
G) Simple attributes do not need annotations. The default value is @ basic.
H) @ transient specifies that the attribute does not require persistence.
I) complex attributes: Association, inheritance, components, and primary keys.
3) use the declared ing Class in the hibernate global configuration file: <mapping class = "Full qualified name of the Object Class"/>
4) When annotation is used to map object relationships, the annotationconfiguration class is used to load the hibernate global configuration file. The following code:

Sessionfactory factory = new annotationconfiguration (). Configure (). buildsessionfactory ();
5) The persistence operation is no different from the previous one.

Ii. ing Association
1. Map multiple to one
1) @ manytoone
2) Specify the join column @ joincolumn (name = "xxx_id ")

2. Map one to multiple
1) @ onetoworkflow uses a connection table for one-to-many association by default.
2) After @ joincolumn (name = "xxx_id") is added, a foreign key Association is used instead of a connection table.

 

3. ing
1) on multiple terminals:
@ Manytoone
@ Joincolumn (name = "foreign key name ")
2) at one end: (one-to-many Association, giving the link maintenance permission to multiple terminals is more efficient)
@ Onetoworkflow (mappedby = "Multi-end Association attribute name ")
@ Joincolumn (name = "foreign key name ")

 

4. mappedby attribute: Used in two-way Association, the maintenance right of the relationship is reversed. Similar to the property-ref in hibernate XML ing.

 

5. Cascade attribute: Specifies the behavior of cascade operations (multiple options are allowed)
Cascadetype. persist: Calls persist () in the JPA specification. It is not applicable to the SAVE () method of hibernate.
Cascadetype. Merge: the update () method of Hibernate is not applicable when Merge () in JPA specifications is called.
Cascadetype. Remove: This method is applicable to the delete () method of hibernate when you call remove () in the JPA specification.
Cascadetype. Refresh: Applicable to the flush () method of hibernate when calling refresh () in the JPA Specification
Cascadetype. ALL: all persistence methods in the JPA specification. Applicable to All persistence methods in hibernate

6. When the cascade attribute and mappedby are used together, the relationship must be established by calling the Set Method of both parties.

 

7. Bidirectional one-to-one
1) based on foreign keys
A) at the prosecution: @ onetoone
B) under prosecution: @ onetoone (mappedby = "")

2) based on primary keys: the JPA standard does not provide a standard method for generating Shared primary keys. You need to use hibernate extension.
A) at the primary prosecution: Car
@ ID
@ Generatedvalue (generator = "My-UUID ")
@ Org. hibernate. Annotations. genericgenerator (name = "My-UUID", strategy = "UUID ")
Private string ID;
@ Onetoone (cascade = {cascadetype. All })
@ Primarykeyjoincolumn
Private Brand brand;
 
B) under the prosecution: Brand
@ ID
@ Generatedvalue (generator = "myfg ")
@ Org. hibernate. Annotations. genericgenerator (name = "myfg ",
Strategy = "foreign", parameters = @ parameter (name = "property", value = "car "))
Private string ID;
@ Onetoone (mappedby = "brand ")
Private car;

 

8. Bidirectional many-to-many: it is better to maintain this relationship at one end to be more efficient. Example: the many-to-many relationship between students and courses
1) on the master prosecution:
@ Manytoyun
@ Jointable (name = "student_course ",
Joincolumns = {@ joincolumn (name = "student_id ")},
Inversejoincolumns ={@ joincolumn (name = "course_id ")})
Private set <course> courseset = new hashset <course> ();
2) under prosecution:
@ Manytoyun (mappedby = "courseset ")
Private set <student> stus = new hashset <student> ();
3) In many cases, you need to split two-way multiple-to-multiple pairs into two ones: 1 --> * <-- 1

 

Iii. Advanced ing
1. Inheritance ing:
1) the entire inheritance tree is a table
Add an annotation from bottom to the parent class
@ Entity
@ Inheritance (Strategy = inheritancetype. single_table)
@ Discriminatorcolumn (name = "type", length = 3)
@ Discriminatorvalue ("U ")
Add the following annotation to the subclass
@ Entity
@ Discriminatorvalue ("W ")

2) one table for each subclass
Add the following annotations to the parent class:
@ Entity
@ Table (name = "user ")
@ Inheritance (Strategy = inheritancetype. Joined)
The ing between a subclass and a common object class is the same.

3) one table for each specific class:
In the parent class
@ Entity
@ Inheritance (Strategy = inheritancetype. table_per_class)
Public class user {
@ ID
@ Generatedvalue (Strategy = generationtype. Table, generator = "xxgen ")
@ Tablegenerator (name = "xxgen", allocationsize = 1)
Private long ID;
...
}
The ing between a subclass and a common object class is the same.

 

2. Component ing: @ emabbedable is used in the component class. Use this component class:
@ Emabbed
@ Attributeoverrides ({
@ Attributeoverride (name = "email", column = @ column (name = "p_email ")),
@ Attributeoverride (name = "Address", column = @ column (name = "p_address ")),
@ Attributeoverride (name = "mobile", column = @ column (name = "p_mobile "))
})

 

3. federated primary key ing
1. Primary Key class: ing with @ emabbedable. Implement the serializable interface and use the primary key attribute to override the hashcode () and equals () methods.
2. Use @ ID ing for the primary key class.

4. Define a name query using annotations

 

5. Extended annotation of Hibernate

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.