Hibernate face Test

Source: Internet
Author: User

First, briefly describe the differences between Hibernate and JDBC, advantages and disadvantages?

JDBC has the advantage of JDBC flexibility over performance compared to hibernate. Hibernate has some advantages in easy learning and ease of use. JDBC has an advantage when it comes to many complex multi-table and complex database operations.
Same point:
Both are Java's database operations middleware.
Both objects that operate directly on the database are not thread-safe and need to be shut down in a timely manner.
Both can make explicit transactions on the database's update operations.


Different points:
Different SQL languages used: JDBC uses a relational database-based standard SQL language, Hibernate uses the HQL (Hibernate query Language) language
Different objects are manipulated: JDBC operates data, passes data directly to the database through SQL statements, and Hibernate operates on persisted objects , which are updated to the database by the data of the underlying persisted object.
Data state is different: the data of the JDBC operation is "instantaneous", the value of the variable cannot be consistent with the value in the database, and the data of hibernate operation is durable, that is, the value of the persisted object's data property can be consistent with the value in the database.


JDBC and hibernate read performance
1, JDBC is still the fastest way to access, whether it is a create or read operation, is JDBC fast.
2, Hibernate uses Uuid.hex constructs the primary key, the performance slightly somewhat loses, but is not big.
3, the Create operation, JDBC is faster than hibernate using batch processing, and consuming JVM memory in batch mode is much more than not using batch processing.
4, read the data, Hibernate's iterator is very slow, because he is the next time to go to the database to fetch data , which from the observation Task Manager Java process Memory changes can also be seen clearly, Memory is an increase of dozens of k dozens of K.
5, read the data, Hibernate list is very fast, because he is a one-time to get the data out, this point from observing the Task Manager Java process Memory changes can also be seen clearly, memory is almost 10M 10M increase.
6, the way JDBC reads the data and hibernate is the same as the list (it is very much related to the JDBC driver, different JDBC driver, the results will be very dissimilar), this from the observation of the Java process memory changes can be judged, Because JDBC does not need to construct a bunch of cat object instances like Hibernate, it takes up about half of the JVM's memory than Hibernate's list. The iterator mode of
7, hibernate is not useless, it is suitable to select a small amount of data from a large result set, that is, do not need to occupy a lot of memory, and can quickly get results. The iterator is also suitable for use with JCS buffering.

Additional instructions: In fact, regardless of cmp,hibernate,jdo and so on, all ORM is a JDBC package, CMP is a heavyweight package, JDO medium package, Hibernate is a lightweight package. In theory, ORM can never be better than JDBC performance. Just like any high-level language, running performance will never be better than assembly language.
For create and update operations, Hibernate will show more speed than JDBC because normal Java programmers do not necessarily use the functionality of JDBC batch.
For read operations, ORM generally has a double- layer buffer , that is, prepreadstatement Buffering and resultset buffering, and JDBC itself has no buffering mechanism, in the case of using connection pooling, Some connection pools will provide prepreadstatement buffering, some even provide resultset buffering, but in general, Java programmers generally do not consider optimizing buffering while writing JDBC, and this is not realistic, so in some cases The ORM will show more than the read speed of JDBC.

What is the difference between the load () and get () of Hibernate session?

1, if you use the Load method, Hibernate believes that the ID corresponding to the object (Database records) in the database is a certain existence, so it can be used with confidence, it can be assured that the use of agents to delay loading the object. The database is queried for other property data in the object, but in case the record does not exist in the database, there is no way to throw an exception, and the Load method throw exception means that when the data for that object is used, the exception is thrown when the data in the database is not present, rather than when the object is created. Because the cache in the session is a fairly inexpensive resource for Hibernate, the session cache is checked at load to see if the object exists for that ID, and the proxy is created if it does not exist. So if you know that the ID must have a corresponding record in the database, you can use the Load method to implement lazy loading. For the Get method, hibernate confirms that the data exists for that ID, first in the session cache, then in the level two cache, and not in the database, which returns null ( There are many misconceptions on the internet that get to go to the database to find out at once not first check the session that is not correct, do not want to believe you go to do the test will know).
2, "get () will always return only the entity class", but in fact this is not correct, if the Get method is found in the session cache object corresponding to the ID, if the object is just before it was proxied, such as used by the Load method, or by other associated objects deferred loading, Then the returned proxy object, not the entity class object, if the proxy object has not yet loaded the Entity data (that is, other than the ID of the property data), then it will query the level two cache or the database to load the data, but the return is the proxy object, but the entity data has been loaded.
3, again pay attention to the description get method first query session cache, no query two cache, and finally query the database; Instead, the Load method first queries the session cache, does not create proxies, and queries two caches and databases when the data is actually used.
In short, for get and load the fundamental difference, in a word, hibernate for the load method that the data in the database must exist, can be assured that the use of agents to delay loading, if the use of the problem found in the process, can only throw exceptions, and for the Get method, Hibernate must obtain the actual data, otherwise null is returned.

