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