I. Overall hibernate knowledge architecture
This article mainly organizes knowledge points based on this figure.
2. Knowledge preparation
1. Use Cases of hibernate
It can only be said that hibernate is not applicable to the following scenarios:
1. It is not suitable for OLAP (On-Line Analytical Processing Online Analytical processing), mainly for querying and analyzing data, and for OLTP (On-line transaction Processing online transaction processing ).
2. For old systems with unreasonable relationship model design, the advantages of hibernate cannot be used.
3. Systems with massive data volumes and demanding performance requirements are also difficult for hibernate to meet the requirements, and the efficiency of batch data operations is not high.
In short, hibernate has low efficiency in data query.
Some convenience provided to programmers is provided by consuming performance.
2. Common hibernate APIs (five core interfaces)
Method
Configuration object, corresponding to the Configuration file
Configure (String)
Configure () load the default master configuration file (hibernate. cfg. xml)
AddResource (String)
AddClass (Class)
BuildSessionFactory ()
Method chain
SessionFactory Session Factory
OpenSession () opens a new Session
GetCurrentSession ()
Close ()
Session
BeginTransaction ()
GetTransaction ()
Close () close
----------
Save (Object)
Update (Object)
Delete (Object) deletes an Object.
Get (Class, id): Object
CreateQuery (String hql): Query
...
Query
List (): List
UniqueResult (): Object
SetFirstResult (int)
SetMaxResults (int)
Criteria (not the core class)
A query method
Criteria. add (Restrictions. eq ("name", name ))
Transaction
Begin ()
Commit ()
Rollbacl ()
3. How to Build hibernate;
1) import the jar package
2) create a hibernate. cfg. xml file under src
3) create an object class
4) Create XXX. hbm. xml corresponding to the object class;
5) Register XXX. hbm. xml to hibernate. cfg. xml.
6) Create HibernateUtil
The Code is as follows:
Private static SessionFactory sessionFactory = null; // fast static initialization (created during class loading) static {try {// Configuration indicates that the hibernate initiator sessionFactory = new Configuration (). configure (). buildSessionFactory ();} catch (Throwable e) {System. out. println ("hibernate initialization failed! "); E. printStackTrace (); // throw new ExceptionInInitializerError (e);}/*** get SessionFactory * @ return */public static SessionFactory getSeesionFactoty () {return sessionFactory ;} /*** get the Session * @ return */public static Session getSession () {return sessionFactory. openSession (); // one function is to possess transactions}
4. Three states of the Persistent Object
They are: Transient, Persistent, and offline ). The lifecycle of objects in three States is as follows:
The three States are different:
Objects in the Instantaneous State: not managed by the session, not in the database;
Offline status: it is not managed by the session, but it exists in the database.
Objects in the persistent state are managed by sessions. Objects in the database are automatically synchronized with the database when the attributes change and the cache is cleared. (Note that changes are synchronized)
Hibernate accesses persistent objects (for example ):