About the result set returned by hibernate pure SQL query (HBM. XML does not write multi-Table Association)

Source: Internet
Author: User

 

I believe that the brothers who have used hibernate will find that after the multi-Table complex query,Worried about how the returned results are assembled into a vo.. I keep worrying about it, but I am reading it.Hibernate TransformLater, I feel that this method is quite useful.

For example, there are two tables, one is the user table with some user information, the other is the post posted by the user, and there is a foreign key for the user. We need to query the Post published by a person. The SQL statement is as follows:

 

Select U. username, P. Title, P. addtime

From user as U, post as P

Where U. ID = P. userid

 

However, our pojo ing only involves user and post table ing. At this time,We need to write a postvo class with the attributes of user information and post information in it, and set the get \ set method. Make sure there is a default constructor in this class.

Then we start to write the Dao for this database operation. The Code is as follows:

 

String SQL = "select U. UsernameUsernameP. TitleTitle, P. addtimeAddtime"+

"From user as U, post as P" +

"Where U. ID = P. userid ";

 

Query q = factory. getcurrentsession (). createsqlquery (SQL). setresulttransformer (transformers. aliastobean (postvo. Class ));

 

The red part must be consistent with the attribute in postvo, so that a set for postvo can be returned.

In fact, you can see the source code of this part of Hibernate and you will find that the main purpose is to use the aliastobeanresulttransformer class. through SQL queries, an array will be returned, and then hibernate will map the data table, this will automatically help us set the corresponding field attribute, so the red part must be consistent with the attribute value in Vo, or an error will be reported.

You can rewrite this class if needed. For example, voresulttransformer. Then change

 

Setresulttransformer (New voresulttransformer (postvo. Class ));

 

 

Original post address: http://glamey.iteye.com/blog/721019

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.