Vii. Comparison of Hibernate list and iterator modes

(1), list mode is 1 times all the data into the memory, the construction of a large result set, the main time overhead is this step, this step of the time is far more than the JDBC and iterator way to construct the result set of the time overhead, and the memory cost is amazing And the traversal of the result set, the speed is very amazing (after testing, 300,000 recorded memory traversal of less than 100ms, because this step is not affected by JDBC, so the result is trustworthy). Therefore, the list method is suitable for repeated operations on the result set, such as paging, traversing backwards, jumping to the first row, jumping to the last line, and so on.

(2), the iterator method only takes the record ID into memory, and does not take all the data into memory, so the time overhead of constructing the result set is small, less than the JDBC and list methods, and the memory overhead is much smaller. In the case of the traversal of the result set, iterator still accesses the database, and all the major time overhead is spent here. As a result, the iterator approach is suitable for only 1 traversal operations on the result set, and the iterator approach is particularly well suited to taking small amounts of data from the very large result sets, which iterator performance is very good.

What is the role of Hibernate's inverse properties?

In hibernate, the term inverse is the meaning of inversion, in the association relationship, the inverse= "false" is the main control, which is responsible for maintaining the object's association relationship.
Inverse determines whether changes to the collection in the object are reflected in the database, so inverse only works on the collection, that is, only for One-to-many or many-to-many (because only these two associations contain collections. and one-to-one and Many-to-one only contain a reference to the other side, note that the general only in two-way association when you need to set inverse).
(1), one-to-many:
Usually in the one side to give up the maintenance of many relationships, so that the efficiency will be higher (such as the teacher remembers each student is a difficult thing, the efficiency is very low, so simply do not remember, this relationship by students to maintain, students remember a teacher is very easy)
So should be set on one side of the inverse=true, many of the parties set Inverse=false (more than one side can also not set the inverse property, because the default value is False), which indicates that the association is maintained by many Parties.
If one side maintains the relationship, it causes the object to update the "many" side of each object that is associated with the "one" when inserting or deleting the "a" side.
And if the "many" aspects of the maintenance relationship will not have an update operation, because the relationship is in the object of the multi-party, directly into the insertion or deletion of multiple objects on the line.
Obviously doing so will reduce the number of operations and improve efficiency.
Note: In a one-way One-to-many Association relationship, you cannot set inverse= "true" because there is no information for the master in the mapped file of the accused party.
(2), many-to-many: attributes are in separate tables. The default value of the inverse property is false. In a many-to-many association relationship, both ends of the relationship cannot be set to false, that is, the default is not inverse, and if it is set to false, the insert operation causes two relationships to be inserted in the relational table. Neither can be set to true, and if it is set to true, no action is triggered on the relational table. So set the inverse=true on either side and the other side inverse=false.

Ix. the role of Hibernate's Cascade attribute?

The purpose of the Cascade property is to describe the cascading nature of the associated object as it operates. Therefore, only the elements that relate to the relationship have the Cascade attribute.
Tags with cascade attributes include <many-to-one/> <one-to-one/> <any/> <set/><bag/> <idbag/> <l ist/> <array/>
Note: <ont-to-many/> and <many-to-many/> are used inside the collection tag, so the Cascade attribute is not required.
Cascade operation: Refers to whether the same action is performed on the associated party when the main control performs an operation.

What is the difference between lazy lazy loading under various related relationships?

