Questions and solutions about hibernate querying for duplicate data

Source: Internet
Author: User

First of all, let me explain what I've been through so that you can decide if you need to continue to read this article:

Suppose I have a table with 4 fields, called ABCD, the primary key is a, corresponding I have a pojo and a hbm file, Pojo Nothing, the <id> tag in the hbm file corresponds to B (perhaps you say how this happens, In fact, the reality is that I encountered in a view of the problem, there is not only 4 fields, I write this is to hope that you can understand the fastest.

OK, here's the question. I used B as a condition to query the table, let us define the condition for b=1, my table matches the criteria of the data there are 2, of course, the value of their a field is different (a a=1, a a=2), the result of the query is: two identical data (that is, two are a=1 or a=2).


I described the problem in front of you, and if you come across the same as me, you can continue to watch, perhaps you have seen in my description above the problem. Yes, because the primary key setting in HBM, because <id> is set to a non-unique field, Hibernate considers it to be unique, Causes the query out two data to have the same B field value, hibernate directly re-uses the first isolated data, because it thinks the second article since the primary key is the same, the other also must be the same.


Since the explanation, the solution is:

1. Find the real unique field of your entity, mark it as <id> (remember to place <id> this tag before other <property> in hbm file, otherwise ...) You're going to do this.

2. Using the federated primary key, this I did not try, online examples are also many, search for hibernate joint primary key, you have the answer.


Well, if this article is helpful to you, just reply below and let me know that what I wrote is not in vain. Or if you have any questions, you can stay.

Refusing to do the hand party, start from me!

Questions and solutions about hibernate querying for duplicate data

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.