Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Hibernate interview questions?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the Hibernate interview questions". In the daily operation, I believe many people have doubts about the Hibernate interview questions. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the Hibernate interview questions?" Next, please follow the editor to study!

1. What is Hibernate?

Hibernate is a schema that corresponds to Java object database table objects. Use a xml file to configure.

2. Why use Hibernate?

① Hibernate is the encapsulation of jdbc, which greatly simplifies the tedious and repetitive code of the data access layer.

② Hibernate is an excellent ORM implementation, which simplifies the coding function of the DAO layer to some extent.

③ can transplant the database conveniently.

④ provides a caching mechanism, which is efficient for programs to execute changes.

3. What is the ORM framework?

ORM (Object Relation Mapping) object-relational mapping is to map the relational data in the database into objects in the program.

The advantages of using ORM: it improves the development efficiency, reduces the development cost, makes the development simpler and more objective, and is more portable.

4. How to view printed SQL statements in the console in Hibernate?

In Config, put Hibernate. Just set show_SQL to true. However, it is not recommended to open it, which will reduce the running efficiency of the program.

5. What is the difference between get and load in Hibernate?

Comparing get and load together is the most frequently asked question in an Hibernate interview, because it is only possible to use Hibernate efficiently after a correct understanding of both get () and load ().

The biggest difference between get and load is that if the corresponding object is not found in the cache, get will directly access the database and return a fully initialized object, and this process may involve multiple database calls.

On the other hand, when the load method does not find an object in the cache, it will only return a proxy object, and only when methods other than the object getId () are called will actually access the database, which can greatly improve performance in some cases.

You can also refer to the differences between get and load in Hibernate. This link gives more differences and discusses the issue in more detail.

6. How does Hibernate work?

① reads and parses the configuration file.

② reads and parses the mapping file to create a SessionFactory.

③, open Session.

④ creates a transaction.

⑤ performs persistence operations.

⑥ commits the transaction.

⑦ closes Session.

⑧ closes SessionFactory.

7. What are the differences among save, persist and saveOrUpdate in Hibernate?

In addition to get and load, this is another frequent Hibernate interview question. All three methods, namely save (), saveOrUpdate (), and persist (), are used to save objects to the database, but there are some subtle differences.

For example, save () can only record INSERT, but saveOrUpdate () can record INSERT and UPDATE.

Also, the return value of save () is a Serializable object, while the persist () method returns void. You can also visit save, persist, and saveOrUpdate to find all their differences.

8. What is the named SQL query in Hibernate?

This interview question for Hibernate is related to the query function provided by Hibernate. A named query refers to a SQL query defined in a mapped document with tags, which can be called by using the Session.getNamedQuery () method.

Named queries allow you to get a particular query using a name you specify. Named queries in Hibernate can be defined using annotations or by using the xml innuendo question I mentioned earlier.

In Hibernate, @ NameQuery is used to define a single named query and @ NameQueries is used to define multiple named queries.

9. What is the state of the Java object in Hibernate?

① temporary status (transient): not in the cache of Session. OID is null or the value of the unsaved-value property equal to id.

② persistence state (persistent): added to Session's cache.

③ Free State (detached): has been persisted, but is no longer in Session's cache.

10. What is the function of SessionFactory in Hibernate?

Is SessionFactory thread safe? This is also a common interview question in the Hibernate framework. As the name implies, SessionFactory is a factory for creating Session objects for Hibernate.

SessionFactory is usually created when the application 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 generally have only one SessionFactory, and all threads serving customer requests use this factory to obtain Session instances of Hibernate, which is why the implementation of the SessionFactory interface must be thread-safe.

Also, the internal state of SessionFactory contains all the metadata related to object-relational mapping, which is immutable and cannot be modified once created.

11. What does "Session" in Hibernate mean? Can a single Session be shared among multiple threads?

After the previous questions are asked, they are usually followed by these two questions. After asking SessionFactory's question, it's Session's turn. Session represents a small part of the work done by Hibernate, which maintains links to the database and is not thread-safe.

