1. What are the status of persistent objects?
A: Instantaneous objects (Transient Objects): Objects initialized with the new operator are not immediately persistent. Their states are instantaneous, that is, they do not have any behavior associated with database tables, and as long as the app no longer references these objects (no longer referenced by any other objects), their state will be lost and reclaimed by the garbage collection mechanism.
Persisted object (Persist Objects): A persistent instance is any instance that has a database identity. It has persistent manager session Unified management, persistent instances are operations in the transaction-their state is synchronized with the database at the end of the transaction. When a transaction commits, the in-memory state is synchronized to the database by executing the INSERT, UPDATE, and DELETE statements of SQL.
Offline object (Detached Objects): After the session is closed, the persisted object becomes an offline object. Offline means that the object can no longer be synchronized with the database, and they are no longer managed by hibernate.
2, hibernate working principle and why use?
Answer: Principle:
1. Read and parse the configuration file
2. Read and parse mapping information, create Sessionfactory
3. Get the session and open sesssion
4. Create Transaction Transation
5. Persistent operation
6. Commit a transaction
7. Close session
8. Close Sesstionfactory
Why to use:
1. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.
2. Hibernate is a JDBC-based, mainstream persistence framework that is an excellent ORM implementation. He simplifies the coding of the DAO layer to a great extent
3. Hibernate uses the Java reflection mechanism rather than the bytecode enhancer to achieve transparency.
4. Hibernate performs very well because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.
3. How does hibernate delay loading?
1. Hibernate2 Deferred load implementation: a) collection of entity object B) (Collection)
2. Hibernate3 provides properties for lazy loading functions
When hibernate is querying the data, the data does not exist in memory, and when the program actually operates on the data, the object exists in memory, and the delay is loaded, which saves the server's memory overhead and improves the performance of the server.
4. The caching mechanism of hibernate
A: 1. The internal cache exists in Hibernate, also called the first level cache, belongs to the application thing-level cache
2. Level Two cache:
A) application and caching
b) Distributed cache
Conditions: Data is not modified by third parties, data size is acceptable, data update frequency is low, the same data is frequently used by the system, non-critical data
c) Implementation of third-party caches
5. What is the role of sessionfactory in hibernate? is sessionfactory thread-safe?
A: Sessionfactory is a factory that is used to create a session object for hibernate. Sessionfactory is usually created when the app starts, and the code in the application uses it to get the session object. As a single data store, it is also thread-safe, so multiple threads can use the same sessionfactory at the same time. Java JEE applications typically have only one sessionfactory, and each thread that serves the client's request passes through the factory to get Hibernate's session instance, which is why the Sessionfactory interface must be thread-safe to implement. Also, the internal state of Sessionfactory contains all the metadata associated with the object's relationship mapping, which is immutable and cannot be modified once it has been created.
6. How hibernate is queried
Answer: SQL, criteria,objectcomposition
HQL:
1. Attribute Query
2, parameter query, named parameter query
3. Related queries
4, paging query
5. Statistical functions
7. How to optimize hibernate?
A: 1. Use bidirectional one-to-many associations without using
2. Flexible use of unidirectional one-to-many associations
3. Do not use one-to-many substitution
4. Configure the object cache without using the collection cache
5. One-to-many collection using bag, multi-set using Set
6. Inheriting classes using explicit polymorphism
7. Table fields are less, tables associated not afraid of more, there is a level two cache backing
8, in the database when the condition query speed is very slow, how to optimize?
A: 1. Build an index
2. Reduce the association between tables
3. Optimize SQL, try to get SQL to locate data quickly, do not let SQL do the full table query, should go index, the data volume of the table in front
4. Simplify the query fields, no useless fields, have control over the returned results, and try to return a small amount of data
9. What does the session in hibernate refer to? Can a single session be shared among multiple threads?
A: The session represents a small portion of Hibernate's work, which is responsible for the maintainer's link to the database and is not thread-safe, meaning that the session in Hibernage cannot be shared among multiple threads. Although the session gets a database connection in an active lag, it is best to close the session immediately after it is exhausted.
10. What is the difference between get and load in hibernate?
A: The biggest difference between get and load is that if a corresponding object is not found in the cache, get will directly access the database and return a fully initialized object, which may involve multiple database calls, while the Load method does not find the object in the cache. Only a proxy object is returned, and the database is actually accessed only when other methods other than Object GetID () are called, which can significantly improve performance in some cases. You can also refer to the differences between get and load in Hibernate, which gives more differences and a more detailed discussion of the problem.
11. What are the differences between the three methods of save, persist, and saveorupdate in hibernate?
A: That is, save (), Saveorupdate (), and persist () are all methods used to save objects to the database, but there are some subtle differences. For example, save () only inserts records, but Saveorupdate () can record inserts and update. Also, the return value of Save () is a serializable object, and the Persist () method returns a value of void. You can also visit save, persist, and saveorupdate to find all the differences.
12. What does a named SQL query in hibernate refer to?
A: A named query refers to an SQL query that is defined in a mapped document with a label and can be called by using the Session.getnamedquery () method. A named query allows you to use a name you specify to get a particular query. Named queries in hibernate can be defined using annotations, or they can be defined using the XML innuendo questions I mentioned earlier. In Hibernate, @NameQuery is used to define a single named query, @NameQueries to define multiple named queries.
13. What is the difference between sorted collection and ordered collection in hibernate?
A: Sorted collection is sorted in memory by using Java comparator, and sorting in ordered collection is using the ORDER BY clause of the database. For larger datasets, it is best to use ordered collection in order to avoid sorting them in memory and OutOfMemoryError in Java.
Java Surface question finishing two (focus on the SSH framework)