Association query is used in the framework of spring + hibernate3.

Source: Internet
Author: User
Association query is used in the framework of spring + hibernate3.

Table User: address is a one-to-many relationship.
Userbean has a set of addresses

Now you need to query such a user, which owns the user whose doorplate is "642" in the address,
Currently, there is only one such user in the DB. This user has five addresses with the portal plate doorplate "642 ~~

The statement for creating detachedcriteria is as follows:

Detachedcriteria = detachedcriteria. forclass (user.Class);
Detachedcriteria. createcriteria ("addresses"). Add (restrictions. Like ("doorplate", "642 "));

The actual query statement is as follows:


List list = gethibernatetemplate (). findbycriteria (querycriteria );

Theoretically, the list queried should be 1 for the user list.

Summary:
1:
Detachedcriteria addresscriteria = detachedcriteria. forclass (user. Class). createcriteria ("addresses ");
Addresscriteria. Add (restrictions. eq ("doorplate", "642 ")):

2:
Detachedcriteria addresscriteria = detachedcriteria. forclass (user. Class). createalias ("addresses", "");
Addresscriteria. Add (restrictions. eq ("A. doorplate", "642 ")):

 

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.