1, one-to-one lazy loading
Lazy loading is not often a one-to-one, because lazy loading is designed to reduce the interaction with the database and thus improve execution efficiency, while in a single relationship, each data in the primary table is only for a database from the table, even if the query does not increase the cost of the interaction, and the main table cannot have contrained= True, so the primary table is not lazy to load. But from the table can have.
This lazy loading must satisfy three conditions on the same side of the object:
1, Lazy!=false (lazy properties have three options: No-proxy, False and proxy)
2, Constrained = true;
3, Fetch=select.
Note: Lazy loading is invalidated when fetch is set to join. Because fetch is a fetch, he has two values of select and join, and the default value is select. That is, when set to join, he will directly query from the table information to join instead of using the Select query again, resulting in lazy load invalidation.
2, One-to-many lazy loading
Unlike the one-to-one association, for One-to-many, each property of the main table will have more than one data from the table, and lazy loading is very effective at this time. For example, a department has a number of employees, if not lazy loading, each query this department will query out multiple employees, which will greatly increase the cost of interaction with the database. So hbernate default is to join lazy loading. This is why a persistentindexed* type object is returned when the collection property is queried. The object is actually a proxy object. Of course, you can disable it in the mapping file by setting the Lazy property to false.
Hibernate defaults to One-to-many is lazy loading, but users can also cancel lazy loading operations:
One: Set lazy= "false";
Two: Set fetch= "Join".
This lazy loading must satisfy two conditions on the same side of the object:
1, Lazy!=false (lazy properties have three options: No-proxy, False and proxy)
2, Fetch=select.
3, Many-to-one lazy loading
The lazy loading of this association relationship is just as easy as one-to-one lazy loading, because the efficiency of execution is not very obvious. While many-to-one relationships are the same, in hibernate, lazy loading is the default. It is also important to note that lazy loading does not distinguish whether there are values in the collection properties, and even if there is no value, he still uses lazy loading.
This lazy loading must satisfy two conditions at a time from the object side
1, Lazy!=false (lazy properties have three options: No-proxy, False and proxy)
2, Fetch=select
4, Many-to-many lazy loading
The lazy loading of this association and the lazy loading of the one-to-many are very obvious to the efficiency of the program execution.
This lazy loading must satisfy two conditions on the same side of the object:
1, Lazy!=false (lazy properties have three options: No-proxy, False and proxy)
2, Fetch=select
Lazy-loaded objects are changed proxy objects, and when the corresponding object is not closed, access to the properties of these lazy-loaded objects (except GetID and GetClass) Hibernate initializes the agents, or uses hibernate.initalize (proxy) To initialize the proxy object, and when the session is closed, an exception is thrown when the lazy-loaded object is accessed.

What are the differences between lazy use in hibernate?

The duration of the lazy: only valid when the session is opened, and lazy when the session is closed.
The lazy policy can be used in:
<class> Label: can be value true/false
<property> tag, you can take the value True/false, this feature requires class enhancement
<set>/<list>, you can take a value of True/false/extra
<one-to-one>/<many-to-one> and other labels, can be value false/proxy/no-proxy
6.1 The difference between get and load:
Get does not support lazy loading, while load is supported.
When querying for data that does not exist in a particular database, GET returns NULL, while load throws an exception.
Deferred loading of Class 6.2:
Lazy= "True" in the Settings <class> tab, or leave the default (that is, do not configure the Lazy property)
If the property value of lazy is true, then the SQL statement is emitted only when the data is actually used when loading with the Load method, which can reduce the overhead of the system.
Note: The lazy attribute configured on the class label does not affect the associated object!!!

12. What is the difference between Ibatis and hibernate?

The same point: masking the underlying access details of the JDBC API, using our ability to access data without dealing with the JDBC API.
The JDBC API programming process is fixed, and SQL statements are mixed with Java code, often with a patchwork of SQL statements, and the details are cumbersome.
Ibatis Benefits: Masking The underlying access details of the JDBC API, separating the SQL statement from the Java code, providing the ability to automatically encapsulate the result set as a collection of entity objects and objects, queryForList returns a collection of objects, Returns a single object with queryForObject, and provides parameters that automatically pass the properties of an entity object to an SQL statement.
Hibernate is a fully automated ORM mapping tool that automatically generates SQL statements, Ibatis requires us to write SQL statements in XML configuration files, and hibernate is more responsible and powerful than the Ibatis feature. Because hibernate generates SQL statements automatically, and we can't control the statement, we can't write a specific high-efficiency SQL. For some of the less complex SQL queries, hibernate can be very good for us to do, but for particularly complex queries, hibernate is difficult to adapt to, this time with Ibatis is a good choice, because ibatis or we write SQL statements.

"Why is it important to provide a parameterless constructor in a hibernate entity class?" 】

Each hibernate entity class must contain a parameterless constructor, because the hibernate framework uses the reflection API to create instances of these entity classes by calling Class.newinstance (). If no parameterless constructor is found in the entity class, this method throws a Instantiationexception exception.

"Can I define hibernate's entity class as the final class?"
Yes, you can define Hibernate's entity class as the final class, but this is not a good practice. Because Hibernate uses proxy mode to improve performance in the case of deferred correlation, if you define the entity class as the final class, because Java does not allow the final class to be extended, hibernate cannot use the proxy anymore. This limits the use of the means to improve performance. However, if your persistence class implements an interface and you declare all of the methods defined in the entity class for all public in that interface, you can avoid the previously mentioned adverse consequences.

Hibernate face Test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.