How do you handle complicated business logic with hibernate?

Source: Internet
Author: User

When hibernate encounters a complex business situation, it cannot define SQL as casually as ibatis and then map it to the XML file (Hibernate is also possible, as shown below:

<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>

)

However, you need to define a BO (or VO) in advance. In hibernate's view, a Bo also needs a mapping XML). In the mapping file, you must specify the current mapping table, however, to write complex SQL statements, you often need to join multiple tables. How can this problem be specified?

As a result, there are four solutions:

1. Write a view and generate a corresponding Bo and mapping XML. This benefit is easy to maintain. During database migration, only the view schema is modified. In this way, two files (one Bo class and one mapping XML) are added for a complex query ).

2. As mentioned above, defining an SQL-query in mapping XML often requires multiple tables, which is troublesome to configure the Mapping File. However, the maintainability is worse than that written in the view. When migrating a database, you need to modify the XML file.

3. the third is a solution that I personally think is the most efficient at present. It is to write native SQL in your Dao. Note that it is not hql, and all programmers know it, writing pure SQL statements can solve queries of many complex businesses. However, hql has many limitations-at least in hibernate3.0, it is not so free. For example, it is difficult to convert such SQL statements into hql:

Select Main. *, C. * from (select a. * From est_step_sub_cost,
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, there is no way. I chose native SQL in hibernate. In fact, this is not pure SQL. In my personal understanding, it is a kind between SQL and hql, as shown below:

Stringbuffer hql = new stringbuffer ("select {main. *}, {C. *} from (select. * From est_step_sub_cost A, est_job_section B where 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;

Comment: The disadvantage is that it may be highly dependent on the database platform, and it will be troublesome for database migration in the future. We have adopted this approach, make sure that your code does not contain SQL statements that are loaded with the platform, such as processing many functions or dates. You don't need to talk about the benefits.

However, when using this method, you must note that the returned list stores an array object, so you need to handle it yourself, for example, when displaying the object on the page.

4. the last type of hql is hql. I personally think that it can barely work together in simple business processing. The complexity is that hql is not flexible enough.

In addition, the execution efficiency and performance are the worst among several.

 

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.