[Hibernate] a confusing place for IDS

Source: Internet
Author: User

After using hibernate for so long, I have encountered a problem that I have never encountered. Fortunately, I am quick-thinking and good at Lenovo, so that I can discover and solve the problem in a short time. The following is a detailed description.

I added a method in the hibernatedao class as follows:

Public object getbyid (class clazz, string ID) throws hibernateexception {
Return Session. Find ( " From " + Clazz. getname () + " O where O. ID =? " , ID, hibernate. String). Get ( 0 );
}

You know, the primary keys of my PO class are named oid. Coincidentally, some po classes not only have the oId attribute, but also have the property named ID to indicate the business number, such as the instructor number and the education level number. These classes always report the following exceptions when using this method:

Java. Lang. indexoutofboundsexception: Index: 0 , Size: 0  

After being depressed for half an hour, I finally thought, will it be the O. ID in the query statement? Does hibernate think that the ID here represents the primary key oid? In debug, we changed the parameter to the oId value, and found that it is true!

Solution: for the moment, I do not know which methods can escape. However, the ID attribute is indeed a bit ambiguous. Change it to code.

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.