1. Main classes and interfaces: Hibernate. Hibernate loads the Configuration file information through the Configuration instance, then reads the content of the specified object link ing file and creates the SessionFactory instance. The SessionFactory interface initializes Hibernate. A SessionFactory instance corresponds to a database. The application obtains the Session instance from SessionFactory. Session interface Session is called persistence manager and is responsible for managing persistence-related operations: storage, update, deletion, and object loading. The Transaction interface is the Transaction interface of the Hibernate framework. It encapsulates the underlying transaction interface. Including jdbc api and JTA. 2. session cache (level-1 cache) the CRUD method of the Session and the list () and iterate () method that call the query interface. If no corresponding object exists in the session cache, hibernate will add this object to the first-level cache. If this object already exists in the session cache, it will directly use the cached object instead of loading it to the database. Flush: clears the cache (data in the cache is not lost at this time), so that the cache and the database can execute some column SQL statements synchronously, but do not commit transactions; commit: First Call flush () method, and then commit the transaction. this means that the commit transaction means that the database operations are permanently saved. The session cache is generally automatically managed by the hibernate framework. 3. session methods: 1) save to save data, equivalent to insert method 2) delete, delete object 3) update, update object. If there is no record in the database, an exception will occur. 4) get. Check the database by ID and access the database immediately. 5) Load, query by ID (the returned result is a proxy and the database will not be accessed immediately ). 6), saveOrUpdate (based on the value of ID and version to determine whether it is save or update 4. attribute Value of the primary key id: Value identifier; Note: increment is a mysql database, identity is an oracle database, and Sequence is an SQL server. The most common attribute is native, this attribute allows you to determine which database is used and can only call the corresponding database identifier 5. an example of adding, deleting, modifying, and querying using hibernate [java] view plaincopy is a tool-type Hibernate written manually. java package com. hbsi. utils; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; public class HibernateUt Il {private static Session session; static {Configuration cfg = new Configuration (). configure (); SessionFactory factory = cfg. buildSessionFactory (); session = factory. openSession ();} public static Session getSession () {return session;} public static void close () {if (session! = Null) {session. close () ;}}add, delete, modify, and query UtilTest. java: package com. hbsi. test; import java. util. date; import java. util. list; import org. hibernate. query; import org. hibernate. session; import org. junit. test; import com. hbsi. domain. user; import com. hbsi. utils. hibernateUtil; public class UtilTest {@ Test public void addUser () {User user = new User (); user. setName ("lisi"); user. setBirthday (new Date (); try {Session session = HibernateUtil. getSession (); session. beginTransaction (); session. save (user); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close () ;}@ Test public void deleteUser () {try {// Method 1: first search and then operate/* Session session = HibernateUtil. getSession (); session. beginTransaction (); User user User = (User) session. get (User. class, 1); session. delete (user); session. getTransaction (). commit (); * // Method 2: directly new the bean class and specify the id Session session = HibernateUtil to be deleted. getSession (); session. beginTransaction (); User user = new User (); user. setId (2); session. delete (user); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close () ;}@ Test public void updateUser () {try {// Method 1: first search and then operate/* Session session = HibernateUtil. getSession (); session. beginTransaction (); User user User = (User) session. get (User. class, 2); user. setName ("lisi"); session. update (user); session. getTransaction (). commit (); * // Method 2: directly new the bean class and specify the id to be updated. This method is suitable for deleting and not suitable for updating. For example, you only update the name field, other fields will become empty Session session = HibernateUtil. getSession (); session. beginTransaction (); User user = new User (); user. setId (2); user. setName ("Li Suo"); session. update (user); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close () ;}// search by id; The get () method immediately accesses the database; the load () method returns a proxy and does not immediately access the database @ Test public void findUser () {try {Session session = HibernateUtil. getSession (); User user = (User) session. get (User. class, 1); System. out. println (user. getId () + "----" + user. getName () + "----" + user. getBirthday ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close () ;}// search by id, and load by id; the lazy reason is that the load () method here is different from the get () method above, the former executes SQL statements only when the objects returned by the load () method are used, but not when the latter is used. The latter is different. It executes @ Test public void loadUser () at any time () {try {Session session = HibernateUtil. getSession (); User user = (User) session. load (User. class, 1); System. out. println (user. getId () + "----" + user. getName () + "----" + user. getBirthday ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close () ;}// find all @ Test public void findAll () {try {Session session = HibernateUtil. getSession (); Query query = session. createQuery ("from User"); List <User> list = query. list (); for (User user: list) {System. out. println (user. getId () + "----" + user. getName () + "----" + user. getBirthday ();} catch (Exception e) {e. printStackTrace ();} finally {HibernateUtil. close ();}}}