1.Hibernate What are the ways to query data
(1) Navigation object Graph query
(2) OID Query
(3) HQL
(4) QBC
(5) Local SQL
the difference between 2.load () and get ()
Load Loading method:
Users user = (users) session.load (Users.class, userId);
Get Loading method:
Users user = (users) session.get (Users.class, userId);
Two Load method differences:
Difference 1:
If there are no UserID objects in the database. If it is loaded by the Get method, a null is returned, and if loaded by load, a proxy object is returned if the subsequent code calls a property of the user object (such as User.getpassword () ) throws an exception: Org.hibernate.ObjectNotFoundException;
Difference 2:
Load supports lazy loading, and get does not support lazy loading.
Other words:
Users user = (users) session.load (Users.class, userId);
This code does not execute the database query, but only when the user is used to execute the database query. and
Users user = (users) session.get (Users.class, userId);
Execute the database query immediately. So users user = (users) session.load (Users.class, userId); no SQL is executed.
= (users) session.load (users). Class= (users) session.load (users). Class, UserId); System.out.println (User.getid ()); The above 2 lines of code do not perform database operations. Since load will store a map object in the Hibernate cache, the map key is the value of the UserID, but when you GetID (), it goes to the first-level cache and takes the key value of the map instead of executing the database query. So I won't report anything wrong. Does not perform any database operations.
3. How does hibernate work and why should I use it?
Principle:
1. Read and parse the configuration file
2. Read and parse mapping information, create Sessionfactory
3. 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.
4. 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.
5. How do the relationships between classes be implemented in hibernate? (e.g. one-to-many, many-to-many relationships)
The relationship between classes and classes is mainly manifested in the relationship between tables and tables, and they operate on objects, and in our program we map all the tables and classes together, through the Many-to-one in the configuration file,
One-to-many, Many-to-many,
6. The caching mechanism of hibernate
1. Internal cache exists Hibernate is also called a first-level cache, belongs to the application of the 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
7. How hibernate is queried
SQL, Criteria,object comptosition
HQL:
1. Attribute Query
2, parameter query, named parameter query
3. Related queries
4, paging query
5. Statistical functions
8. How to optimize hibernate?
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
=====================hibernate Pen Question ==========================
(1) In general, what are the matching relationships between the relational data model and the object model (multiple selection)
A) Table corresponding to class B) record corresponding object C) table field corresponding to the class's attribute D) the reference relationship between tables corresponds to the dependency between classes
(2) Which of the following statements about Sessionfactory are correct? (Multiple selection)
A) For each database transaction, you should create a Sessionfactory object
B) A Sessionfactory object corresponds to a database storage source.
C) Sessionfactory is a heavyweight object and should not be created arbitrarily. If there is only one database storage source in the system, you only need to create one.
D) the Sessionfactory load () method is used to load the persisted object
(3) The customer class has a set type of orders property that holds the order object, and in the Customer.hbm.xml file, which element is used to map the orders property?
A) <set> B) <one-to-many> C) <many-to-one> D) <property>
(4) The <set> element has a cascade attribute, what value should the Casecade attribute take if you want Hibernate to cascade to save the objects in the collection? (radio)
A) None B) Save C) Delete D) save-update
(5) Which of the following methods are part of the session?
A) load () B) Save () C) Delete () D) update () E) open () F) Close ()
(6) What are the printing results of the following programs? (radio)
tx = Session.begintransaction ();
Customer c1= (Customer) Session.load (Customer.class,new Long (1));
Customer c2= (Customer) Session.load (Customer.class,new Long (1));
System.out.println (C1==C2);
Tx.commit ();
Session.close ();
A) Run error, throw exception B) print false C) print true
(7) The following program code has been modified two times for the customer's Name property:
tx = Session.begintransaction ();
Customer customer= (Customer) session.load (Customer.class,
New Long (1));
Customer.setname (\ "Jack\");
Customer.setname (\ "mike\");
Tx.commit ();
To execute the above procedure, hibernate needs to submit several update statements to the database? (Single radio)
A) 0 B) 1 C) 2 D) 3
(8) In the persistence layer, what states are the objects divided into? (Multiple selection)
A) temporary state B) independent status C) Free State D) Persistent state
(9) For the following program, the customer object becomes persisted in the first few lines? (Single radio)
Customer Customer=new customer (); Line1
Customer.setname (\ "tom\"); Line2
Session session1=sessionfactory.opensession (); Line3
Transaction tx1 = Session1.begintransaction (); Line4
Session1.save (customer); Line4
Tx1.commit (); Line5
Session1.close (); Line6
A) line1 B) line2 C) line3 D) line4 E) line5 F) Line6
(10) for the following program, the customer object in the first few lines into a free state? (Single radio)
Customer Customer=new customer (); Line1
Customer.setname (\ "tom\"); Line2
Session session1=sessionfactory.opensession (); Line3
Transaction tx1 = Session1.begintransaction (); Line4
Session1.save (customer); Line4
Tx1.commit (); Line5
Session1.close (); Line6
A) line1 B) line2 C) line3 D) line4 E) line5 F) Line6
(11) Which of the following search strategies take advantage of an external link query? (Single radio)
A) Immediate retrieval B) deferred retrieval C) urgent left-side link search
(12) Assume that a deferred retrieval strategy is used for the orders collection of the customer class, and what happens if you compile or run the following program (radio)
Session session=sessionfactory.opensession ();
tx = Session.begintransaction ();
Customer customer= (Customer) Session.get (Customer.class,new Long (1));
Tx.commit ();
Session.close ();
Iterator orderiterator=customer.getorders (). Iterator ();
A) Compile error B) compile pass, and normal run C) compile pass, but runtime throws exception
(13) About HQL and SQL, which of the following statements is correct? (Multiple selection)
A) HQL is no different from SQL
B) Hql object-oriented, while SQL manipulates relational database
C) in HQL and SQL, both contain select,insert,update,delete statements
D) hql only for querying data, insert,update and DELETE statements are not supported
(14) Who is the transaction isolation level implemented by? (Single radio)
A) Java application B) Hibernate C) database system D) JDBC Driver
(15) Pessimistic lock and optimistic lock, which has good concurrency performance? (Single radio)
A) pessimistic lock B) optimistic lock
Answer:
(1) A,b,c (2) b,c (3) A (4) D (5) a,b,c,d,f (6) C (7) B (8) a,c,d (9) D (+) F (one) c (+) C (+) B,d (+) C (+) b
Hibernate face Test