/**
* Add
*/
public void Save (Stu Stu) {
try {
Tran=this. GetSession (). BeginTransaction ();
This. GetSession (). Save (Stu);
Tran.commit ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
}
/**
* Full Query using HQL
*/
Public List getallbyhql () {
List Arr=null;
try {
String hql= "from Stu";
Query Query=this. GetSession (). CreateQuery (HQL);
Arr=query.list ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return arr;
}
/**
* Query based on PRIMARY key
*/
Public Stu GetbyID (int id) {
Stu Stu=null;
try {
Stu= (Stu) this. GetSession (). Get (Stu.class, id);
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return Stu;
}
/**
* Query based on object properties (using query)
*/
Public List getbypropertyquery (String name) {
List Arr=null;
try {
This cannot be the same as the SQL SELECT * from Stu where sname=:name, which is not right.
Query Query=this. GetSession (). CreateQuery ("from Stu where Sname=:name");
Query.setstring ("name", name);
Or
Query Query=this. GetSession (). CreateQuery ("From Stu where sname=?");
Query.setstring (0, name);
Arr=query.list ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return arr;
}
/**
* Query based on object properties (use criteria)
*/
Public List Getbypropertycriteria (String name) {
List Arr=null;
try {
Criteria Cri=this. GetSession (). Createcriteria (Stu.class);
Criterion C1=expression.eq ("SName", name);
Cri.add (C1);
Arr=cri.list ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return arr;
}
/**
* Query Section Properties
*/
Public list GetProperty () {
List arr=new ArrayList ();
try {
String hql= "Select S.S Name,s.ssex from Stu as S ";
Query Query=this. GetSession (). CreateQuery (HQL);
List list=query.list ();
Iterator iter=list.iterator ();
while (Iter.hasnext ()) {
object[] obj= (object[]) iter.next ();
Stu s=new Stu ();
S.setsname (obj[0].tostring ());
S.setssex (obj[1].tostring ());
Arr.add (s);
}
} catch (Hibernateexception e) {
this. CloseSession ();
}
return arr;
}
/**
* Query for a property
*/
Public list Getoneproperty () {
List arr=new ArrayList ();
try {
String hql= " Select S.sname from Stu as S ";
Query Query=this. GetSession (). CreateQuery (HQL);
Iterator iter=query.iterate ();
while (Iter.hasnext ()) {
Object obj= (Object) Iter.next ();
Stu s=new Stu ();
S.setsname (obj.tostring ());
Arr.add (s);
}
} catch (Hibernateexception e) {
this. CloseSession ();
}
return arr;
}
/**
* Queries an object for one property value
*/
Public object Getonlyproprotyvalue (int s_id) {
Object obj=null;
try {
String hql= "Select S.sname from Stu as S where s.sid=?";
Query Query=this. GetSession (). CreateQuery (HQL);
Query.setinteger (0, s_id);
Obj=query.uniqueresult ();
} catch (Hibernateexception e) {
throw e;
} finally{
this. CloseSession ();
}
return obj;
}
/**
* SQL query
*/
Public list Getallbysql () {
list arr=null;
try {
String sql= ' select {c.*} from Stu As C ";
SQLQuery sqlquery=this. GetSession (). createsqlquery (SQL);
Sqlquery.addentity ("C", Stu.class);
Arr=sqlquery.list ();
} catch (Hibernateexception e) {
throw e;
} finally{
this. CloseSession ();
}
return arr;
}
/**
* Query based on object
*/
Public List getallbyobject (Stu s) {
List arr=null;
try {
String hql= ' from Stu as S where s=:stuentity ";
//or
//string hql= "from Stu as S where s.sid=:stuentity";
Query Query=this. GetSession (). CreateQuery (HQL);
Query.setentity ("stuentity", s);
Arr=query.list ();
} catch (Hibernateexception e) {
throw e;
} finally{
this. CloseSession ();
}
return arr;
}
/**
* Fuzzy query
*/
Public List getallquerylike (String name) {
List arr=null;
try {
String hql= ' from Stu As s where S.sname like:name ";
Query Query=this. GetSession (). CreateQuery (HQL);
query.setstring ("name", "%" +name+ "%");
//Cannot
//query.setstring ("name", "'%" +name+ "% '");
Arr=query.list ();
} catch (Hibernateexception e) {
Throw e;
}finally{
this. CloseSession ();
}
return arr;
}
/**
* Statistic function
*/
public int countstu () {
int count=0;
try {
String hql= "SELECT COUNT (*) from Stu"; Query Query=this. GetSession (). CreateQuery (HQL);
count= (Integer) Query.uniqueresult ();
} catch (Hibernateexception e) {
throw e;
} finally{
this. CloseSession ();
}
return count;
}
/**
* Condition Statistics
*/
public int countbywhere (String sex) {
int count=0;
try {
Query Query=this. GetSession (). CreateQuery ("SELECT count (*) from Stu where Ssex=:sex");
Query.setstring ("Sex", sex);
Count= (Integer) Query.uniqueresult ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return count;
}
/**
* Statistical average
*/
public float vagage () {
float vag=0;
try {
Query Query=this. GetSession (). CreateQuery ("Select AVG (SAge) from Stu");
Vag= (Float) Query.uniqueresult ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
Return of the;
}
/**
* Summation function
*/
public int sumage () {
int sum=0;
try {
Query Query=this. GetSession (). CreateQuery ("Select sum (SAge) from Stu");
Sum= (Integer) Query.uniqueresult ();
} catch (Hibernateexception e) {
Throw e;
}finally{
This. CloseSession ();
}
return sum;
}
Transfer from http://hlbng.iteye.com/blog/401548