The latest collection of JAVA interview questions (10)

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 the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

114. What is the ORM framework?

Object-Relational Mapping (ORM), the object-oriented development method is the mainstream development method in today's enterprise application development environment, and the 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: View the printed sql statement in the console in hibernate

116. How many query methods does hibernate have?

  • hql query

  • sql query

  • Conditional 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:

  • Through Configuration config = new Configuration().configure();//Read and parse the hibernate.cfg.xml configuration file
  • Read and parse the mapping information by <mapping resource="com/xx/User.hbm.xml"/> in hibernate.cfg.xml
  • Through SessionFactory sf = config.buildSessionFactory();//Create SessionFactory
  • Session session = sf.openSession();//打开Sesssion
  • Transaction tx = session.beginTransaction();//Create and start transaction Transation
  • persistent operate operation data, persistent operation
  • tx.commit();//Commit transaction
  • Close Session
  • 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

  • When get() does not use other properties of the object, SQL is also generated and loaded immediately

121. Tell me about hibernate's caching mechanism?

The cache in Hibernate is divided into a first-level cache and a 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 the database is saved, a copy of the cache is saved in the memory, and if the database is updated, it must be updated synchronously.

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

  • Last reply time for data posts that are rarely modified
  • Frequently queried data e-commerce locations
  • Not very important data, allowing occasional concurrent data
  • Data that will not be accessed concurrently
  • Constant data

Extension: hibernate's second-level cache does not support distributed cache by default. Use central caches such as memcahe and redis to replace the secondary cache.

122. What are the statuses of hibernate objects?

There are three states of objects in hibernate:

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

  • Persistent (persistent): call save(), saveOrUpdate(), it becomes Persistent with id.

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

Insert picture description here

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, the case of using a database), so in general, openSession is less 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 construction method to construct an instance object, that is, the newInstance method of the Class class. This method creates an instance object by calling the default construction method.

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 constructor is not necessary, it is only necessary when there are multiple constructors, here " Must" means "must be written manually."

Guess you like

Origin blog.csdn.net/weixin_42120561/article/details/114936898