In the previous example, the List Method of bookhibernatedao uses getsession (). createquery ("hql statement"); returns a query, which is the most important interface for Object-Oriented queries. equivalent to statement in JDBC, query interacts with the database through hql. the simplest hql: from book lists all book objects. where did select go? This statement implies select, which can be written as follows:
Select B from book B, but it cannot be written like this: Select * from book
Hql can contain conditions, such as from book B where B. Author = 'sunxing007 '.
Hql can also contain placeholders, such:
Query = getsession (). createquery ("from book B where B. Author =? ");
Query. setparameter (0, 'sunxing007 '); note that it starts from 0 and is different from preparedstatement;
Hql can also contain named parameters, such:
Query = getsession (). createquery ("from book B where B. Author =: Authur ");
Query. setparameter ("author", 'sunxing007 ');
The above hql is written in the code, and you can also write hql in the configuration file for centralized management. The following method is used to improve book. HBM. xml and improve bookdao:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> </P> <p> <pibernate-mapping package = "model"> <br/> <class name = "book" table = "book" schema = "DBO" catalog = "hibernate"> <br/> <ID name =" ID "column =" ID "> <br/> <generator class =" UUID "/> <br/> </ID> <br/> <property name =" name" /> <br/> <property name = "author"/> <Br/> <property name = "price"/> <br/> </class> <br/> <! -- Configure a query here --> <br/> <query name = "book. byauthor"> <br/> <! -- [CDATA [from book B where B. author =: author] --> <br/> </query> <br/> </pibernate-mapping> <br/> <! -- <Br/> Create Table Book (<br/> ID int identity (1, 1) primary key, <br/> name varchar (20) not null, <br/> author varchar (20), <br/> price float <br/>) <br/> --> </P> <p>
Package Dao; <br/> Import Java. util. list; <br/> Import model. book; </P> <p> Public interface bookdao {<br/> public list <book> List (); <br/> Public book findbyid (string ID ); <br/> Public void save (Book); <br/> Public void Delete (string ID); <br/> public list findbyauthor (string author ); <br/> Public book findbyname (string name); <br/>}</P> <p>
Package Dao. hibernate; <br/> Import Java. util. list; <br/> Import model. book; <br/> Import Dao. bookdao; </P> <p> public class bookhibernatedao extends basehibernatedao implements bookdao {</P> <p> @ suppresswarnings ("unchecked ") <br/> public list <book> List () {<br/> return getsession (). createquery ("from book "). list (); <br/>}</P> <p> Public book findbyid (string ID) {<br/> return (book) getsession (). load (BOO K. class, ID); <br/>}</P> <p> Public void save (Book) {<br/> // transaction Tx = getsession (). begintransaction (); <br/> getsession (). saveorupdate (book); <br/> getsession (). flush (); <br/> // Tx. commit (); <br/>}</P> <p> Public void Delete (string ID) {<br/> book B = (book) getsession (). load (book. class, ID); <br/> // transaction Tx = getsession (). begintransaction (); <br/> getsession (). delete (B); <br //> // Tx. commit (); <br/>}< br/> public list findbyauthor (string author) {<br/> // book. byauthor is configured in book. HBM. <br/> return getsession () in XML (). getnamedquery ("book. byauthor "). setstring ("author", author ). list (); <br/>}</P> <p> Public book findbyname (string name) {<br/> string hql = "from book B where B. name =? "; <Br/> return (book) getsession (). createquery (hql ). setstring (0, name ). list (). get (0); <br/>}< br/> // test case <br/> @ suppresswarnings ("unchecked ") <br/> Public static void main (string [] ARGs) {<br/> bookdao Dao = new bookhibernatedao (); <br/> book B = Dao. findbyname ("WW2"); <br/> system. out. println (B); </P> <p> List <book> List = Dao. findbyauthor ("author2"); <br/> system. out. println (list. size (); <br/> for (Book B1: List) {<br/> system. out. println (B1); <br/>}</P> <p>/*** <br/> book B = New Book (null, "book name ", "book author", 11.5f); <br/> // Dao. save (B); <br/> book b1 = Dao. findbyid ("204"); </P> <p> system. out. println (B1); <br/> **/<br/>}</P> <p >}< br/>