Custom Hibernate Query(SQL) Examples


Custom Hibernate Query Examples;

public static boolean queryStaffLogin(Session openSession,Staff stf)
{
boolean loginResult=false;
try
{
//selecting all information from STAFFS and AUTHORITIES TABLE
//using {s.*} and {a.*}

SQLQuery sqlQry = openSession.createSQLQuery(“”
+”SELECT {s.*} , {a.*} “
+” FROM STAFFS {s}, AUTHORITIES {a}”
+” WHERE {s}.STAFFAUTHORYID = {a}.AUTHORITYID”
+” AND {s}.STAFFAUTHORYID=:pstaffauthid“+ ” AND {s}.STAFFUSERNAME=”
+”:pstfusername AND {s}.STAFFPASSWORD=:pstfpassword“);

sqlQry.addEntity(“s”, Staff.class); //s variable will refer to Staff object
sqlQry.addEntity(“a”, Authority.class); //a varible will refer to Authority object

sqlQry.setLong(“pstaffauthid”, stf.getAuthority().getAuthorityId()); //parameter setting :pstaffauthid
sqlQry.setString(“pstfusername”, stf.getStaffUsername());//parameter setting :pstfusername
sqlQry.setString(“pstfpassword”, stf.getStaffPassword());//parameter setting :pstfpassword

Iterator it = sqlQry.list().iterator();
if(it.hasNext())
{
loginResult = true;
}
}
catch(Exception ex)
{
log.log(Level.SEVERE, “QueryManager : queryntvStaffLogin”, ex);
}
return loginResult;
}

Second example:

public static List<CustomerAddress> queryntvSearchCustomerAddressList(Session openSession, Customer customerIn)
{
 List<CustomerAddress> custAddrList = null;
try
{
SQLQuery sqlQry = openSession.createSQLQuery(“”
+”SELECT {ca.*} FROM CUSTOMERADDRESSES {ca}”
                    +” WHERE {ca}.ADDRESSCUSTOMERID=:acId
                    +””);
sqlQry.addEntity(“ca“, CustomerAddress.class);
sqlQry.setLong(“acId“,customerIn.getCustomerId());

custAddrList = sqlQry.list();
}
catch(Exception ex)
{
log.log(Level.SEVERE, “QueryManager : queryntvSearchCustomerAddressList “,ex);
}
return custAddrList;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s