(GO) Collection: Hibernate FAQ No row with the given identifier exists the cause and solution of the problem

Source: Internet
Author: User
Tags throw exception

Hibernate No row with the given identifier the cause and solution of exists problem

The cause of this issue occurs:

There are two tables,table1 and table2. This problem is caused by the fact that Table1 has made a connection <one-to-one> or <many-to-one unique= "true" > (a special many-to-one mapping, in fact, is to relate table2.) When hibernate is looking for, the data in table2 does not match the Table1, so it will be reported no row with the given identifier exists this mistake. ( A word, is the problem of data!)

If said,Table1 has its own primary key id1, there are Table2 's primary key id2, these two fields .

If hibenrate sets a single association, even if id2 in table1 is a null value, there is a value in Id2 in Table2, and the query does not go wrong. But if the Id2 field in Table1 has a value, but this value is not in the primary key value in Table2, it will be reported the error !

If Hibernate is a two-way association, then id2 in table1 is a null value, but if there is a value in the Table2, the error is reported. The current solution to this situation is to change to a single link, or to change the data that does not correspond!

This is the reason to report this mistake, know the reason for the corresponding change on the line. Maybe some people are confused. Hibernate associations are well-matched, how can there be such a mistake? In fact, this is the problem of programming, if I add information, The page passed the Struts Formbean to the DAO method needs to be encapsulated into Hibernate po (that is, hibenrate bean), if a po.get (Form.set ()) is too troublesome, this will generally write a special method to encapsulate , encountered Po.get (Form.set ()) This situation directly to the struts Formbean object to this method encapsulation is OK, if I have a field is the creator ID, then this field is never changed, I added when I also call this method, This special encapsulation method is some judgment, if I judge, if I encounter the creator ID passed to null value, I judge if it is null, I set the Creator ID to 0, but the user table UserID is the primary key starting from 1, then the data is not corresponding to the This is the wrong thing to do. This error occurs at the beginning of the development, because each person's module is developed by the corresponding people to complete the integration after the completion of the individual, each write a single piece of time tend to ignore these, so the integration of these problems are often all of a sudden come out .... Integration is hard, tnnd!.

Hibernate queries are a lot more than hibernate queries, query,find,criteria,get,load

Query using the HSQL statement, you can set the parameters is a common way

The way to criteria, try to avoid writing HQL statements, looks more object-oriented.

Find way, this way has been discarded by the new hibernate

The Get and load method is to get a record based on the ID and the difference between get and load is described in detail, because sometimes the find is added in order to compare.

1, from the return result on the comparison: Load mode is not retrieved will throw Org.hibernate.ObjectNotFoundException exception get method can not retrieve the words will return null

2, comparison from the search execution mechanism: The Get method and the Find method are retrieved directly from the database and the execution of the load method is more complex 1, first find out if there is a cache in the persistent context of the session, and if there is a direct return 2, If there is no judge whether it is lazy, if it is not directly access to the database retrieval, found that the record returned, not to throw exception 3, if it is lazy need to establish a proxy object, the object's initialized property is the False,target property is null 4, When accessing the properties of the obtained proxy object, the database is retrieved, and if a record is found, the object of the record is copied to the target of the proxy object, and the exception is thrown if the initialized=true is not found.

(GO) Collection: Hibernate FAQ No row with the given identifier exists the cause and solution of the problem

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.