During his understanding of hibernate, Mr. Fan Kai gave many careful guidance, which made me less detours. He had a great chance to talk with him at the csdn hero conference. Thank you.
Let's take a look at a common problem: inverse =?
We consider two situations:
Inverse = false (default)
1. One-to-Multiple Association
2. Parent. getchildren (). Add (child) insert sub-Object
3. Parent. getchildren (). Delete (child) Delete sub-objects
Inverse = true
1. Bidirectional one-to-Multiple Association
2. Child. setparent (parent); Session. Save (child) insert sub-Object
3. session. Delete (child) Delete sub-objects
In a layered system, crud Encapsulation by parentdao and childdao often persists objects directly through the session interface, but seldom accesses objects through association.
When considering the performance of hibernate, we need to consider one-to-many relationships.
Unidirectional or bidirectional?
1. The hitting operation of parent. getchildren (). Add (child) on the set will lead to the initialization of the lazy set. This operation should be avoided if no second-level cache is configured for the set.
2. Select * from child where parent_id = xxx
In this case, the key points of performance are;
1. Avoid unidirectional association in general, and try to use bidirectional association as much as possible.
2. Use bidirectional association with inverse = true
3. In the layered structure, the session is used to directly persist objects through the DaO interface, avoiding accessibility persistence through associations.
Multi-to-one relationship
One-way multi-to-one relationship expresses the foreign key storage object
Flexible use of multiple-to-one can avoid unnecessary performance problems
The expression of multiple-to-one is: 0 .. n: 1. Multiple values can be 0, 1, or N.
1-to-1
Associate with a primary key
This is equivalent to splitting a large table into multiple small tables.
For example, split large fields separately to improve database performance.
The 1-to-1 attribute of hibernate cannot be lazy. You must use bytecode enhancemant
Set list, bag, Set
In case of one-to-many scenarios:
1. Index column needs to be maintained for list. It cannot be used for Bidirectional Association. It must be inverse = false. Exercise caution when using it in rare scenarios.
2. There is no semantic difference between bag and set.
3. Bag is recommended.
In many-to-many cases:
1. There are differences in bag/set Semantics
2. Set is recommended.
Filter Sets
1. Children = session. createfilter (parent. getchildren (), "where this. age> 5 and
This. age <10 "). List ()
It is particularly suitable for paging of a large set when the set elements in one-to-Multiple Association are very large.
2. session. createfilter (parent. getchildren (),
""). Setfirstresult (0). setmaxresults (10). List ();
Implicit polymorphism in the inheritance relationship
1. hql: From Object
1.1 All database tables will be queried
1.2. polymorphism = "implicit" (default) extracts all the current object and all the inherited subclasses of the object at a time.
1.3. polymorphism = "explicit" only retrieves the current query object
Hibernate second-level cache
The famous n + 1 problem: from child. Then, the parent class information of each subclass is displayed on the page, which leads to N queries to the parent table:
Select * from parent where id =?
Solution:
1. Eager fetch
2. Secondary Cache
Hibernate second-level cache is a magic weapon to improve Web Application Performance
For OLTP web applications, because the application server can perform horizontal cluster scaling, the final system bottleneck is always unable to escape database access. What framework can minimize database access and reduce database access pressure, and which framework provides higher performance;
Database cache policy:
1. Object cache: Fine Granularity, transparent access to the table's record level, can greatly improve the performance of Web applications without changing the program code. Objects are critical to Orm.
2. The advantages and disadvantages of Object Caching depend on the implementation level of the framework. The Oracle toplink object cache in the open-source framework is the most powerful.
3. query cache: coarse granularity, applicable to scenarios with low real-time data requirements for query result sets.
Select:
1. hibernate: it tends to be fine-grained, object-oriented, and large tables are split into small tables to eliminate redundant records and improve performance through second-level cache. DBA is very jealous of the appearance of associations, however, the ORM cache will break through the performance bottleneck of the association relationship. The key to hibernate's performance lies not in the association relationship, but in the operations on large tables.
2. ibatis: it tends to design with coarse granularity and is oriented to the relationship. It is recommended to merge tables as much as possible, and there is no effective cache means to eliminate the association through Table Record redundancy. The key to ibatis performance lies not in large table operations, but in associations.
3. toplink: it tends to be coarse-grained, relational-oriented, and has effective but complex caching methods. Its cache can also be effectively associated with Oracle database RAC parameters, which is very effective for DBAs. However, it is complicated to master. The most effective commercial ORM before open source
3. JPA: the New Technology of J2EE 5, designed by hibernate. It is a new method that can be reused with hibernate. It draws on the advantages of Hibernate and adds many features that support other J2EE standards and protocols. It is worth noting. As it is Sun's standard Orm, it is bound to receive strong support from middleware vendors. For medium-and large-sized projects, it can well use the technical resources and technical support of various middleware vendors to solve problems in actual development, or even hibernate problems. If Hibernate is used before, the future migration of JPA can be very smooth.
4. Sap: it is a commercial product that is very powerful and located on the SAP NetWeaver technology platform. But expensive. We recommend that you do not miss it if you have the opportunity to learn it.