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 basic knowledge points of Hibernate

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the basic knowledge points of Hibernate". In the daily operation, I believe that many people have doubts about the basic knowledge points of Hibernate. 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 basic knowledge points of Hibernate?" Next, please follow the editor to study!

The relevance of Hibernate Foundation

◆ one-way association: one-way association has no inverse usage

◆ two-way association: use inverse to specify. False = false (default) indicates that you are the owner of the association, and Inverse=true indicates that the other end of the association is the owner of the association.

How do I manage two-way associations?

Both ends of the association must be explicitly managed with changes to one end, reset at both ends, and synchronized. How to determine the owner of the association (generally speaking, the end with the foreign key is the owner of the association?)

◆ one-to-one: either end can act as the owner of the association. But one of them should be chosen (and only one can be selected). If no owner is specified, it will lead to circular dependency.

◆ one-to-many: the many side must be the owner of the association. (usually multiple terminals have foreign keys.)

◆ many-to-one: the many side must be the owner of the association. (usually multiple terminals have foreign keys.)

◆ many-to-many: either end can act as the owner of the association.

Use of saveOrupdate () on the basis of Hibernate

It is not appropriate to save persisted objects, nor is it appropriate to update transient objects. If it is impossible or inconvenient to determine the state of an object from your application code, you can use the saveOrUpdate () method.

Load () and get () of Hibernate Foundation

Public Object load (Class theClass, Serializable id) throws HibernateException public Object load (String entityName, Serializable id) throws HibernateException public Object get (Class clazz, Serializable id) throws HibernateException public Object get (String entityName, Serializable id) thtows HibernateException

The fundamental difference between the get method and the load method in Hibernate is:

1. For the get method, Hibernate confirms whether the data corresponding to the id exists, first looks it up in the session cache, then looks up it in the secondary cache, and returns null before querying the database.

When the 2.load method loads an entity object, it is discussed on a case-by-case basis according to the configuration of the lazy attribute at the class level on the mapping file (default is true):

If ◆ is true, first look in the Session cache to see whether the corresponding object of the id exists. If it does not exist, delay loading is used to return the proxy class object of the entity (the proxy class is a subclass of the entity class and is dynamically generated by CGLIB). Wait until the specific use of the object, and then query the secondary cache and database, if still do not find a qualified record, an ObjectNotFoundException will be thrown.

If ◆ is false, it is in the same order as the get method, but in the end, if no qualified record is found, an ObjectNotFoundException will be thrown.

There are two important differences between get and load:

If a record that matches the condition is not found, the get method returns null, and the load method throws an ObjectNotFoundException. The load method returns an instance of the proxy class that does not have entity data loaded, while the get method always returns an object that has entity data. (for the return types of load and get methods: many books say that "the get method always returns only the entity class". In fact, it is not true that the get method finds the corresponding object of the id in the session cache, and if the object happens to be proxied, such as used by the load method, or delayed loaded by other associated objects, then the original proxy object is returned instead of the entity class object. If the proxy object has not yet loaded entity data (that is, attribute data other than id), it queries the secondary cache or database to load the data, but the proxy object is returned, but the entity data is already loaded.)

Refresh entity based on Hibernate

Hibernate provides a mechanism to refresh (refresh) persistent objects with database tables. Refresh an instance of a persistent object using one of the refresh () methods of the Session interface.

Public void refresh (Object object) throws HibernateException so far, the study of "what are the basic knowledge points of Hibernate" 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

Development

Wechat

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

12
Report