1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
/*
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package akatsuki.domain.repositryimpl;
import akatsuki.domain.model.Produit;
import akatsuki.domain.repositry.ProductDao;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
//import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
*
* @author hamza
*/
public class ProduitDaoHibernate implements ProductDao{
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List<Produit> getProduits() {
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session sessionHibernate) throws HibernateException, SQLException {
Transaction tx= sessionHibernate.beginTransaction();
List<Produit>lst;
lst=new ArrayList<Produit>();
Query req = sessionHibernate.createQuery("select id,nom from Produit");
for(Iterator it = req.iterate();it.hasNext();){
Produit p = new Produit();
Object[]ligne=(Object[])it.next();
p.setId(String.valueOf(ligne[0]));
p.setNom(String.valueOf(ligne[1]));
lst.add(p);
}
tx.commit();
return lst;
}
};
return (List<Produit>)hibernateTemplate.execute(callback);
}
} |
Partager