HQL query and caching mechanism in hibernate

Source: Internet
Author: User

HQL: Fully Object-oriented query

execution order of SQL :

1. from

2.Where to filter the underlying data

The difference between where and having : 1. different order 2.where filtering underlying data 3. filtering aggregate functions

3.Group by

4.Select

5. Having

6.Order by

Use Hibernate Query , use One of Hibernate interface query

HQL is an object-oriented query statement , so it is followed by the class name

Query query = Session.createquery ("Select Id,name,stu.cla.name from Student");

list<object[]> list = Query.list ();

For (object[] obj:list) {

........ printing, etc.

OBJ[0] = ID;  OBJ[1] = name; OBJ[2] = Stu.cla.name;

}

The data after the query exists in an Object array

--------------------------------------------------------------------------------------------------------------- ------------------

List get query

list<list> list = = Session.createquery ("Select New List (id,name,stu.cla.name) from Student"). List ();

for (List list1:list) {

System.out.print (list1.get (0) + list1.get (1));

}

--------------------------------------------------------------------------------------------------------------- ------------------

Java Object query requires a constructor with parameters

Create an Intermediate class , including the required properties, such as the new object : Package name . class name

list<mydata> list = Session.createquery ("Select New com.lovo.my.MyData(id,name,classname) from MyData" ). List ();

for (MyData data:list) {

System.out.print (data.id + "+ data.name +" "+ data.classname);

}

--------------------------------------------------------------------------------------------------------------- ------------------

Map Get Query

list<map> list = = Session.createquery ("Select New Map (name as Stuname,stu.cla.name as Claname) f Rom Student"). List ();

for (Map map:list) {

System.out.print (Map.get ("stuname") + "" + map.get ("classname");

}

--------------------------------------------------------------------------------------------------------------- ------------------

know that the structure of a query is an object that gets a result set

Student stu = (Student) session.createquery ("from Student where id=2"). Uniqueresult ();

( lazy loading ) when the session is cleared , lazy loading errors are reported

System.out.print (Stu.getname () + "" + STU.GETCLA (). GetName ());

Open Lazy Loading : 1. Settings in Configuration lazy="false" 2. in the Session before closing , Initialize Hibernate. Initialize ()

Internal connection : matching is not connected , does not match does not connect , can cause data loss

Dto: Data Persistence

Singleton mode : For example :p rivate hibernateutil () {} cannot be new

Hibernate placeholder : 1.id=: ID ( named parameter ) 2.id=?

The Group by projection has a limit of 1.group by the ability projection 2. Aggregation Functions

Round ( need to take the whole function , keep a few decimals )

Ifnull ( Judging data , 0);--- if the data is empty , use 0 instead

Subqueries can only be used in Where/select in HQL

with local SQL/HQL Statement :

Select name from T_student; Select ID from T_student;

returns a column of data : accepted with an Integer using string

Session.createsqlquery (SELECT * from T_student). Addscalar ("name"). List ();

in the there is a scalar in the SQL statement , * it has no effect

Session.createsqlquery (Select * from T_student). Addenitity (Student.class). List ();

Store all the data you have queried to in the Student object

Hibernate is the encapsulation of JDBC : A reduction in the efficiency of database access and a decline in performance ( caching is generated for this ---- in Memory ) is located in the database and database access layer

First- level cache : Session Cache

Session.clear ();----- Clear session, all data in the cache is cleared

Lazy ( lazy loading ) has lazy loading :

1. What data is required to send what SQL statement

2. Turn off lazy loading : all associated tables are concatenated with one SQL statement , too many objects are stored in memory

3. when the session is closed , lazy loading is also turned off

4. Default lazy loading on

Crawl Strategy :

Immediately after the Session is closed, fetch the data ----- Fetch all the data into the database

From Student Stu the left join fetch stu,myclass where stu.id = 1;

Level Two cache : sessionfactory level with third-party jar Packages

under Hibernate configuration :

<!-- Query cache --

<property name= "Cache.use_query_cache" >true</property>

<!-- Turn on level two cache--

<property name= "Cache.use_second_level_cache" >true</property>

<!-- configuration echche Implementation Class--

<property name= "Cache.region.factory_class" >

Org.hibernate.cache.EhCacheRegionFactory

</property>

Hibernate pessimistic and optimistic lock : Control Data is not destroyed by the outside when it is operated

pessimistic lock : The state of the lock table Setlockmode ( object , Lockmode.upgpade) depends on the database lock table mechanism , T release after submitting

optimistic lock : mainly in version form

Configuration :

1. when creating a class , Add a property , version int

2.Version must be configured at The back of the ID <version name="version" column= "Version" ></ Version>

Gson Gson = new Gson ();

String list = Gson.tojson ();

To make a jar package :

1.File--export

2. Enter the jar---jarfile

3. Select the jar package you want to hit to write the jar Package name Select Save Path

HQL query and caching mechanism in hibernate

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.