Query multiple tables in Detachedcriteria database query in Hibernate database

Source: Internet
Author: User

1:hibernate queries with a deeper query, using detachedcriteria multiple table queries.


There are two table Clazz,user in the database, because it is a test program, the foreign key is not set, and the associated attribute between two objects is not included in the mapping file, such as <many-to-one>

It's just that when you write an hql, you implement an association between two objects, similar to the

Sql:select c.id,c.name,c.age,u.name from Clazz p,user u where C.rid = u.id where c.age = and u.name= ' Kevin '


Workaround:


1. Create Detachedcriteria
The call CreateAlias method CreateAlias is mainly used for querying, establishing a relationship between two tables, and querying data related to two tables.

Detachedcriteria Personcriteria = Detachedcriteria.forclass (Clazz.class);

Gethibernatetemplate (). Findbycriteria (Personcriteria.add (Restrictions.eq ("Age", new Integer). CreateAlias (" User "," U "). Add (Restrictions.eq (" U.name "," Kevin "));


2. Establish an association relationship

Use Detachedcriteria multiple table queries must decide to include in the person object's mapping file:

<many-to-one name= "Room" cascade= "All" class= "Hamob". Room "column=" rid "/>

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.