Hql:hibernate Query Language (Hibernate-specific querying language)
HQL is an object-based query language, and its syntax is similar to SQL, but he differs from SQL in that SQL is a table-and field-oriented query, and HQL is an object-and attribute-oriented query.
Demand 1. Find out all the items
@Test Public voidTestfindallporject () {//Get Hibernate sessionSession session =hibernateutils.getsession (); //Defining HQL StatementsString hql = "Select project from Com.deng.hibernate.bean.Project project";//"SELECT * From Project"; //Create a Query object queriesQuery query =session.createquery (HQL); //invoke the relevant method of query to perform the related actionlist<project> list =query.list (); SYSTEM.OUT.PRINTLN (list); Session.close (); }
Demand 2. Fuzzy query based on the address of a given item, and in chronological order of creation time (query with parameters in HQL)
@Test Public voidtestfindporjectbycondition () {//Get Hibernate sessionSession session =hibernateutils.getsession (); String Addr= "Dragon"; //Defining HQL StatementsString hql = "from Com.deng.hibernate.bean.Project Project" +//"where project.address like?" + parameters based on question marks"Where project.address like:myaddr" +//based on named arguments, the colon is the syntax for named arguments to begin with"ORDER BY project.createtime Desc";//"SELECT * From Project"; //Create a Query object queriesQuery query =session.createquery (HQL); //To set the parameters, note that the position of the question mark parameter starts at 0Query.setstring ("myaddr", "%" +addr+ "%"); //query.setstring (0, "%" +addr+ "%"); //invoke the relevant method of query to perform the related actionlist<project> list =query.list (); SYSTEM.OUT.PRINTLN (list); Session.close (); }
Requirement 3: Check out all the projects under the company name that contain "New Hope".
Project and company relationships are many-to-one, and in project objects there are
@Basic @ManyToOne = "company_id") Public company Getcompany () { return Company ; }
The practice at this time:
@Test Public void Testfindprojectbycompanyname () { = hibernateutils.getsession (); = "from Com.deng.hibernate.bean.Project p where" + "P.company.companyname like:cname"; = Session.createquery (HQL); Query.setstring ("cname", "% New Hope%"); List<Project> list = query.list (); SYSTEM.OUT.PRINTLN (list); Session.close (); }
Demand 4. Query out all the items, according to the creation time flashbacks, the request is the third page of data (3 rows per page)
@Test public void Testfindprojectbypage () {Session session = hibernateutils.getsession (); String hql = "from Com.deng.hibernate.bean.Project p order by p.createtime desc" ; Query Query = Session.createquery (HQL); // Sets the starting index of the result set, that is, the number of rows from which to start fetching Query.setfirstresult (6 // Sets the maximum number of data to get Query.setmaxresults (3 <Project> list = Query.list (); SYSTEM.OUT.PRINTLN (list); Session.close (); }
Demand 5. Get a total of how many items
@Test Public void Testcountproject () { = hibernateutils.getsession (); = "SELECT count (1) from Com.deng.hibernate.bean.Project P"; = Session.createquery (hql); // Uniqueresult returns a unique result Long Count = (long) query.uniqueresult (); System.out.println ("----------->" +count); Session.close (); }
Demand 6. To count the total rent area and the property right area of each project
//to find out the total amount of the leased area of all resources, the sum of property right area@Test Public voidTestsumresource () {Session session=hibernateutils.getsession (); String hql= "Select Res.project, sum (Res.measurearea) as Allarea,sum (Res.canleasearea) as can from Resource res" + " GROUP BY Res.project "; Query Query=session.createquery (HQL); //when there is more than one row of datalist<object[]> list =query.list (); //only one row of data//object[] obj = (object[]) query.uniqueresult ();//System.out.println (obj[0]);//System.out.println (obj[1]);Session.close (); }
Hibernate: Implement data query based on HQL