Java the most common interview questions: Module twelve

Twelve, Hibernate

 

113. Why use hibernate?

  • JDBC database access code to make a package, simplifies the data access layer tedious repetitive code.

  • Hibernate is a JDBC-based persistence framework mainstream, is an excellent ORM implementation. His great degree of coding to simplify the DAO layer

  • hibernate using Java reflection mechanism, instead of bytecode program to implement transparency.

  • hibernate performance is very good, because it is a lightweight frame. Flexibility mapped very well. It supports a variety of relational databases, complex relationships from one-to-many.

 

114. What is ORM framework?

Object - Relational Mapping (Object-Relational Mapping, referred to as ORM), object-oriented development approach is to mainstream development approach in today's enterprise application development environment, a relational database is the main data storage system enterprise application environments permanent storage of data. Object and relational data are two forms of business entities in the form of business entity performance object in memory, the performance of relational data in the database. An association and inheritance relationships between objects in memory, and in the database, relational data can not be directly expressed many to many associations and inheritance. Accordingly, the object - relational mapping (ORM) system is generally present in the form of middleware, the main procedures for mapping objects to a relational database data.

 

115. hibernate how to view sql statement printed in the console?

Reference: blog.csdn.net/Randy_Wang_/article/details/79460306

 

116. hibernate There are several ways to search?

 

  1. hql inquiry

  2. sql query

  3. Conditions inquiry

hql query, sql query, the query conditions 

HQL: Hibernate Query Language Object-oriented wording:. 
Query Query = session.createQuery ( "from the Customer the WHERE name =?" ;) 
query.setParameter ( 0, "Cang teacher" ); 
Query. List (); 



QBC: query By Criteria (criteria query). 
Criteria Criteria = session.createCriteria (the Customer. class ); 
criteria.add (Restrictions.eq ( "name", "flower girls" )); 
List <the Customer> List = criteria.list (); 



the SQL: 
SQLQuery Query = session.createSQLQuery ( "SELECT * from Customer" ); 
List <Object []> List = Query.list (); 

SQLQuery QuerySession.createSQLQuery = ( "SELECT * from Customer" ); 
query.addEntity (the Customer. Class ); 
List <the Customer> List = Query.list (); 



Hql: specific categories of
 1, 2 attribute query, query parameters, query parameters named 3, 4 associated with the query, the query page 5 , statistical functions 



HQL and SQL distinction 

HQL is object-oriented query operations, a structured query language SQL oriented database table structure

 

 

117. hibernate entity class may be defined as the final it?

Hibernate entity classes can be defined as the final category, but this approach is not good. Because Hibernate will use a proxy mode to improve performance in the case of delays associated, if the entity class after you define the class as final, because Java does not allow for the final class is extended, so Hibernate can no longer use a proxy, and this way limits the use of tools can improve performance. However, if you are persistent class implements an interface and declares all turn to the definition of words to all public methods in the entity class in the interface, you will be able to avoid the adverse consequences mentioned earlier.

 

118. Integer and int do use mapping What is the difference in the hibernate?

In Hibernate, if the OID defined type Integer, Hibernate can have a null value if it is judged in accordance with whether an object is temporary, if the OID defined for int type, which also need to set hbm mapping file unsaved- attribute value is 0.

 

119. hibernate How does it work?

hibernate works:

  1. // read and parse the configuration file hibernate.cfg.xml; by Configuration config = new Configuration () configure ().

  2. <= "Com / xx / User.hbm.xml" / mapping resource> hibernate.cfg.xml read by the map information and parse

  3. By SessionFactory sf = config.buildSessionFactory (); // create SessionFactory

  4. Session session = sf.openSession();//打开Sesssion

  5. Transaction tx = session.beginTransaction (); // create and start a transaction Transation

  6. persistent operate the operation data, the operation persistence

  7. tx.commit (); // commit the transaction

  8. Close Session

  9. Close SesstionFactory

 

Difference 120. get () and load () is?

  • load () no other property uses the object when there is no delay in loading SQL

  • get () no other property when using objects, also generated SQL load immediately

 

121. talk about caching mechanism to hibernate?

Hibernate into the cache and a secondary cache buffer.

Session-level cache is a cache, the effective range within a transaction, the built-in can not be uninstalled. Secondary cache is SesionFactory level cache, from application to application launch end effective. Is optional, no default secondary cache, you need to manually open. After saving the database, save a copy in the cache memory, and if necessary to update the database updated simultaneously.

What kind of data suitable for storage to the second level cache?

  • Last reply time are rarely modified data posts

  • Frequently requested location data electricity supplier

  • Not very important data that allows concurrent data appears occasionally

  • It will not be concurrent access to data

  • Constant data

Extended: hibernate's second level cache by default does not support distributed cache. Use memcahe, redis other central cache to cache instead of two.

 

122. hibernate objects What state?

hibernate objects in three states:

  1. Transient (instantaneous): new objects just out, has not set id, set the other values.

  2. Persistent (persistent): Calling the save (), saveOrUpdate (), it becomes Persistent, there id.

  3. Of type Detached (detached): when a session close () after completing become Detached.

     

 

123. What is the difference in hibernate in getCurrentSession and openSession is?

openSession can be seen literally, is to open a new session object, and each time is to open a new session, session if used several times in a row, the same object is not obtained, and used up close to call methods closed session.

 

to getCurrentSession, literally can be seen, the current context is to obtain a session object, when using this method for the first time, will automatically generate a session object, and continuous use times of the session is to give the same object, this is one difference between openSession simple terms, getCurrentSession is this: If you have already used, with the old, if not built a new one.

 

Note: In the actual development, often using getCurrentSession more, because the general process is the same transaction (that is, using a database), it is more or less use of openSession openSession in general is a relatively old set of interfaces .

 

124. hibernate entity class must be no-argument constructor do? why?

 

Must, because hibernate framework calls the default constructor to construct an instance object that newInstance method of class Class, this method is to create an instance of an object by calling the default constructor.

Also remind that if you do not provide any constructors, virtual machines are automatically provide a default constructor (no-argument constructor), but if you provide other constructors have arguments, then the virtual machine will no longer provide a default constructor for you method, then you must manually write the no-argument constructor in the code, or new Xxxx () is being given, so the default constructor is not required, only when there are multiple constructors is necessary here. " It must "means" must be manually written. "

 

(Finish)

Guess you like

Origin www.cnblogs.com/xiaofengwang/p/11257298.html