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

How to unify the configuration and management of Hibernate query statements

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

Share

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

This article is about how to unify the configuration and management of Hibernate query statements. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Were the projects you were involved in, SQL and HQL, flying all over the place? In the logical layer, the display layer where you can see the handwritten Hibernate query statements? This practice extremely destroys the hierarchical architecture, no matter how the XP should follow certain management and norms, then the importance of unified management of query statements is highlighted.

What are the advantages of uniformly managing query statements?

1. To maintain the hierarchical structure of the system, the management statement is the responsibility of the persistence layer, and it is most suitable to be managed by itself. Loose coupling is always the goal we aspire to.

2. Unified management is convenient for modification, which can reduce the low-level errors caused by manual modification.

OK, the next step is to think about how to manage these statements.

1. Configuration file management

Used in the mapping file of the Hibernate query statement

Xml code

From User eo where eo.id =? From User eo where eo.id =? Inside is the hql statement attribute name that is the alias that the statement is stored in the container. Use the Xml code SELECT user.id AS {user.id}, user.name AS {user.name} FROM t_user user WHERE user.name =? in the mapping file of hibernate. SELECT user.id AS {user.id}, user.name AS {user.name} FROM t_user user WHERE user.name =?

The statement must be a sql statement, and the attribute name is the alias that the statement is stored in the container. The content indicates the type and alias of the returned object. The alias is mainly used to correspond to the content of {} in the sql.

After writing the mapping file, of course, you should tell hibernate to add these statements to the container. There are many configuration methods. Only the configuration methods of using spring and hibernate are listed here, and add them to the configuration of SessionFactoryBean.

Xml code

Classpath:hbm/name-query.hbm.xml classpath:hbm/name-query.hbm.xml

Is complex, so it is not recommended when you encounter complex cross-table queries.

2. Label management

In Hibernate query statements, the general habit is to use @ NamedQueries to unify the statements related to yourself in the entity. For example, the statements that query User are put in the User object.

Java code

@ Entity @ Table (name = "t_user") @ Cache (usage = CacheConcurrencyStrategy.READ_WRITE) @ NamedQueries ({@ NamedQuery (name = "User.findById", query = "FROM User eo where eo.id=?")}) public class User implements java.io.Serializable {private int id Private String name; @ Entity @ Table (name = "t_user") @ Cache (usage = CacheConcurrencyStrategy.READ_WRITE) @ NamedQueries ({@ NamedQuery (name = "User.findById", query = "FROM User eo where eo.id=?")}) public class User implements java.io.Serializable {private int id Private String name

Using tag management can better classify Hibernate query statements, and there is no need to make troublesome configuration files. Although Hibernate query statements that modify configuration files can take effect without recompilation, query statements will not be modified very frequently, so tag management is a good choice.

Thank you for reading! This is the end of the article on "how to unify the configuration management of Hibernate query sentences". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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