http://www.cnblogs.com/rockniu/archive/2009/10/26/1590254.html
package dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import util.SessionUtil;
import entity.Product;
public class ProductDaoImp {
Session session=SessionUtil.getSession();
public int delProduct(Product product){
Session session=SessionUtil.getSession();
int row=0;
try {
session.beginTransaction();
String hql = "delete from Product where productId="
+ product.getProductId();
Query query = session.createQuery(hql);
row = query.executeUpdate();
session.getTransaction().commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
public List<Product> findAll(){
List<Product>products = null;
String hql="from Product";
Session session=null;
try {
session = SessionUtil.getSession();
Query query=session.createQuery(hql);
products=query.list();
} catch (Exception e) {
e.printStackTrace();
}
return products;
}
public Product findOne(Product product){
String hql="from Product p where productId="+product.getProductId();
try {
Query query= session.createQuery(hql);
product =(Product)query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
}
return product;
}
public void findProductById(int id){
String hql="select p.productName from Product p where productId=?";
String product=null;
try {
Query query=session.createQuery(hql);
query.setInteger(0, id);
product=(String) query.uniqueResult();
System.out.println(product);
} catch (Exception e) {
// TODO: handle exception
}
}
public Object[] findproductPicdangPriceById(Product p){
Object[]obj=null;
String hql="select p.productName,p.dangPrice from Product p where p.productId=:productId";
try {
Query query=session.createQuery(hql);
query.setParameter("productId", p.getProductId());
obj=(Object[])query.uniqueResult();
} catch (Exception e) {
// TODO: handle exception
}
return obj;
}
public List<Product>findSomeNameLike(String str){
List<Product>list=null;
String hql="from Product p where p.productName like ?";
Query query=session.createQuery(hql);
query.setParameter(0, "%"+str+"%");
list=(List<Product>)query.list();
return list;
}
public Object newOne(){
String hql="select new Product(p.productId,p.productName,p.fixedPrice)" +
"from Product p where productId=7";
Query query=session.createQuery(hql);
Object obj=query.uniqueResult();
return obj;
}
public Product getOne(Product product) {
product=(Product) session.get(Product.class, product.getProductId());
return product;
}
public Product getOneByQbc(){
Product product=null;
Criteria criteria=session.createCriteria(Product.class);
criteria.add(Restrictions.eq("productId", 9));// 向qbc语句加入属性
product=(Product)criteria.uniqueResult();
return product;
}
public Product getDouble(){
Product p=new Product();
p.setProductId(10);
Product product =(Product)session.get(Product.class,10);
session.evict(product);//逐出以及缓存中的product
return product;
}
//对sql 支持
public Product getOneByQbcSql(){
String sql="";
SQLQuery sq=session.createSQLQuery(sql);
sq.addEntity(Product.class);
Product pd=(Product)sq.uniqueResult();
return pd;
}
}