1. Feel the power of myeclipse. You only need to right-click to add the hibernate function for the project to quickly apply hibernate.
2. Remember to add the corresponding package name if you want to modify the object name when generating an object for the table name reverse engineering.
3. The generated config file is stored in the root directory by default. The path can be specified in sessionfactory.
4. The sessionfactory class is used to create a hibernate session. Session is equivalent to a connection in JDBC. It has the open, close, begintrans, commitrans, and rollbacktrans methods.
5,In the Application Scenario of threadlocal, I think it is best to access objects with multiple threads (each thread corresponds to an instance), and this object is used in many places.
Public static session getsession () throws hibernateexception {
Session session = (Session) threadlocal. Get ();
If (session = NULL |! Session. isopen ()){
If (sessionfactory = NULL ){
Rebuildsessionfactory ();
}
Session = (sessionfactory! = NULL )? Sessionfactory. opensession ()
: NULL;
Threadlocal. Set (session );
}
Return session;
}
6. Public remployee findbyid (Java. Lang. String ID) is searched through the primary key
7. Three major functions of hibernate: A, or-mapping, B, shielding the database, C, and Cache
1. ORMapping will affect the deletion efficiency.
It should be noted that during batch operations, the session should be flush in a timely manner to avoid outofmenoryerror. delete: I mainly talk about batch Delete. hibernate must first load all qualified data to the memory before batch Delete (this is a problem that all ORM frameworks must face, this is mainly to sort the data in the current internal cache and the second-level cache to save the data consistency with the database.) in this way, if the operation data volume is too large, an outofmenoryerror occurs, therefore, iterative deletion is recommended.
Hibernate cache mechanism:
Level 1 cache: Session-level cache
The Hibernate session provides a level-1 cache. Each session loads the same ID twice and does not send two SQL statements to the database. However, when the session is closed, the level-1 cache becomes invalid.
Level 2 Cache: Global cache at sessionfactory level,
The cache can be simply regarded as a map, and the value is searched in the cache through the key.