That is, Session in Hibernage cannot be shared among multiple threads. Although Session will obtain a database connection in an active lag manner, it is best for Session to shut it down as soon as it is used up.

12. What's the difference between sorted collection and ordered collection in Hibernate?

This is the easier question of all the Hibernate interview questions you will encounter. Sorted collection is sorted in memory by using Java's Comparator, and the sorting in ordered collection uses the database's order by clause.

For larger datasets, to avoid sorting them in memory and appearing OutOfMemoryError in Java, it is best to use ordered collection.

13. What is the difference among transient, persistent, and detached objects in Hibernate?

In Hibernate, objects have three states: transient, persistent, and detached. The object associated with the session of Hibernate is the persistent object.

All changes made to this object are reflected in the database in accordance with the pre-set refresh strategy, that is, it can be refreshed automatically when any property of the object changes, or explicitly by calling the Session.flush () method.

If an object is originally associated with Session, but is no longer associated with it now, such an object is the object of detached. You can re-associate the detached object with the corresponding seesion by calling either of the session's () or saveOrUpdate () methods.

The Transient object refers to an instance of a newly created persistent class that has never been associated with any Session of Hibernate. Similarly, you can call the persist () or save () method to turn the transient object into a persistent object.

Keep in mind, however, that the transient here does not refer to the transient keyword in Java, and the two are irrelevant.

14. What is the purpose of the lock () method of Session in Hibernate?

This is a tricky Hibernate interview question because Session's lock () method rebuilds the relationship without synchronizing and updating with the database. Therefore, you must be careful when using the lock () method.

By the way, you can use Session's () method to synchronize with the database at any time when rebuilding relationships. Sometimes this question can be asked like this: what is the difference between the lock () method and the () method of Session? The key points in this section can also be used to answer this question.

15. What does secondary cache refer to in Hibernate?

This is the first interview question related to Hibernate's caching mechanism, and there will be more questions to come. Secondary caching is a cache maintained at the SessionFactory level, which can improve performance by saving several round trips to database calls. It is also worth noting that secondary caching is for the entire application rather than for a particular session.

16. What does query cache in Hibernate refer to?

This question is sometimes raised as a follow-up to the last Hibernate interview question. The query cache actually holds the results of the sql query, so that you can get the results from the cache between the same sql queries.

To improve performance, query caching can be used in conjunction with secondary caching. Hibernate supports a variety of different open source caching schemes, such as EhCache, to implement query caching.

17. Why is it important to provide a parameterless constructor in the entity class of Hibernate?

Each Hibernate entity class must contain a no-argument constructor because the Hibernate framework uses Reflection API to create instances of these entity classes by calling Class.newInstance (). If no parameterless constructor is found in the entity class, this method throws an InstantiationException exception.

18. Can the entity class of Hibernate be defined as the final class?

Yes, you can define the entity class of Hibernate as the final class, but this is not a good practice. Because Hibernate uses proxy mode to improve performance in the case of delayed association, if you define an entity class as a final class, Hibernate can no longer use proxies because Java does not allow extensions to final classes, thus limiting the use of means that can improve performance.

However, if your persistent class implements an interface and declares all the methods defined in all the public in the entity class, you can avoid the adverse consequences mentioned earlier.

19. Advantages and disadvantages of Hibernate

Advantages of ①:

Encapsulated the code for JDBC to access the database, simplifying the tedious repetitive code of the data access layer

Mapping flexibility, which supports a variety of relational databases, from one-to-one to many-to-many complex relationships

Non-invasive and portability will be good

> caching mechanism: first-level cache and second-level cache are provided.

Disadvantages of ②:

Unable to optimize SQL

> the use of ORM principles in the framework results in overly complex configuration

> execution efficiency deviates from native JDBC: especially when batch data processing

> batch modification and deletion are not supported.

Does the Hibernate entity class have to have a nonparametric constructor? Why?

Each entity class in Hibernate must provide a no-argument constructor, because the Hibernate framework uses reflection api, creates an instance of the entity class by calling ClassnewInstance (), and throws an exception if there is no no-argument constructor.

At this point, the study of "what are the Hibernate interview questions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report