Ibernate Learning Note 5---Entity class or attribute name conflicts with database keywords, hql named arguments, HQL implements generic paging

Source: Internet
Author: User
Tags generator


I. Entity class or attribute name conflicts with database keywords
1. Entity class name conflicts with keyword in database
Example: Entity table user conflicts with system tables in Oracle
Workaround 1: Add the Table property to the XML and specify the name of the tables so that they are not equal to the name by default

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Cn.itcast.hibernate.domain">
  6. <class name="User" table="Tb_user">
  7. <!--object identifiers, type can not be written, hibernate self-identification
  8. <ID name="id" column= "id">
  9. <!--Specify how the primary key is generated.
  10. Native how to generate a primary key based on dialect judgment
  11. -->
  12. <generator class="native"/>
  13. </ID>
  14. <property name="name" column="name" />
  15. <property name="Birthday" />
  16. </class>
  17. </hibernate-mapping>

Workaround 2: If the corresponding table is the original, you cannot modify the table name, you can add "in the Table property" (inverted quotation marks are 1 front of the key, to specify the name)

<class name= "user" table= "' User '" >

2. Conflict between attribute name and database keyword
Workaround 1; Add the column attribute to the property node in XML to specify a specific field

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Cn.itcast.hibernate.domain">
  6. <class name="User" table="Tb_user">
  7. <!--object identifiers, type can not be written, hibernate self-identification
  8. <ID name="id" column= "id">
  9. <!--Specify how the primary key is generated.
  10. Native how to generate a primary key based on dialect judgment
  11. -->
  12. <generator class="native"/>
  13. </ID>
  14. <property name="name" column="name" />
  15. <property name="Birthday" />
  16. </class>
  17. </hibernate-mapping>


Workaround 2: You cannot modify the field name, you can add ' ' in the column property (the backslash is the 1 front key to specify the name)

<property name= "name" column= "' Name '"/>
Ii. about all attribute values of Hibernate.cfg.xml
You can download the source code on the Hibernate website.
Hibernate-release-4.2.1.final\project\etc\hibernate.properties.template

Or

Configurable property values in the Hibernate-release-4.2.1.final\project\etc\hibernate.properties file

Iii. named parameters of HQL
Example:
/**
* Use HQL according to the name Query method
* @param entity
*/
public static void Query (String name) {

Session s = null;
try {
S=hibernateutil.getsession ();
HQL:
The from behind is not the table name, but the object name (class name)
String hql = "from user as user where user.name=?"; From Object support multi-attitude
Query query = s.createquery (HQL);
Query.setstring (0, name);
List<user> list=query.list (); Executquery ();
for (User user:list) {
System.out.print (User.getname ());
}

If you determine that there is at most one piece of data, you can simplify the code by using the method
User u= (user) Query.uniqueresult ();
System.out.print ("Only one piece of data" +u.getname ());
} finally {
if (s!=null) {
S.close ();
}
}
}
The parameters of the subsequent assignment (index), with the? Position one by one corresponds to.
If there are many parameters, it is convenient to assign a value, it is easy to error.
Workaround: Use named parameters: Name:

public static void Query (String name) {

Session s = null;
try {
S=hibernateutil.getsession ();
HQL:
The from behind is not the table name, but the object name (class name)//String HQL = "from user as user where user.name=?"; From Object support multi-attitude
String hql = "from user as user where user.name=:name"; Use named parameters instead?
Query query = s.createquery (HQL);
Query.setstring ("name", name);

Universal paging across databases via query
Query.setfirstresult (0);//start getting data from the first number of bars
Query.setmaxresults (100);//How many data are taken together
List<user> list=query.list (); Executquery ();
for (User user:list) {
System.out.print (User.getname ());
}

If you determine that there is at most one piece of data, you can simplify the code by using the method
User u= (user) Query.uniqueresult ();
System.out.print ("Only one piece of data" +u.getname ());
} finally {
if (s!=null) {
S.close ();
}
}
}
Benefits: This does not depend on the location, as long as the name corresponds to. When you use the same parameter multiple times in SQL, you can assign it one time. Code Simplicity
Four, query interface paging
Explanation: Each database is in a different paging mode, hibernate is configured in Hibernate.cfg.xml
<property name= "Hibernate.dialect" >org.hibernate.dialect.OracleDialect</property><!--dialect--- To identify different databases so that the lower level uses the appropriate paging method
Universal paging across databases via query
Query.setfirstresult (0);//start getting data from the first number of bars
Query.setmaxresults (100);//How many data are taken together
Advantage: The two methods in the query interface implement the common paging method, which increases the portability of the program.

Ibernate Learning Note 5---Entity class or attribute name conflicts with database keywords, hql named arguments, HQL implements generic paging

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.