Hibernate comprehensive summary of various mappings based on annotation method

Source: Internet
Author: User

1. Use hibernate annotation to do object relational mapping

1) Add the mandatory package:

Hibernate-jpa-2.0-api-1.0.0.final.jar

2) Add the JPA standard annotations to the entity class for Object Relational mapping. Annotations can be added to attributes or added on the GetXXX () method.

A) @Entity mapping an entity class

@Table Specify the associated table

b) @Id Mapping OID

c) @GeneratedValue the generation policy for the specified OID

d) @Version Map version number properties

e) @Column the information for the column that corresponds to the specified attribute

f) @Temporal the type of date time specified (Timestamp,date,time)

g) Simple attributes can be used without annotations. The default is @basic.

h) @Transient The specified attribute does not require mapping

i) Complex attributes: Association, inheritance, component, federated primary Key, collection

3) How to use the claim mapping class in the Hibernate global configuration file:

<mapping class= "Fully qualified name of the entity class"/>

4) When using annotation to map object relationships, load hibernate global configuration to use the Annotationconfiguration class

5) persistence does not differ from the previous operation.

2. Hibernate Annotation Basic Mapping

3. Mapping many-to-one

1) @ManyToOne

2) Specify the associated column @joincolumn (name= "xxx_id")

4. Mapping one-to-many

1) @OneToMany Use the Join table to make a one-to-many association by default

2) after adding @joincolumn (name= "xxx_id"), the Foreign Key association is used instead of the join table.

5. Mapping bidirectional one-to-many

1) at the multi-terminal:

@ManyToOne

2) at one end:

@OneToMany (mappedby= "Multi-Port Association attribute name"):----Upgrade--@OneToMany

@JoinColumn (name= "foreign Key Name")

6. Cascade Property: Specifies the behavior of the Cascade operation (multiple options available)

Cascadetype.persist: Calling PERSIST () in the JPA specification, not for Hibernate's Save () method

Cascadetype.merge: Does not apply to Hibernate's update () method when calling the JPA specification for MERGE ()

Cascadetype.remove: Delete () method for Hibernate when calling remove () in the JPA specification

Cascadetype.refresh: Flush () method for Hibernate when calling REFRESH () in the JPA specification

All persistence methods in the CascadeType.ALL:JPA specification.

7. Mappedby attribute: Use in two-way correlation, reverse the maintenance right of the relationship

Same as the property-ref in Hibernate XML mappings.

8. When Cascade properties and Mappedby are used together, the relationship must be established by invoking the set method of both parties.

10. Bidirectional one-to-one

1) based on foreign key

A) in the main control: @OneToOne

b) In the accused party: @OneToOne (mappedby= "each other's associated attribute name")

2) based on primary key: The JPA standard does not provide a standard method for sharing primary key generation problems, using Hibernate extensions

A) in the main control: Car

@Id

@GeneratedValue (generator= "My-uuid")

@GenericGenerator (name= "My-uuid", strategy= "UUID")

Private String ID;

@OneToOne (Cascade={cascadetype.all})

@PrimaryKeyJoinColumn

Private brand Brand;

b) In the accused party: Brand

@Id

@GeneratedValue (generator= "MYFG")

@GenericGenerator (name= "MYFG", strategy= "foreign", [email protected] (name= "Property", value= "Car"))

Private String ID;

@OneToOne (mappedby= "brand")

Private car car;

11. Bidirectional many-to-many

1. On the main control side:

Many-to-many from students to courses: it's better to maintain the relationship at one end.

@ManyToMany

@JoinTable (name= "Student_course",

joincolumns={@JoinColumn (name= "student_id")},

inversejoincolumns={@JoinColumn (name= "course_id")})

Private set<course> Courseset = new hashset<course> ();

2. On the accused party:

Many-to-many students in the course

@ManyToMany (mappedby= "Courseset")

Private set<student> Stus = new hashset<student> ();

12. Split bidirectional Many-to-many into two one-to-many: 1-->*<--1

13. Inheritance Mapping:

1. A table for the entire inheritance tree

Add from next annotation in parent class

@Entity

@Inheritance (strategy=inheritancetype.single_table)

@DiscriminatorColumn (name= "type", length=3)

@DiscriminatorValue ("U")

The following annotations are added to the child class

@Entity

@DiscriminatorValue ("W")

2. A table for each sub-class

Add the following annotations to the parent class

@Entity

@Table (name= "user")

@Inheritance (strategy=inheritancetype.joined)

Same as the normal entity class in the subclass

3. A 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;

...

}

Same as the normal entity class in the subclass

14. Component Mappings

@emabbedable in component classes

In the use of this component class

@Emabbed

@AttributeOverrides ({

@AttributeOverride (name= "email", [email protected] (name= "P_email")),

@AttributeOverride (name= "Address", [email protected] (name= "p_address"),

@AttributeOverride (name= "mobile", [email protected] (name= "P_mobile"))

})

15. Federated PRIMARY Key Mapping

1. Primary KEY class: Map with @emabbedable. and implement the Serializable interface, overriding the Hashcode () and Equals () methods using the primary key property.

2. Use this primary key class in the class. Map with @id.

Reference address

Hibernate comprehensive summary of various mappings based on annotation method

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.