Gethibernatetemplate (). Find () method

Source: Internet
Author: User
Tags table name

Find (String querystring, object[] values);

The argument for this method must be an array, not a list.

List ul=gethibernatetemplate (). Find ("from User u where u.username=?") And u.password=? ", New String[]{user,pass});

Where user is the corresponding class name, not the table name in the database.

How to access the database. We have two options:

GetSession (). CreateQuery ("from Users");

Gethibernatetemplate (). Find (' from Users ');

with which. Confused AH.

Online looking for information are recommended to use gethibernatetemplate, the reason is not very clear.

The following tests were done: loop-call GetSession () CreateQuery ("from Users"), Gethibernatetemplate (). Find (' from users ');

1000 times Results getsession () Soon the package could not be established to connect. And Gethibernatetemplate can not run out of things. Through the background observation, the use of getsession will leave many sql*net message from client connections in the database, terminating the test after the connection is automatically released. Gethibernatetemplate uses a connection from beginning to end. Does the getsession () not automatically free the connection. I then cycled through the GetSession (). CreateQuery ("from Users"), Gethibernatetemplate (). Find (' from users '); 5 times found that the current end of the program as soon as the end of the GetSession 5 connections immediately released. When the connection is automatically released after terminating the test 1000 times, it can be explained that the getsession () will automatically release the connection.

Conclusion:

(1) getsession () and gethibernatetemplate can automatically release the connection (of course, your configuration to be correct), but in a line agenda getsession will get a lot of sessions (that is, many conversations, connections), is likely to cause the database connection to exceed the upper limit. So it is recommended to use Gethibernatetemplate.

(2) If some statements cannot be implemented with Gethibernatetemplate, you can use the Hibernatecallback callback interface using Gethibernatetemplate.execute. Another: You can set the Hibernatetemplate allowcreate to True and close the session in finally. You can also pass true as an argument to the Super.getsession (..) method to get the session. This can also be a point of trouble.



parameter Bindings

By order placeholder. To populate the parameters:

1) Hibernate 2 is populated by the Session.find method

Session.find ("From TUser user where user.name=?", "Erica", hibernate.string);

Conditions for multiple parameters:

object[] args = new object[] {"Erica", New Integer (20)};
type[] types = new type{hibernate.string, Hibernate.integer};
Session.find ("From TUser user where user.name=?") And user.age=? ", args, types);

2) through the query interface for parameter filling:

Query query = Session.createquery ("From TUser user where user.name=?") and User.age>? ");
Query.setstring (0, "Erica");
Query.setinteger (1, 20);

To populate a parameter by referencing a placeholder:

String hql = "from TUser where Name=:name";
Query query = session.createquery (HQL);
Query.setparameter ("name", "Erica");

How to pass a parameter of two time types to the second argument of find

Public List findall (date begin, date end) throws Exception {
String hql= "from Salebill where Saledate bewteen?" and? ";
Object[] Value={begin,end};
List List = This.gethibernatetemplate (). Find (hql, value);
return list;

List morecats = This.gethibernatetemplate (). Find (
"From Cat as Cat where" + "Cat.name = ' Fritz ' or cat.id =? or cat.id =? ",
New object[] {id1, id2},
New type[] {hibernate.long, hibernate.long}
);

Public Auctionuser Finduserbyitemandprice (Integer itemId, Double)
{
object[] args = {itemId, price};
List L = gethibernatetemplate (). Find ("Bid as Bid where bid.bidItem.id =? and Bid.bidprice =? ", args);
if (L.size () >= 1)
{
Bid B = (Bid) l.get (0);
return B.getbiduser ();
}
Else
{
return null;
}

}

PS: Check online with Gethibernatetemplate (). The Find () method can sometimes cause problems
1. If the database does not have a primary key set, and the data has a null field, the return list.size () is not 0. But the time of get is null 2. The return result of the pass parameter is also null.
Please know the principles of the heroes who are pointing twos

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.