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 ")):