Original: http://www.donews.net/dyhcn/ 1. Two profiles: a.hibernate.cfg.xml and B.Hibernate.properties
A can contain the configuration of mapped files, and the Hard Codes plus the file in B.
A. CONFIGURATION Config = New Configuration (). Config (); B. Configuration config = new configuration (); config.addclass (tuser.class);
2. You don't have to use Hibernate.cfg.xml or Hibernate.properties two file names, you don't have to put the configuration file under the classes, file file = new file ("c: //sample/myhibernate.xml "); Configuration config = new configuration (). Config (file);
3. Session.flush () Forced database immediately, when using transactions, do not have to use flush, transaction submission automatically call flush when session is closed, will also call flush.
4. Hibernate always uses object types as field types
5. XDoclet specializes in Hibernate Doclet, adds some Java DOCTAG on the Java code, then let XDoclet analyzes the Java code to generate a mapping file;
6. HQL clause own case is not related, but the class name and attribute name appearing must be aware of case.
7. Relationship: constrained: constraints, indicating whether there is a foreign key on the primary key of the main controlle.
Property-REF: The primary key attribute name associated with the master class in the association class, the primary key attribute name of the associated class
One-way pair needs to be configured in one side, two-way one-to-one needs to configure both parties
8.Lazy = false: The passive record is taken by Hibernate, which is stored in the Collection type attribute specified by the master party.
9. Java.util.SET or Net.Sof.Hibernate.collecton.bag type Collection
10. Important: Inverse: Used to identify one end of the passive party in the two-way association. INVERSE = FALSE (the main controller) is responsible for maintaining the relationship. Default: false
11. Batch-size: When the delay is loaded, the number of data reads is yesterday.
12. One-to-many update by the main controller (when the main control is one party)
User.Getaddresses (). add (addr); session.save (user); // update through the master target
13. In one-to-many relationship, set the MANY to the active (Inverse = FALSE) will have a performance improvement. When setting up the relationship, inverse = true, is to give the master right to multiple parties, so many people can actively get the Foreign Key from one party, and then INSERT is completed.
Addr.Setuser (user); // Set the associated Tuser object user.getaddresses (). add (addr); session.save (user); // Cascade update
14. Only one side of the primary controller cares for (access) each other's properties, the passive party is not concerned about the attribute of the other party.
15. One-to-Many and the configuration properties of the MANY-TO-ONE node are different: a pair of more relationships. Lazy and Inverse two properties Multi-to-multi-node properties: column: Intermediate mapping table, associated field of associated target table Class: Class name, associated target class Outer-join: Whether to use an external linkage Note: Access is a way of reading attribute values.
Column is setting the associated field.
16. Many-to-many, pay attention to both parties to set Inverse and Lazy, and cascade can only be set to INSERT-UPDATE multi-parallelism, because the association relationship is the two tables, and therefore, the two parties must be simultaneously save.
Group1.getroles (). add (role1); role1.getgroups (). Add (group1);
Session.save (role1); session.save (group1);
17. With regard to the Hibernate capacity processing, the VO and PO Vo turned into PO (the reference to the VO will be saved by the container, and Flush is turned off when Session is closed, so the PO will change to other places to change, it is dangerous) VO And PO mutual conversion: beanutils.copyproperties (Anotheruser, User);
18. For Save operations, if the object has been associated with the session (ie, in a solid container that has been added to the session), there is no need to perform a specific operation. Because after the subsequent session.flush process, Hibernate traverses the objects in this entity container to find changes that have changed entities, generate and perform the corresponding UPDATE statements.
19. If we use the latency load mechanism, it is hoped that in some cases, the function of non-delayed loading, that is, we hope that the user's addresses property hibernate.initialize method can be enforced after session shut down. Loading the associated object to achieve this: This is why we must use the JDK Collection interface (such as set, map), rather than a specific JDK Collection, without a specific JDK Collection, for the reason for the COLECTION attribute. .
20. Transaction: Get the session from SessionFactory, which automatically submits the attributes, is closed (AutoCommit = false), if the JDBC operation is performed, if it is not explicitly called session.begintransAction (), it does not perform transaction operation.
JDBC Transaction: Based on the same session; JTA Transaction: Cross session (cross connection) transaction.
There are three implementation methods for JTA transactions: A. Usertransaction tx = new initialcontext (). Lookup ("..."); tx.commit (); B. Method using Hibernate package: (not recommended) Transaction TX = session.BegintransAction (); tx.commit (); C. Using EJB sessionBean's transaction skills, you only declare the method of JTA transactions as needed in Require as needed in the publishing descriptor.
21. Pessimistic lock, optimistic lock: Optimistic locks are generally implemented through Version, pay attention to the Version node must appear after ID. 22.Hibernate, you can set the page range via Criteria.setFirstResult and criteria.setFetchSize method. The Query interface also provides the same method, Hibernate is mainly implemented in this function in the Diagect class.
23.cache
The Cache policy for each mapping entity is specified in the mapping file
*********************************************************** *** query.list () Differences with query.Isterate (): For query.list () always get all records through a SQL statement, then read it, fill in POJO; but query.Itemate () Then, first obtain the ID of the recorded records that meet the query conditions, and then loop operation, and remove the records corresponding to each ID by separate SELECT SQL, then fill in the POJO.
That is, for the List operation, a SQL is required to be completed. For the Iteerate operation, N 1 SQL is required. The LIST method will not read data from the Cache. Iterator is.
24.ThreadLocal: It maintains a private variable space for each thread. In fact, the principle is the principle to maintain a map in JVM. This MAP's key is the current thread object, and Value is an object instance saved by thread through the ThreadLocal.SET method. When the threadlocal.get method is called, Threadlocal removes the corresponding object returns in the MAP according to the reference to the current thread object.
Thus, ThreadLocal is separated by different threads by a reference to each thread object, thereby separating the variables of different threads. Official standards development manual 25.Hibernate example:.. Public class HibernateUtil {private static SessionFactory sessionFactory; static {try {// Create the SessionFactory 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.opensesis (); 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. The filter implemented by reusing session: 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);}}}} ...
27. The parameterized transaction management function of Spring is quite powerful. The author recommends that to use the container management transaction in the development of Spring Framework, to obtain the best readability of the data logic code. Public Class Userdao Extends Hibernatedaosupport Implements iUserdao {Public Void Insertuser (User) {gethibernateTemplate (). Saveorupdate (user);}}
The above Userdao implements a custom iUserdao interface and extends abstract classes: Hibernatedaosupport HibernateSupport implements the association of HibernateTemplate and SessionFactory instances. HibernateTemplate encapsulates the Hibernate Session operation, and the HibernateTemplate.execute method is the core of a package. * In the Spring configuration file, the entire hibernate.cfg.xml is transplanted.