Hibernate (ii) HIBERNATE-API detailed

Source: Internet
Author: User

First, hibernate architecture

  

Second, Hibernate-api detailed    2.1. Configuration Object

Configuration is used to load the config file.

Our hibernate mainly has two configuration files, the first one is the core configuration file, the second is the mapping file.

    

   2.2. Sessionfactory Factory

Sessionfactory is equivalent to the Java Web Connection pool for managing all session
Access method: Config.buildsessionfactory ();
Sessionfactory Hibernate cache configuration information (database configuration information, mapping files, pre-defined HQL statements, etc.)
Sessionfactory thread security, which can be a member variable, does not occur when multiple threads concurrently access the thread.

    

The difference between sessionfactory and connection pool

      

Getcurrentsession () method

    

  2.3. Session Object

Used to manipulate the database. Sesion is equivalent to connection in JDBC. We through the session Operation Po object to achieve additions and deletions to search.

     2.3.1, Increase

      

   2.3.2, change  

      

  2.3.3, delete

      

     2.3.4, check

      A. Querying using the Get method in session

      

Results:

    B. Using the Load method in the session

The Load method. Returns a proxy object that, when it obtains its contents, queries the database, and every access property queries the database?

Answer: Not every time. There is a Boolean variable in the proxy object that identifies whether it is initialized. Whether the record has been initialized.

Agents are interfaces-based, and the proxy returned with the Load method does not implement any interfaces?

A: Dynamic proxies in Java are based on interfaces. Hibernate, however, uses Javassist-3.12.0.ga.jar to generate proxy objects.

The relationship between the agent and the Proxied object is an inheritance relationship. The dynamic agent that we learn is not one. Therefore, interfaces are not required.

    

A bug has occurred:

Analysis: How get and load are executed

      

   C. Querying all records of a table in a database using the HQL language

    

   D. Using criteria to query all databases

    

   E. Querying using native SQL statements

    

     2.3.5, Summary

In the additions and deletions of our study methods have

Session.save ();
Session.update ();
Session.delete ();
Session.get ();
Session.load (); Compare get
Session.createquery ();
Session.createcritieria ();
Session.createsqlquery ();

  2.4. Transaction Object

Transaction encapsulates the operation of a transaction

In the operations of general transactions are:

Open transaction BeginTransaction ()

Get Transaction gettransaction ()

Commit Transaction commit ()

Rolling back transaction rollback ()

      

Detail handling

      

From here we use the Getcurrentsession () method to get a transaction, and when you do not close the transaction,

When you get a transaction again, they belong to the same transaction.

   2.5. Query Object

The query object encapsulates the HQL statement object and encapsulates the details

    

2.6. Criteria Object (Understanding)

    

Using the like lookup

      

Some basic query criteria query

    

2.7. Tool class

We know that we have to get to the session when we use Hibernate for redaction, which makes the code very reusable, so we can

Create a tool class to solve this problem.

Import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import org.hibernate.classic.Session;//Complete the Hibernate tool class//encapsulates a profile read operation//Encapsulating Sessionfactroy Creation Operations//Package session Get action Public classHibernateutils {Private Staticsessionfactory SF; Static{        //1 Load ConfigurationConfiguration conf =NewConfiguration (). Configure (); //2 Creating sessionfactory based on configuration informationSF =conf.buildsessionfactory (); //Runtime.getruntime (). Addshutdownhook (NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("virtual institutions shut down! Release Resources");            Sf.close ();            }        })); }         Public Staticorg.hibernate.Session opensession () {//3 Getting sessionSession session =sf.opensession (); returnsession; }         Public Staticorg.hibernate.Session getcurrentsession () {//3 Getting sessionSession session =sf.getcurrentsession (); returnsession; }    }

    

    

  

     

        

  

        

Hibernate (ii) HIBERNATE-API detailed

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.