Common hql query methods in spring (gethibernatetemplate ())

Source: Internet
Author: User
1. Find (string querystring );

Example: This. gethibernatetemplate (). Find ("from bean. User ");

Returns all user objects.

2. Find (string querystring, object value );

Example: This. gethibernatetemplate (). Find ("from bean. User u where u. Name = ?", "Test ");

Or fuzzy query: This. gethibernatetemplate (). Find ("from bean. User u where u. name like ?","% Test %");

Returns the object whose name attribute value is test (fuzzy query returns the object whose name attribute value contains test)

3. Find (string querystring, object [] values );

Example: String hql = "from bean. User u where u. Name =? And U. Password = ?"

This. gethibernatetemplate (). Find (hql, new string [] {"test", "123 ″});

Returns all user objects whose username is test and whose password is 123.

---------------------------------

Iv. findbyexample (Object exampleentity)

Example:

User u = new user ();

U. setpassword ("123"); // conditions that must be met, but they are tied in parallel (like and in SQL)

U. setname ("BB ");

List = This. gethibernatetemplate (). findbyexample (u, start, max );

Return: the user name is BB and the password is 123.

V. findbyexample (Object exampleentity, int firstresult, int maxresults)

Example:

User u = new user ();

U. setpassword ("123"); // conditions that must be met, but they are tied in parallel (like and in SQL)

U. setname ("BB ");

List = This. gethibernatetemplate (). findbyexample (u, start, max );

Return: if the user name is BB and the password is 123, a total of Max user objects are generated from start. (Object count starts from 0)

-----------------

6. findbynamedparam (string querystring, string paramname, object value)

Use the following statement to query:

String querystring = "select count (*) from bean. User u where u. Name =: Myname";

String paramname = "myname ";

String value = "xiyue ";

This. gethibernatetemplate (). findbynamedparam (querystring, paramname, value );

System. Out. println (list. Get (0 ));

Returns the number of user objects whose name is xiyue.

VII. findbynamedparam (string querystring, string [] paramname, object [] value)

Example:

String querystring = "select count (*) from bean. User u where u. Name =: myname and U. Password =: mypassword ";

String [] paramname = new string [] {"myname", "mypassword "};

String [] value = new string [] {"xiyue", "123 ″};

This. gethibernatetemplate (). findbynamedparam (querystring, paramname, value );

Returns the user object whose username is xiyue and whose password is 123.

VIII. findbynamedquery (string queryname)

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name = "queryalluser"> <! -The name of the query to be called->

<! [CDATA [

From bean. User

]>

</Query>

</Hibernate-mapping>

2. Use the following query:

This. gethibernatetemplate (). findbynamedquery ("queryalluser ");

9. findbynamedquery (string queryname, object value)

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name ="Querybyname"> <! -The name of the query to be called->

<! [CDATA [

From bean. User u where u. Name =?

]>

</Query>

</Hibernate-mapping>

2. Use the following query:

This. gethibernatetemplate ().Findbynamedquery("Querybyname","Test");

10. findbynamedquery (string queryname, object [] value)

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name = "querybynameandpassword"> <! -The name of the query to be called->

<! [CDATA [

From bean. User u where u. Name =? And U. Password =?

]>

</Query>

</Hibernate-mapping>

2. Use the following query:

String [] values = new string [] {"test", "123 ″};

This. gethibernatetemplate (). findbynamedquery ("querybynameandpassword", values );

11. findbynamedqueryandnamedparam (string queryname, string paramname, object value)

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name = "querybyname"> <! -The name of the query to be called->

<! [CDATA [

From bean. User u where u. Name =: myname

]>

</Query>

</Hibernate-mapping>

2. Use the following query:

This. gethibernatetemplate (). findbynamedquery ("querybyname", "myname", "test ");

12. findbynamedqueryandnamedparam (string queryname, string [] paramname, object [] value)

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name = "querybynameandpassword"> <! -The name of the query to be called->

<! [CDATA [

From bean. User u where u. Name =: myname and U. Password =: mypassword

]>

</Query>

</Hibernate-mapping>

2. Use the following query:

String [] names = new string [] {"myname", "mypassword "};

String [] values = new string [] {"test", "123 ″};

This. gethibernatetemplate (). findbynamedquery ("querybynameandpassword", names, values );

13. findbyvaluebean (string querystring, object value );

Example:

1. Define a valuebean. The attribute name must be the same as the variable name following the hsql statement. Here, there must be at least two attributes: myname and mypassword. After setting the attribute value using the setter method

Valuebean = new valuebean ();

Valuebean. setmyname ("test ");

Valuebean. setmypasswrod ("123 ″);

2,

String querystring = "from bean. User u where u. Name =: myname and U. Password =: mypassword ";

This. gethibernatetemplate (). findbyvaluebean (querystring, valuebean );

14. findbynamedqueryandvaluebean (string queryname, object value );

Example:

1. First, you must define the name query in user. HBM. xml.

<Hibernate-mapping>

<Class> ...... </Class>

<Query name ="Querybynameandpassword"> <! -The name of the query to be called->

<! [CDATA [

From bean. User u where u. Name =: myname and U. Password =: mypassword

]>

</Query>

</Hibernate-mapping>

2. Define a valuebean with the attribute name and user. HBM. in the XML Naming query statement: the variable name is the same as the variable name. There must be at least two attributes, myname and mypassword, respectively.

Valuebean = new valuebean ();

Valuebean. setmyname ("test ");

Valuebean. setmypasswrod ("123 ″);

3,

String querystring = "from bean. User u where u. Name =: myname and U. Password =: mypassword ";

This. gethibernatetemplate (). findbynamedqueryandvaluebean ("Querybynameandpassword", Valuebean );

 

From http://holoblog.iteye.com/blog/1245768

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.