HQL Query
Internal Connection:
From Entity inner JOIN [fetch] Entity.property
Ignoring the fetch keyword, we get the result set, each row of data is an object array
The Fetch table name indicates that the right Table object is immediately populated with the corresponding left Table object when it is read Out.
Left outer connection:
From Entity left join [fetch] Entity.property
Session.createquery ("from District D left join fetch d.streets s");
Hibernate's right outer join syntax is as follows
From Entity right Join [fetch] Entity.property
From District D right join fetch d.streets s
To query a single field:
That is, the data you need is just a property of the entity object, and HQL can do it simply.
String hql = "select s.name from Street s";
List.get (0). GetClass ()
For queries of entity single attributes, hibernate returns objects that are the type of the property
Querying multiple Fields:
The property contents of the ID and name are Taken. In the returned list object, each entry is an array of objects (object[]), and each object array holds the property data we obtained in TURN.
Experience: to make the returned results more consistent with the object-oriented style, you can encapsulate the data by dynamically constructing the object instance in Hql.
The following HQL statements can be used:
"select New Street (id,name) from street"
This will save the street object in the returned list Object. however, You must add a constructor that takes the ID and name attribute as a parameter in the street class
Distinct: Go Heavy
What is a cache
The data on the disk is put into memory, and we read it from the cache, no more on the Disk.
Hibernate cache function
Improve application performance by reducing the frequency of application access to physical data sources
Cache classification
First level cache (internal cache) Session: object
Second level cache: Sessionfactory: object
First-level Cache Description:
The First-level cache, also known as the "session cache", is built-in and cannot be unloaded in the primary cache, using the Key-value map method. When the entity object is cached, the Object's primary key ID is the map key, and the entity object is the value of the Object. So the first level cache is stored in the entity object Unit. Automatically managed by session
, when using the same session to find the same object, only query the database once--using key-value form to store the object, key is oid, value is an object, cannot be unloaded (ehcath)
Second-level Cache function
The Second-level cache, also known as "sessionfactory cache", is a process-wide or cluster-wide cache due to the lifetime of the Sessionfactory object and the entire process of the application, so there is a potential for concurrency Problems. therefore, you need to use the appropriate concurrent access policy to turn off using Third-party plugins by default
Sessionfactory Cache (level Two cache):
List method for hql query: only queries from the level three cache will not be queried from the 1 2 cache
The iterate method uses a 1 2 cache and does not use a Three-level cache to query all IDs before querying for classes
Configuring Level Two Caching
1. First import the Ehcache.jar level two cache Package.
2. then, Add the Ehcache.xml configuration under src,
3. At the same time, enable level two cache in Hibernate.cfg.xml
<property name= "hibernate.cache.use_second_level_cache" > true</property>
<property name= "hibernate.cache.provider_class" > net.sf.ehcache.hibernate.ehcacheprovider</property>.
4.: specify which type of object to use with level two cache cache, add <cache region= "sampleCache1" usage= "read-only" in hbm.xml/>
Steps for Hibernate database Access:
1) Read and parse the configuration file
Configuration conf = new configuration (). Configure ();
(2) Read and parse the mapping file, create Sessionfactory
Sessionfactory SF = conf.buildsessionfactory ();
(3) Open Session
Session session = Sf.opensession ();
(4) start a transaction (additional deletions required, Optional query Operation)
Transaction tx = Session.begintransaction ();
(5) Database Operation.
Session.save (user);//or Other Operations
Session.save (tea);
(6) COMMIT TRANSACTION (rollback Transaction)
Tx.commit ();//tx.rollback ();
(7) Close Session
Session.close ();
Persistence:
We organize programs in an object-oriented way, and instantaneous data also exists in the form of objects, while persistent data is multi-insured in relational Databases. so, in general, the action to be done is to save the object to a relational database, or to read the data in the relational database and encapsulate it in the form of an object.
Module Test:
Add the jar packages required by JUnit
Write @test on top of the test method
Outline view Select the Test method to execute right-click Run as to select JUnit test
Configuration file Example
Entity class:
<class name= "cn.jbit.houserent.bean.User" table= "users" schema= "jbit"
<id name= "id" type= "java.lang.Integer";
<column name= "id"/>
<generator class= "native" />
</id>
<property name= "password" type= "java.lang.String";
<column name= "password"/
</property>
<property name= "telephone" type= "java.lang.String";
<column name= " Telephon "/>
</property>
<property name=" username "type=" java.lang.String ";
<column Name= "username"/>
</property>
</class>
General configuration file; name must be Hibernate.cfg.xml
<session-factory>
<property name= "dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
<property name= "connection.url" >
Jdbc:mysql://localhost:3306/test
</property>
<property name= "connection.username" >root</property>
<property name= "connection.password" >dgt</property>
<property name= "connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "myeclipse.connection.profile" >my</property>
<property name= "show_sql" >true</property>
<mapping resource= "com/bawei/bean/zuserrole.hbm.xml"/>
<mapping resource= "com/bawei/bean/zuser.hbm.xml"/>
</session-factory>
**********************
Load mode of Lizy Configuration object lizy= "true" Instant Load
Cascade whether the Cascade operation All/delete/none/saveandupdate
Whether the inverse is actively managed by one side foreign key true by multiparty management foreign key
Inquire
Get all Objects
A column gets a collection of (strings)
Multi-column Get Object Array Collection get Object
Conditional query
Number of positions starting from 0
Named parameter bindings
Encapsulating parameter Bindings setproperties ()
Java Knowledge summary-12