No row with the given identifier exists [artproject. domains. Users #2]

Source: Internet
Author: User

 

Cause:

There are two tables, table1 and table2. are associated with <one-to-one> or <allow-to-one unique = "true"> (Special multiple-to-one ing, actually, it is one-to-one) to associate table2. when hibernate searches, the data in Table2 does not match Table1, so it will reportNo Row With The Given Identifier ExistsThis error. (in one sentence, it is a data problem !)

Assume that table 1 has its own primary key id1 and table 2's primary key Id2.

If the hibenrate is set to a single association, even if Id2 in Table1 is null and Id2 in Table2 has a value, no query error will occur. however, if the Id2 field in Table 1 has a value but this value does not exist in the primary key value in table 2, the above error will be reported!

If Hibernate is a bidirectional Association, Id2 in Table1 is null, but if there is a value in Table2, this error is reported. in this case, the current solution is to change to a single association, or change the corresponding data!

This is the cause of this error. If you know the cause, you can modify it accordingly. Maybe some people are confused that the hibernate Association has been configured. How can this problem occur? In fact, this is a problem in programming. For example, when I add information, the formbean of struts passed through the page to the DAO method needs to be encapsulated into the Po of hibernate (that is, the bean of hibenrate), if one by one PO. get (form. set () is too troublesome. In this case, a special method is usually written to encapsulate it. get (form. set () directly uploads the formbean object of struts to this method for encapsulation. If I have a field that is the creator ID, this field will never be changed, I also called this method when I added it. This specially encapsulated method has some judgment. If I judge it, if the ID of the Creator is passed as a null value, in my opinion, if it is a null value, I set the Creator ID to 0, but the userid in the User table is auto-incrementing since the primary key starts from 1, so the data will not be matched, this error will occur after a query. this error often occurs at the beginning of development, because each module is integrated after independent development by the corresponding person, and each person writes a ticket. When it comes to integration, these problems are often ignored, so these problems come out all of a sudden... integration is very hard, tnnd!

Comparison of hibernate queries
There are many hibernate queries, such as query, find, criteria, get, and load.

Using hsql statements in query, you can set parameters as a common method.

Criteria tries to avoid writing hql statements and looks more object-oriented.

Find method, which has been discarded by the new hibernate

The get and load methods are used to obtain a record based on the ID.
The difference between get and load is described in detail below, because sometimes find will be added for comparison.

1. Compare the returned results:
The Org. hibernate. objectnotfoundexception exception will be thrown if the load method cannot be retrieved.
If the get method cannot be retrieved, null is returned.

2. Comparison of retrieval execution mechanisms:
Both the get and find methods are retrieved directly from the database.
The execution of the load method is complicated.
1. First, check whether the persistent context of the session has been cached. If yes, the system returns
2. If not, the system determines whether it is lazy. If it does not directly access the database for retrieval, the system checks the record and returns the result. If it does not, an exception is thrown.
3. If it is lazy, a proxy object needs to be created. The initialized attribute of the object is false, and the target attribute is null.
4. When accessing the properties of the obtained proxy object, retrieve the database. If a record is found, copy the object of the record to the target of the proxy object.
And set initialized = true. If it cannot be found, an exception is thrown.

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.