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 name of the table in the database.
How to access the database. We have two options:
GetSession (). CreateQuery ("from Users");
Gethibernatetemplate (). Find ("from Users");
with which. Confused AH.
Looking for information on the Internet is recommended to use gethibernatetemplate, the reason is not very clear.
The following tests were done: loop calls to GetSession (). CreateQuery ("from Users"), Gethibernatetemplate (). Find ("from users");
1000 results getsession () Soon the package was unable to establish a connection. and gethibernatetemplate not be able to run out. With the background observation, using getsession will leave a lot of sql*net message from the client connection in the database, terminate the test after the connection is automatically released. The gethibernatetemplate uses a connection from beginning to end. GetSession () does not automatically release the connection. So I called GetSession (). CreateQuery ("from Users"), Gethibernatetemplate (). Find ("from users"); 5 times Discover the end of the current end of the program, the getsession of the 5 connection immediately released. After terminating the test with the previous 1000 times, the connection is automatically released, which indicates that the getsession () is automatically released.
Conclusion:
(1) getsession () and gethibernatetemplate can automatically release the connection (although your configuration is correct), but in a line range getsession will get a lot of sessions (that is, open a lot of conversations, connections), Most likely causes database connections to exceed the upper limit. So it is recommended to use Gethibernatetemplate.
(2) If some statements cannot be implemented with Gethibernatetemplate, you can use Gethibernatetemplate.execute to use the Hibernatecallback callback interface. Another: You can set the Hibernatetemplate allowcreate to True and close the session in finally. You can also pass true as a parameter to Super.getsession (..) method to get the session. This is also possible, is the trouble point.
parameter Binding
by sequential placeholders. To populate the parameters:
1) Hibernate 2 is populated with the Session.find method
Session.find ("From TUser user where user.name=?", "Erica", hibernate.string); |
Multiple parameter conditions:
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) parameter padding via the query interface:
Query query = Session.createquery ("From TUser user where user.name=?") and User.age>? "); Query.setstring (0, "Erica"); Query.setinteger (1, 20); |
To populate parameters by referencing placeholders:
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 parameter 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 Price)
{
object[] args = {itemId, price};
List L = gethibernatetemplate (). Find ("from 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 sometimes causes problems
1. If the database does not have a primary key set, and if there are null fields in the data, the returned List.size () is not 0. But when get is null 2. Sometimes it is used. The result of the parameter return is also null.
Please know the principles of the heroes pointing twos