1. configuration class
① Manages the configuration information of hibernate.
② Read hibernate. cfg. xml
③ Load the driver, URL, user name, password, and connection pool configured in the hibernate. cfg. xml configuration file.
④ Manage *. HBM. XML Object Relational file <Mapping Resource = "">
Sample Code: configuration cf = new configuration (). Configure ();
2. hibernate. cfg. xml file
① This file is mainly used to specify parameters and is the core file of hibernate.
② It is stored in the src directory by default, or in another directory.
③ Specify the driver, user name, password, URL, and connection pool used to connect to the database ..
④ Specify the location of the object link ing file.
⑤ You can also use the hibernate. properties file to replace this file. (hibernate. cfg. XML is recommended)
3. Object link ing file (*. HBM. XML)
① This file is mainly used to establish the ing between tables and classes and is an indispensable and important file.
② It is generally placed in the same directory of the ing class, but it is not necessary.
③ The naming method is generally the class name. HBM. XML, but not required.
4. sessionfactory Interface
① Cache SQL statements and some data
② Is a heavyweight class, so we need to ensure that a database has a sessionfactroy. If an application accesses multiple databases, we need to create multiple session factory instances, generally, a database is a session factory instance.
③ Session instances can be obtained through the sessionfactory interface.
Use sessionfactory to obtain one getcurrentsession () in opensession;
Sample Code: configuration cf = new configuration (). Configure ();
Sessionfactory Sf = Cf. buildsessionfactory ();
Session S = SF. getcurrentsession ();
// Or: Session S = SF. opensession ();
5. Session Interface
① Session an instance represents an operation with the database (of course, one operation can be a combination of crud)
② The session instance is obtained through sessionfactory and needs to be closed after use.
③ The session is not synchronized by the thread (unsafe). Therefore, you can use getcurrentsessiong () to ensure that the session is used in the same thread ().
④ Session can be viewed as a persistence manager, which is an interface related to persistence operations.
Sample Code: configuration cf = new configuration (). Configure ();
Sessionfactory Sf = Cf. buildsessionfactory ();
Session S = SF. getcurrentsession ();
// Or: Session S = SF. opensession ();
Several important methods of the session Interface
Sessions are generally operated in the form of objects,
① Save an object (record)-save Method
② Delete an object (record)-delete Method
③ Query an object (record)-Get/Load Method
④ Modify an object (record)-update Method
6. Transaction (transaction) Interface
Here we will briefly explain what a transaction is. We use an online transfer case to explain.
Transactions are simply a set of database operations. They are either all successful or all failed. This ensures data consistency and atomicity.
① Transaction is the abstract interface in the underlying implementation of things
② It may be a JDBC or JTA transaction, which facilitates hibernate to execute different
Environment migration.
③ Call transactions required by hibernate (if only query is required, no call is allowed .)
Transaction Ts = S. begintransaction ();
...
TS. Commit (); S. Close (); TS. rollback () is required to roll back when an exception occurs.
7. query interface
An object of the query interface type can operate on the database. It can operate on the database using hql, QBC, QBE, and Native SQL. hql statements are officially recommended.
8. Criteria Interface