Hibernate query Statement Unified configuration Management

Source: Internet
Author: User

Have you ever been involved in a project where SQL and HQL are flying? In the logical layer, the display layer can see the random query statement? Such practices are extremely damaging the layered architecture, anyway, XP should follow a certain management and norms, then the importance of unified management query statement.

What are the advantages of unified management query statements?

1, maintain the system hierarchy, management statement is the responsibility of the persistent layer, by its own management is the most suitable. Loose coupling is always the goal we aspire to.

2, the unified management facilitates the modification, may reduce the manual modification to bring the low-level mistake.

OK, then consider how to manage these statements.

1. configuration file Management

Use <query> in Hibernate's mapping file

XML code
<query name= ' Finduserbyid ' >
From User eo where eo.id =?
</query>

<query name= ' Finduserbyid ' >
From User eo where eo.id =?
</query> <query> inside is to use the HQL statement property name is the alias that the statement is saved in the container.

Use <sql-query> in Hibernate's mapping file

XML code
<sql-query name= "Finduserbyname" >   
     <return alias= "User" class= "hibernate.entity.User"/>   
                SELECT user.id as {user.id},   
                              User.Name as {user.name}   
                from t_user user WHERE User.Name =?   
< /sql-query> 

<sql-query name= "Finduserbyname" >
<return alias= "user" class= "Hibernate.entity.User"/>
SELECT user.id as {user.id},
User.Name as {user.name}
From T_user user WHERE User.Name =?
</sql-query>

The statement in <sql-query> must be an SQL statement, and the property name is the alias that the statement is saved in the container. The,<reruen> inside indicates the type and alias of the returned object, and the alias is mainly used for the contents of the corresponding SQL {}.

After writing the mapping file of course to tell hibernate to add these statements to the container, there are many ways to configure, there are only the use of spring combined with hibernate configuration, in the Sessionfactorybean configuration to join

XML code
<property name= "mappinglocations" >   
       <list>   
           <value>    
                 classpath:hbm/name-query.hbm.xml   
            </value>   
       </list> 
</property> 

<property name= "Mappinglocations" >
<list>
<value>
Classpath:hbm/name-query.hbm.xml
</value>
</list>
</property>

<sql-query> is more complex to use, so it is not recommended when you encounter complex cross table queries.

2. Label Management

The general habit is to use @namedqueries to unify the statements associated with yourself in the entity, such as query user statements are placed inside 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;

The use of tag management can be a better classification of query statements, and do not have trouble with the configuration file, although the query to modify the configuration file can not be recompiled can be effective, but the query statement changes will not be very frequent, so tag management is a good choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.