How do you deal with Hibernate when you encounter complex business logic?

Source: Internet
Author: User
Tags one table stringbuffer

When hibernate encounters a complex business situation, it is not possible to define SQL mapping XML files as Ibatis, as in the case of Hibernate, as follows:

<sql-query name= "Getcomponentquery" >
<return alias= "com" class= "Com.vizia.app.bo.ViewGetcomponentaclBymoduleid"/>
<! [cdata[

Select Main.id as {com.id},
Main.code as {Com.code},
Main.description as {com.description},
Main.component_type as {Com.componenttype},
main.module_id as {Com.moduleid},
B.mask as {com.mask}
From (SELECT a.id,
A.code,
A.description, A.component_type, a.module_id
From Tb_component a where a.module_id =:moduleid) main
Left JOIN Tb_component_acl b on main.id = b.component_id
]]>
</sql-query>

)

But you have to define a bo (or VO, in Hibernate's view, a Bo also needs a mapping XML), in the mapping file also have to specify the current mapping table, but write complex SQL, often need to join more than one table, How to specify it.

This leads to a total of four solutions:

1. is to write a view, and then generate a corresponding Bo and mapping XML. This benefit, easy to maintain, as a database migration, when modified only view schema. But this would have to be two more files for a complex query (a Bo class is a mapping XML).

2. As mentioned above, in mapping XML to define a sql-query, so the disadvantage is often to have more than one table, configuration mapping file is more cumbersome. But maintenance is worse than writing in view. As a database migration, you need to change the XML file.

3. The third is my current personal thought is one of the most efficient solution, is in your DAO write native sql--notice is not hql, as programmers know, write more pure SQL can solve many complex business queries. But HQL has a lot of restrictions--at least in hibernate3.0 or not so free. For example: Such a not complex SQL to convert to HQL is more difficult:

Select main.*,c.* from (select A.* from Est_step_sub_cost A,
Est_job_section b where b.job_section_id =a.section_id and b.header_id=. ) Main
Left join Est_step_cost C on c.step_cost_id =main.step_cost_id;

Finally no way, I chose the hibernate in the native sql--in fact this is not pure SQL, personal understanding is a kind of between SQL and HQL, as follows:

StringBuffer hql =new stringbuffer ("Select {main.*},{c.*} from" (Select A.* from Est_step_sub_cost A, est_job_section b WH ere b.header_id = "). Append (Headerid);
Hql.append ("and b.job_section_id =a.section_id) main left outer join Est_step_cost C on c.step_cost_id =main.step_cost_id ");
Query query = getsession (). Createsqlquery (Hql.tostring ()). Addentity ("main", Eststepsubcost.class). addentity ("C", Eststepcost.class);
List List =query.list ();
return list;

Evaluation: Such a disadvantage is likely to be more dependent on the database platform, in the future when the database migration is more cumbersome, we take the practice of ensuring that their own code does not appear with the platform Swatch SQL, such as many function or date aspects of the processing;

However, when using this method, it should be noted that the return of the list object is an array, so you need to handle it yourself, for example, when you need to display the page.

4. The last kind of writing hql, this hql I personally think in simple business processing, but also can barely make it. Complex or, HQL is still not flexible enough.

And the efficiency of execution is performance the worst of the few.

Welcome to the discussion ....

 

Related Article

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.