Use of the Hibernatetemplate method

Source: Internet
Author: User

1, Query post (POST) as an example

Find All Posts

     Public List<post> findposts () {        = "from Post p left join fetch p.user";        Listnull;          This . Ht.find (HQL);         return list;    }
  @Transactional (propagation  = public  list<post> FindPosts2 () {List  <Post> list= null  ; Detachedcriteria DC  = Detachedcriteria.forclass (Post.        Class  ); List  = (list<post>) this  .        Ht.findbycriteria (DC);     return   list; }    
    @Transactional (propagation=propagation.required)    public list<post> findPosts3 () {        List NULL ;         this. Ht.findbyexample (new  Post ());         return list;    }

Specific criteria Query

 @Transactional (Propagation= propagation.required)  public  list<post> Findpostbyuser (string userid) {string hql  ="        From Post p where P.user.userid =: userid ";        List  <Post> list= null  ;  try  {list  = (list<post>) this . Ht.findbynamedparam (HQL, "userid" ,userid);// list = (list< post>) This.ht.findByNamedParam (hql, New string[]{"userid"},new String[]{userid}) 
}catchreturn  list;}

Paging Query

 @Transactional (Propagation=propagation.required)  public  list<post> findposts (int  start, int   limit)        {String hql  = "from Post p left join fetch p.user"  <Post> list= null  ; Detachedcriteria DC  = Detachedcriteria.forclass (Post.        Class  ); List  = (list<post>) this  .        Ht.findbycriteria (DC, start, limit),//this.ht.findbyexample (New Post (), start, limit);     return   list; }

Native SQL query (returns an object array)

     @Transactional (propagation=propagation.required)    public list<post> findPosts4 () {         = "SELECT * from Post";        Listnull;          This . Ht.getsessionfactory (). Getcurrentsession (). createsqlquery (SQL). List ();         return list;    }        

Use of the Hibernatetemplate method

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.