Repeated Records are queried using criteria in the bag set of fatch = eager in hibernate (a Java list ).

Source: Internet
Author: User

This issue is described in detail in section 13.2.3 of jpwh!

Example: forumgroup and Forum

 Public class forumgroup {<br/> @ onetoworkflow (fetch = fetchtype. eager, mappedby = "group") <br/> private list <Forum> Forums = new arraylist <Forum> (); <br/>}< br/> public class Forum {<br/> @ manytoone (fetch = fetchtype. lazy) <br/> @ joincolumn (name = "groupid", nullable = false) <br/> @ Org. hibernate. annotations. foreignkey (name = "fk_forum_groupid_forumgroup") <br/> private forumgroup group; <br/>}

Since forumgroup's forums is configured as eager fetch, that is to say, when loading forumgroup, it will load all its Forum together. This process is implemented through left out join. The following is the SQL statement produced by hibernate.

Select
This _. ID as id3_1 _,
This _. creationtime as creation2_3_1 _,
This _. description as descript3_3_1 _,
This _. modifiedtime as modified4_3_1 _,
This _. Name as name3_1 _,
Forums2 _. groupid as groupid3 _,
Forums2 _. ID as ID3 _,
Forums2 _. ID as id2_0 _,
Forums2 _. creationtime as creation2_2_0 _,
Forums2 _. description as descript3_2_0 _,
Forums2 _. groupid as groupid2_0 _,
Forums2 _. modifiedtime as modified4_2_0 _,
Forums2 _. Name as name2_0 _
From
Forumgroup this _
Left Outer Join
Forum forums2 _
On this _. ID = forums2 _. groupid

Assume that there is one forumgroup and three Forum groups. The preceding SQL statement generates three records. So how does hibernate encapsulate the three pieces of data? From the actualCodeIn the running results, we can see that hibernate actually only generates one forumgroup instance and three Forum instances. However, if you use loadallforumgroup, three forumgroup references will be returned, pointing to the instance of the same forumgroup!

If forumgroup's forums is configured as lazy fetch, this problem will be solved. But this just avoids this problem!

What is the correct solution?

To filter duplicate objects in join and fetch, you can use either of the following methods:

1. encapsulate the sequencer into set. For example: Set nodupes = new javashashset (resultlist ))

2. Use distinct. For example, select distinct I from item I join fetch I. Bids

 

Another problem is:

When using JPA for annotation, if one entity needs to map multiple set entities, we cannot set the fetchtype of the Two sets to eager. In this case, we can only set it to lazy, otherwise it will report:Cannot Simultaneously Fetch Multiple Bags. Or we can also use @ indexcolumn (adding it will not be a bag set, but a list set ). That is to say, Hibernate does not allow capturing multiple bags at a time.

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.