hibernate--Comprehensive summary of various mappings based on annotation method

Source: Internet
Author: User
Tags uuid

Transferred from: http://www.cnblogs.com/cuizhf/archive/2013/04/08/3006767.html

1. Using hibernate annotation To do object relational mapping

1) to add the mandatory package:

hibernate-jpa-2.0-api-1.0.0. Final.jar

2Add JPA's 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) Use the declarative mapping class in the Hibernate global configuration file:

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



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

5The persistence operation is not different from the previous.



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) Add @joincolumn (name= "xxx_id"), the Foreign Key association is used instead of the join table.




5. mapping bidirectional one-to-many

1) in multi-terminal:

@ManyToOne

2) At one end:

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

@JoinColumn (name= "foreign Key Name")



6. Cascade Property: Specifies the behavior of the cascade operation (multiple choices 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. Cascade properties are used together with mappedby, it is important to establish a relationship by invoking the set method of both parties.







10. Two-way One

1) based on the foreign key

A) in the main control: @OneToOne

b) In the accused party: @OneToOne (mappedby= "The associated property name of the other party")



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 mappings:

1. Entire Inheritance tree a table

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. One table per concrete 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
  in component classes with @emabbedable

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 Mappings

  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.

hibernate--Comprehensive summary of various mappings based on annotations

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.