Hibernate query optimization-delayed loading Optimization

Source: Internet
Author: User

I believe that everyone is using hibernate (I am using 3.6 here, because I prefer spring, but the hibernate4.x series are a lot of duplicates with spring later, and spring also thinks that, hibernate4.x is a very well-developed framework. spring3.x has discarded Many hibernate support classes since then, so I will not upgrade hibernate, during the configuration of the hibernate ing file, if there is one-to-many (or many-to-many) attributes, hintify provides an optimization mechanism, the lazy attribute, but the problem is, the configuration file is a one-time configuration. If some parts of my project require delayed loading, some parts do not require delayed loading. In this way, you must force lazy = false, in this way, the optimization mechanism will lose its meaning.

After a period of experience, I finally got along with a set of feasible solutions:

All the lazy properties in the project are set to true by default and are not set to false. A commonService is added to the project, which provides two methods: query a single object and query list:


[Java]
@ Override
Public Object findById (Class <?> Objclass, Class <?> IdClass, Object id, String [] fields ){
Object o = dao. findById (Objclass, (Serializable) idClass. cast (id ));
If (fields! = Null ){
For (String field: fields ){

String targetMethod = "get" + upperFirstWord (field );

Method [] methods = Objclass. getDeclaredMethods ();
For (Method m: methods ){
If (m. getName (). equals (targetMethod )){
Try {
Hibernate. initialize (m. invoke (o ));
Break;
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
}
Return o;
}

@ Override
Public Object findById (Class <?> Objclass, Class <?> IdClass, Object id, String [] fields ){
Object o = dao. findById (Objclass, (Serializable) idClass. cast (id ));
If (fields! = Null ){
For (String field: fields ){

String targetMethod = "get" + upperFirstWord (field );

Method [] methods = Objclass. getDeclaredMethods ();
For (Method m: methods ){
If (m. getName (). equals (targetMethod )){
Try {
Hibernate. initialize (m. invoke (o ));
Break;
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
}
Return o;
}
Class of the object to be queried
IdClass id field type
Id value
Fields to be initialized by fields

 


[Java]
Public List <?> Find (Class <?> EntityClazz, QueryParameterSetter paraSetter, Sorter sorter, Page, String [] fields ){
DetachedCriteria dc = DetachedCriteria. forClass (entityClazz );
If (paraSetter! = Null)
ParaSetter. setParameters (dc );
If (sorter! = Null & sorter. getOrder ()! = Null)
Dc. addOrder (sorter. getOrder ());
List <?> L = findPageListByCriteria (dc, page. getPageSize (), page. getRecordStartIndex ());
LazyInitialize (entityClazz, l, fields );
Return l;
}

Private void lazyInitialize (Class <?> EntityClazz, List <?> L, String [] fields ){
If (fields! = Null ){
For (String field: fields ){

String targetMethod = "get" + upperFirstWord (field );

Method [] methods = entityClazz. getDeclaredMethods ();
For (Method m: methods ){
If (m. getName (). equals (targetMethod )){
Try {
For (Object o: l ){
Hibernate. initialize (m. invoke (o ));
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
}
}

Public List <?> Find (Class <?> EntityClazz, QueryParameterSetter paraSetter, Sorter sorter, Page, String [] fields ){
DetachedCriteria dc = DetachedCriteria. forClass (entityClazz );
If (paraSetter! = Null)
ParaSetter. setParameters (dc );
If (sorter! = Null & sorter. getOrder ()! = Null)
Dc. addOrder (sorter. getOrder ());
List <?> L = findPageListByCriteria (dc, page. getPageSize (), page. getRecordStartIndex ());
LazyInitialize (entityClazz, l, fields );
Return l;
}
 
Private void lazyInitialize (Class <?> EntityClazz, List <?> L, String [] fields ){
If (fields! = Null ){
For (String field: fields ){

String targetMethod = "get" + upperFirstWord (field );

Method [] methods = entityClazz. getDeclaredMethods ();
For (Method m: methods ){
If (m. getName (). equals (targetMethod )){
Try {
For (Object o: l ){
Hibernate. initialize (m. invoke (o ));
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
}
} EntityClazz entity type
ParaSetter parameter setting Interface
Sorter sorting rules
Page Object
Fields to be initialized by fields

In this case, you only need to use these two methods to load associated object data in the project, and you can specify attributes without loading them all, the performance has been greatly improved.

This is just a piece of code. I believe most programmers can understand it. As for those programmers who cannot understand it, let's figure it out.

 


 

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.