Common APIs in Hibernate and common APIs in Hibernate

Source: Internet
Author: User

Common APIs in Hibernate and common APIs in Hibernate
1. Configure 1.1 load the core Configuration file,

When loading hibernate. properties: Configuration configuration = new Configuration ();

When hibernate. cfg. xml is loaded: Configuration configuration = new Configuration (). configure ();

1.2 load the ing file (however, generally, the ing files are configured in the core configuration file, and this method is rarely used)

Configuration. addResource ("com/itheima/hibernate/domain/Customer. hbm. xml ");

Configuration. addClass (Customer. class); // The object class must be in the same package as the ing file.

2. SessionFactory

SessionFactory manages sessions, connection pools, and Hibernate second-level caches. Is a heavyweight, thread-safe object.

3. session

Session is a bridge between the Hibernate program and the database. Complete the CRUD operation. Session is a single-threaded object that maintains the Hibernate primary cache internally.

Hibernate Level 1 cache ---> session level cache

The first-level cache is the session-level cache, and the declaration cycle of the same session is always. The first-level cache is actually composed of a set of sessions.

Primary Role of level-1 cache: reduces the number of accesses to the database

Execution principle: When the query operation is performed in the session, the result is first obtained from the cache. If the result is not in the cache, the result is queried in the database, and store the query results in the cache; if there is a cache, it is obtained directly from the cache.

Level 1 cache snapshot zone: data stored in the cache will be stored in the session Level 1 cache snapshot zone. When the data changes, the data in the cache will be modified, the data in the snapshot area is still the original data. when the transaction is committed, the first-level cache and snapshot area will be compared. If the data is inconsistent, an SQL statement will be sent to change the data in the database; if the data is consistent, the database is not updated. therefore, even if no update statement is displayed for objects in the persistent state, the database is automatically updated when the transaction is committed if the data changes.

 

Session saves an object: session. save (entity );

Session modification object: session. update (entity );

Session delete object session. delete (entity );

Session query objects: session. get () and session. load ();

Differences between get () and load () Methods

1. the loading mechanism is different. the get method uses immediate loading. When the code is executed, an SQL statement is immediately sent for query. The load method uses the Lazy mechanism, the SQL statement is not sent immediately when the code is executed. The SQL statement query is sent only when the object is actually used.

2. the query results are different. get method queries get the object generated, while load method queries return a proxy object;

3. When the query fails to be found, the response is different. The get method returns null, while the load method throws an exception ObjectNotFoundException;

4. Query: HQL Query is supported.

You can obtain the Query interface through session. createQuery (String hql.

HQL: Hibernate Query Language. Hibernate query language. The syntax is similar to that of SQL. Objects are queried in HQL.

Public VoidDemo2 (){

// HibernateUtils creates the sessionFactory and obtains the session tool class for compiling.

Session session = HibernateUtils.GetCurrentSession();

Transaction tx = session. beginTransaction ();

// Basic HQL Query

Query query = session. createQuery ("from Customer ");

List <Customer> list = query. list ();

For (Customer customer: list ){

System. out. println (customer );

}

Tx. commit ();

}

5. Criteria: QBC query supported

The Criteria interface can be obtained through session. createCriteria.

QBC: Query By Criteria. Conditional query. A more object-oriented approach.

Statistics Query

Criteria criteria = session. createCriteria (Customer.Class);

Criteria. setProjection (Projections.RowCount());

Long count = (Long) criteria. uniqueResult ();

5.1 offline Condition query: DetachedCriteria.

DetachedCriteria detachedCriteria = DetachedCriteria.ForClass(Customer.Class);

DetachedCriteria. add (Restrictions.Like("Cust_name", "% "));

Session session = HibernateUtils.GetCurrentSession();

Transaction tx = session. beginTransaction ();

Criteria criteria = detachedCriteria. getExecutableCriteria (session );

List <Customer> list = criteria. list ();

For(Customer customer: list ){

System.Out. Println (customer );

}

Tx. commit ();

}

 

6. SQLQuery: supports SQL queries.

You can obtain the SQLQuery interface through session. createSQLQuery.

SQLQuery: queries using SQL statements.

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.