"Hibernate learning"--crawl strategy (annotation method)

Source: Internet
Author: User

Hibernate Gets the policy of the associated object when the application needs to navigate between the associated relationships .

How to crawl a policy:

fetchtype.lazy: Lazy loading, when loading an entity, the properties that define lazy loading are not immediately loaded from the database.

Fetchtype.eager: When loading an entity, the properties that define the urgent load are immediately loaded from the database.

reflected in the project, I have two entities here, one entity that puts the underlying data, and the other is the entity that records the operations. The relationship between the two entities is a one-to-a-relationship, so the primary key in the underlying data is placed in the operation record as a foreign key, and the query Operation record table by the way, and the related information of the base table is brought out:

Here is the way to annotate:

Private company Company, @OneToOne (Fetch=fetchtype.eager) @JoinColumn (name= "company_id") Publiccompany Getcompany () { Returncompany;} Publicvoid Setcompany (company company) {this.company= Company;}

Capture strategy embodiment: Fetch=fetchtype.eager, here we query to immediately show out, so choose the urgent load.

Here's a question:

Could not Determinetype For:com.tgb.xyb.entity.Company, @ Table:operaterecord, for columns:[ Org.hibernate.mapping.Column (company)]

Solution: Annotations are placed in Get method Above (note: Originally put annotations on the method)

because the get method is an external way to get the inner properties of a class, everything that needs to get a property or object is declared on the get method. For example , Spring 's dependency injection is also injected from the get method.

The advantage of using a crawl strategy now is that we only need to add this strategy, then the corresponding entity will be associated. We don't have to write the corresponding HQL statement to associate or create a new entity to associate the two entities.

"Hibernate learning"--crawl strategy (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.