[Tencent Ali byte Java must ask] A collection of common interview questions + detailed answers, don’t worry after reading the interview (10)

2020 latest Java collection of common interview questions + detailed answers (10)

Continue to update Java related materials. Recently, I have spent 7 days consulting the latest interview news of the big guys in an effort to collect more comprehensive interview information. If you want to see the first few collections, you can go to my homepage to find them.

Some of the answers are summarized by myself, and some are collected on the Internet. Don't panic after watching these interviews! If you have more experience, you can share it in the comments. If you have any mistakes, you are welcome to point it out. Please let me know, thank you~

Twelve, Hibernate

 

113. Why use hibernate?

 

  • The JDBC access database code is encapsulated, which greatly simplifies the tedious and repetitive code of the data access layer.

  • Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. He greatly simplified the coding of the DAO layer

  • Hibernate uses Java reflection mechanism instead of bytecode enhancement program to achieve transparency.

  • The performance of hibernate is very good because it is a lightweight framework. The flexibility of mapping is excellent. It supports various relational databases, from one-to-one to many-to-many complex relationships.

Although I have provided you with questions and answer analysis, it is best for you to check the questions for yourself, and then you will learn more.           

 

114. What is the ORM framework?

 

Object-Relational Mapping (ORM), an object-oriented development method is the mainstream development method in today's enterprise application development environment. Relational database is the mainstream data storage system for permanent data storage in the enterprise application environment. Object and relational data are two manifestations of business entities. Business entities are represented as objects in memory and as relational data in databases. There are associations and inheritance relationships between objects in memory, while in a database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, the object-relational mapping (ORM) system generally exists in the form of middleware, which mainly realizes the mapping of program objects to relational database data.

 

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

 

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

 

116. How many query methods does hibernate have?

 

  1. hql query

  2. sql query

  3. Condition query

 

hql查询,sql查询,条件查询

HQL:  Hibernate Query Language. 面向对象的写法:
Query query = session.createQuery("from Customer where name = ?");
query.setParameter(0, "苍老师");
Query.list();



QBC:  Query By Criteria.(条件查询)
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("name", "花姐"));
List<Customer> list = criteria.list();



SQL:
SQLQuery query = session.createSQLQuery("select * from customer");
List<Object[]> list = query.list();

SQLQuery query = session.createSQLQuery("select * from customer");
query.addEntity(Customer.class);
List<Customer> list = query.list();



Hql: 具体分类
1、 属性查询 2、 参数查询、命名参数查询 3、 关联查询 4、 分页查询 5、 统计函数



HQL和SQL的区别

HQL是面向对象查询操作的,SQL是结构化查询语言 是面向数据库表结构的

117. Can hibernate entity classes be defined as final?

 

Hibernate's entity class can be defined as a final class, but this approach is not good. Because Hibernate will use the proxy mode to improve performance in the case of delayed association, if you define the entity class as a final class, because Java does not allow the final class to be extended, Hibernate can no longer use the proxy, so it will limit The use of methods that can improve performance. However, if your persistent class implements an interface and declares all public methods defined in the entity class in the interface, it is your turn to avoid the adverse consequences mentioned above.

 

118. What is the difference between using Integer and int for mapping in hibernate?

 

In Hibernate, if the OID is defined as an Integer type, then Hibernate can determine whether an object is temporary based on whether its value is null. If the OID is defined as an int type, you also need to set its unsaved- in the hbm mapping file. The value attribute is 0.

 

119. How does hibernate work?

 

How hibernate works:

 

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

  2. Read and parse the mapping information by <mapping resource="com/xx/User.hbm.xml"/> in hibernate.cfg.xml

  3. Through SessionFactory sf = config.buildSessionFactory();//Create SessionFactory

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

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

  6. persistent operate operation data, persistent operation

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

  8. Close Session

  9. Close SesstionFactory

 

120. The difference between get() and load()?

 

  • When load() does not use other properties of the object, there is no SQL lazy loading

  • get() When other attributes of the object are not used, SQL is also generated and loaded immediately

 

121. Tell me about hibernate's caching mechanism?

 

The cache in Hibernate is divided into the first level cache and the second level cache.

 

The first level cache is the session level cache. It is valid within the scope of the transaction, and the built-in cache cannot be uninstalled. The second level cache is the SesionFactory level cache, which is valid from the start of the application to the end of the application. It is optional, there is no second-level cache by default, and it needs to be turned on manually. After saving the database, the cache will save a copy in memory. If the database is updated, it will be updated synchronously.

 

What kind of data is suitable for storing in the second-level cache?

 

  • Data that is rarely modified Last reply time of the post

  • Frequently queried data E-commerce location

  • Data that is not very important, occasionally concurrent data is allowed

  • Data that will not be accessed concurrently

  • Constant data

 

Extension: Hibernate's second-level cache does not support distributed cache by default. Use memcahe, redis and other central caches instead of secondary caches.

122. What are the statuses of hibernate objects?

 

There are three states of objects in hibernate:

 

  1. Transient (transient): The object has just come out, the id has not been set, and other values ​​have been set.

  2. Persistent (persistent): Called save(), saveOrUpdate(), it becomes Persistent with id.

  3. Detached: When the session close() is over, it becomes Detached.

 

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

 

openSession can be seen literally, is to open a new session object, and each use is to open a new session, if you use multiple times, the obtained session is not the same object, and you need to call close after use Method to close the session.

 

getCurrentSession, literally, is to obtain a session object in the current context. When this method is used for the first time, a session object is automatically generated, and when it is used multiple times in a row, the obtained session is the same object. This is one of the differences from openSession. In simple terms, getCurrentSession is: if there is an already used one, use the old one, if not, create a new one.

 

Note: In actual development, getCurrentSession is often used more, because generally the same transaction is processed (that is, when using a database), so in general, openSession is rarely used or openSession is an older set of interfaces .

 

124. Does hibernate entity class have to have a parameterless constructor? why?

 

It must be because the hibernate framework will call this default constructor to construct an instance object, that is, the newInstance method of the Class class. This method creates an instance object by calling the default constructor.
 

In addition, if you do not provide any construction method, the virtual machine will automatically provide the default construction method (parameterless constructor), but if you provide other construction methods with parameters, the virtual machine will no longer provide you with the default construction Method. At this time, you must manually write the no-parameter constructor in the code, otherwise new Xxxx() will report an error, so the default construction method is not necessary, it is only necessary when there are multiple construction methods, here " Must" means "must be written manually."

At last

The content of the interview questions is over here, there will be more updates in the follow-up, I hope it will be helpful to everyone.

Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

 It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn the secret code together: csdn

                         

  I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation! Welcome to follow and like!

                                                       

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/108956310