1. Two configuration files:
A. hibernate. cfg. xml and B. hibernate. Properties
A can contain the configuration of the ing file, while hard codes in B adds the ing file.
A. Configuration Config = new configuration (). config ();
B. Configuration Config = new configuration ();
Config. addclass (Tuser. Class );
2. you do not have to use hibernate. cfg. XML or hibernate. properties, you do not have to put the configuration file under classes, file = new file ("C:/sample/myhibernate. XML "); configuration Config = new configuration (). config (File );
3. session. Flush () forces the database to synchronize immediately. When a transaction is used, it does not need to be flush. The transaction commit will automatically call flush when the session is closed. Session. Refresh () forces the bean in the cache to synchronize with the database. Flush indicates that the database update cache is not updated, and refresh indicates that the cache update database is not updated.
4. hibernate always uses the object type as the Field Type
5. XDoclet specifically sets up hibernate doclet, that is, adding some java doctag to the Java code, and then letting XDoclet analyze the Java code to generate a ing file;
6. The hql clause is case-insensitive, but the class name and attribute name must be case sensitive.
7. Relationship: Constrained: Constraints, indicating whether a foreign key exists on the primary key of the master table.
Property-Ref: name of the property associated with the main control class in the association class. The default value is the primary key property name of the association class.
One-to-multiple deployment must be configured on one side, and one-to-multiple deployment must be configured on both sides.
8. Lazy = false: the passive records are recorded by Hibernate and stored in the collection type attribute specified by the master.
9. Collection of Java. util. set or net. Sof. hibernate. collecton. Bag Type
10. Important: inverse: used to identify the passive end of a bidirectional Association.
The inverse = false Party (main prosecution) is responsible for maintaining the association. Default Value: false
11. batch-size: the number of data records read at a time when the feature is loaded with latency.
12. One-to-multiple update through the master prosecution (when the master is one party)
User. getaddresses (). Add (ADDR );
Session. Save (User); // cascade update through the master object
13. In the one-to-one relationship, setting the active side as the active side (inverse = false) will help improve the performance. When one party sets the link, inverse = true, that is, the master control is handed over to multiple parties, so that multiple parties can take the initiative to obtain the foreign key from one party, and then one insert can be completed.
ADDR. setuser (User); // sets the associated Tuser object
User. getaddresses (). Add (ADDR );
Session. Save (User); // cascade update
14. Only the owner of the primary prosecution cares about the attributes of the other party. The passive party does not care about the attributes of the other party.
15. The configuration attributes of one-to-one and one-to-one nodes are different:
The one-to-multiple relationship has the following attributes: lazy and inverse:
Column: fields associated with the target table in the intermediate ing table
Class: Class Name, associated with the target class
Outer-join: whether to use outer join
Note: access is the read method for setting the attribute value.
Column is the associated field.
16. many to many. Note that both parties must set inverse and lazy. cascade can only be set to insert-update.
In many-to-many relationships, because the association relationship is referenced by two tables, the two tables must be saved simultaneously when the relationship state is saved.
Group1.getroles (). Add (role1); role1.getgroups (). Add (group1 );
Session. Save (role1); Session. Save (group1 );
17. after hibernate capacity processing, vo and Po VO become Po (the reference of this vo will be saved by the container and flush when the session is closed, therefore, if the Po is transferred to another place and changed, it is dangerous. Vo and Po are converted to each other: beanutils. copyproperties (anotheruser, user );
18. For the save operation, if the object has been associated with the session (that is, the object container that has been added to the Session), no specific operation is required. In the subsequent session. Flush process, Hibernate will traverse the objects in the object container, find the starting and changing objects, generate and execute the corresponding update statement.
19. if we adopt the delayed loading mechanism, but want to implement the non-delayed Loading Function in some cases, that is, after the session is closed, you can still operate on the addresses attribute hibernate of the user. the initialize method can implement this function by forcibly loading the correlated object: this is precisely why we must use the JDK collection interface (such as set and map) when writing pojo ), the non-specific JDK collection implementation class (such as hashset and hashmap) declares the reason for the collection attribute.
20. transaction: the session is obtained from sessionfactory, and its automatic submission attribute is closed (autocommit = false). If the JDBC operation is performed, the session is not explicitly called. begintransaction () does not execute transaction operations.
JDBC transaction: A transaction based on the same session (that is, the same connection;
JTA transaction: Cross-session (cross-connection) transaction.
There are three implementation methods for JTA transactions:
A. Usertransaction Tx = new initialcontext (). Lookup ("...");
TX. Commit ();
B. hibernate encapsulation method: (not recommended)
Transaction Tx = session. begintransaction ();
TX. Commit ();
C. Use the transaction technology of sessionbean of EJB. You only need to declare the method that requires JTA transaction in the release descriptor as require.
21. pessimistic lock, optimistic lock: Optimistic lock is generally implemented through version. Note that the version node must appear after the ID.
22. In hibernate, you can use the criteria. setfirstresult and criteria. setfetchsize methods to set the paging range.
The query interface also provides the same method. hibernate mainly implements this function in the dialect class.
23. Cache
......
Net. SF. ehcache. hibernate. Provider
You also need to configure the ecache itself
Then, specify the cache policies for each ing object in the ing file.
....
....
**************************************** * ************ Query. list () and query. iterate (): For query. list () always obtains all records through an SQL statement, reads them, and fills them in pojo for return; but query. iterate () is to first obtain the IDs of all records that meet the query conditions through a select SQL statement, and then perform cyclic operations on this ID set, use a separate select SQL statement to retrieve the records corresponding to each ID, and then enter the records in pojo to return the results.
That is to say, the list operation requires an SQL statement. For iterate operations, n + 1 SQL statement is required ., The list method does not read data from the cache. But iterator does.
24. threadlocal: it maintains a private variable space for each thread. In fact, the implementation principle is to maintain a map in JVM. The map key is the current thread object, and the value is the object instance saved by the thread through the threadlocal. Set Method. When the thread calls the threadlocal. Get method, threadlocal retrieves the corresponding objects in the map based on the reference of the current thread object.
In this way, threadlocal isolates variables of different threads by distinguishing the references of various thread objects.
25. hibernate Official Development Manual standard example:
Public class hibernateutil... {Private Static sessionfactory;
Static... {try... {// create the sessionfactory = new configuration (). Configure (). buildsessionfactory ();
} Catch (hibernateexception ex)... {Throw new runtimeexception ("configuration problem:" + ex. getmessage (), Ex );
} Public static final threadlocal session = new threadlocal ();
Public static session currentsession () throws hibernateexception... {session S = (Session) Session. Get ();
// Open a new session, if this thread has none yet if (S = NULL) {S = sessionfactory. opensession ();
Session. set (s );
} Return S;
} Public static void closesession () throws hibernateexception... {session S = (Session) Session. Get ();
Session. Set (null );
If (s! = NULL) S. Close ();
}}
26. Reuse session through filter:
Public class persistencefilter implements filter... {protected static threadlocal hibernateholder = new threadlocal ();
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception... {hibernateholder. Set (getsession ());
Try ...{...... Chain. dofilter (request, response );
...... } Finally... {session sess = (Session) hibernateholder. Get ();
If (sess! = NULL)... {hibernateholder. Set (null );
Try... {sess. Close ();} catch (hibernateexception ex)... {Throw new servletexception (Ex );
}}}}......}