Hibernate annotation and hibernate Annotation

Source: Internet
Author: User

Hibernate annotation and hibernate Annotation

 

1. Introduction to JPA annotations

Hibernate Annotation: A new way to replace the XML ing object relationship with annotations
JPA: Java Persistence API is part of the EJB3.0 specification, which specifically describes how to deal with Persistence specifications.

Common Methods:

• Use JPA standard annotations for object link ing

• Use Hibernate to perform persistence operations.

JPA annotation definitions are placed in the javax. persistence package.

Common annotations include:

• @ Entity

• @ Table

• @ Id

• @ Temporal

• @ Basic

• Column

• Optimistic lock @ Version in JPA annotations

 

A

Compile the entity test class:

 

2. Object relationship ing Based on JPA annotations

Applications of association ing in JPA Annotations:

1. One-to-many (@ onetoworkflow)

2. One-to-one (@ OneToOne)

3. many-to-many (@ ManyToMany)

 

One to multiple:

Category and Product are one-to-multiple relationships. Products and categories are multi-to-one.

First, configure one-to-many annotations under the Category class at one end,

 

MappedBy indicates that the maintenance relationship is reversed, which is equivalent to property_ref. The maintenance relationship is reversed and handed over to the other end to maintain the relationship.

Let's take a look at multiple terminals:

// Configure the link here and determine the link maintenance end and the maintained end. MappBy indicates that the link is maintained, and only the link has the right to update the foreign key. Here, we also note that the default onetoworkflow loading method is dependent on loading. When the last word in the Set link is "bytes", the load is lazy by default.

Here, JoinColum is set to set the foreign key name. Here, Product indicates the link maintenance end, and Category indicates the link maintenance end.

One to one:

2. One-to-one ing based on primary keys:

    It is not often used. This means that two tables use the same primary key and use Hibernate to customize the primary key generation policy, which is implemented by @ GenericGenerator. Reference myfor.

One-to-one based on foreign keys is relatively easier

  

3. many-to-many:

Taking Student and Course as an example, the relationship between students and courses is many-to-many.

 

 

JoinTable: An intermediate table is created as an association between two tables. After configuring the Student end, the Course end configuration is simple. You can simply use @ ManyToMany.

 

Ing name query

• NameQueries

  • NameQuery

 

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.