Several questions about hibernate (two)


Seven, talk about the caching mechanism of hibernate?

There are two main hibernate caching mechanisms:

  • Level 1 cache: Also called session cache, it is only valid within the scope of the session and does not require user intervention. It is maintained by hibernate itself. You can clear the object cache through evict(object), and clear() clear all caches in the first level cache. , Flush() flushes out the cache.
  • Second-level cache: application-level cache, effective in all sessions, supports configuration of third-party caches, such as Ehcache

8. What are the statuses of hibernate objects?

Hibernate has three states:

  • Temporary/transient state: directly new object, which has not been persisted (not stored in the database) and is not subject to session management.
  • Persistent state: When the save/saveOrupdate/get/load/list methods of the session are called, the object is in the persistent state.
  • Free state: The object after the session is closed is the free state.

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

There are two main differences:

  • Thread aspect: getCurrentSession will bind the current thread, while openSession will not.
  • Automatic assembly: The transaction in getCurrentSession is managed by spring, so there is no need to manually close it, and the transaction in openSession requires us to manually open and commit the transaction.

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

Every entity class in hibernate must have a parameterless constructor, because the hibernate framework uses reflection api to create an instance of the entity class by calling ClassnewInstance(). If there is no parameterless constructor, an exception will be thrown.

Guess you like

Origin blog.csdn.net/qq_42697271/article/details/113852499