Three major Java frameworks-Hibernate and java three major hibernate
What is Hibernate?
Hibernate is a persistent layer framework based on ORM (O: object, R: relationship, M: ing) ing. It is a lightweight framework that encapsulates JDBC and mainly implements DatabaseCUPDOperation.
Note:CRUDIt refers to the abbreviation of the first letter of the words "Create", "Retrieve", "Update", and "Delete" during computing. It is mainly used to describe the basic operation functions of the database or persistent layer in the software system.
Core interfaces and classes in Hibernate
Hibernate has six core classes and interfaces: Session, SessionFactory, Transaction, Query, Criteria, and Configuration. These six core classes and interfaces are used in any development. Through these interfaces, you can not only access persistent objects, but also control transactions.
Session Interface: Responsible for Object Persistence operations, but note that it is not thread-safe.
SessionFactory Interface: Initializes Hibernate, stores proxy objects, and creates Session objects,
Transaction interface:Responsible for data transaction operations.
Query interface:Queries databases and persistent objects. It can be expressed in HQL or SQL statements of local databases. Query is often used to bind Query parameters, limit the number of Query records, and finally perform Query operations.
Criteria interface:The Criteria interface is similar to the Query interface, allowing you to create and execute object-oriented standardized queries. It is worth noting that the Criteria interface is also lightweight and cannot be used outside the Session.
Configuration class:The Configuration class configures and starts Hibernate. During Hibernate startup, the Configuration instance first locates the ing document, reads the configurations, and creates a SessionFactory object. Although the Configuration class only plays a small role in the entire Hibernate project, it is the first object encountered when hibernate is started.
Hibernate workflow:
Workflow:
Seven steps for Hibernate Persistence:
1. Create a Configuration object and load the cfg. xml Configuration file. (the Configuration file can be automatically generated using Eclipse)
2. Build the SessionFactory factory through the Configuration object;
3. Open a Session through the SessionFactory factory;
4. Start the transaction. (The query method can be omitted)
5. Use Session sessions to perform CRUD operations on databases and persistent objects;
6. Submit the transaction. (The query method can be omitted)
7. Close the session;
The Code is as follows:
Import java. util. list; import org. hibernate. criteria; import org. hibernate. query; import org. hibernate. SQLQuery; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. criterion. restrictions; public class HibernateTest {public void Test () {// create a partition object and load cfg. xml Configuration file Configuration conf = new Configuration (). configure ("hibernate. cfg. xml "); // construct SessionFactory factory SessionFactory sessionFactory = conf. buildSessionFactory (); // open the Session session Session = sessionFactory. openSession (); // to enable a Transaction (add, delete, modify, or delete), you must enable transaction query. You do not need to enable Transaction transaction = session. beginTransaction (); // create Users Class Object Users users = new Users (); /*** several common methods encapsulated in the Session * // call the method Session added in the session. save (users); // call the deleted method Session in the session. delete (users); // call the modified method Session in the session. update (users); // call the Session to query the method session of a class based on the primary key. get (Users. class, users. getId (); // Query String HQL = "from Users" based on hql statements; query Query = session. createQuery (hql); List <Users> userList = query. list (); // query String SQL = "select * from Users" based on SQL statements; SQLQuery sqlQuery = session. createSQLQuery (SQL); List <Users> sqlUsersList = sqlQuery. list (); // according to your Bean. class to specify which class to query Criteria criteria = session. createCriteria (Users. class); // use the Criteria object to add conditions (use the Restrictions object in brackets to select which condition you want to add) criteria. add (Restrictions. ilike ("name", "Washington % ")); /*** after the transaction is committed, the database will be added, deleted, modified, and queried. * You can determine whether to submit the transaction by using conditions. * For example: * If the program runs normally and commits a transaction * otherwise, the transaction will be rolled back * The database will not be operated * the transaction will be rolled back: transaction. rollback (); * // submit the transaction. commit (); // close the Session session to release the resource Session. close ();}}