Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package controller; import hib.session.SklepHibernateUtil; import hib_beans.Produkt; import hib_beans.Uzytkownik; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; /** * * @author HP */ public class HibProduktController { ArrayList<Produkt> produkty = new ArrayList<>(); private Produkt p = new Produkt(); public Produkt getP() { return p; } public void setP(Produkt p) { this.p = p; } public HibProduktController() { } public List<Produkt> getProdukty() { SessionFactory sf = SklepHibernateUtil.getSessionFactory(); Session session = sf.openSession(); produkty = (ArrayList<Produkt>) session.createQuery("from Produkt").list(); session.close(); return produkty; } public String save() { SessionFactory sf = SklepHibernateUtil.getSessionFactory(); Session session = sf.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); SQLQuery insertQuery = session .createSQLQuery("INSERT INTO Produkt(nazwa,cena,liczba_sztuk)VALUES(?,?,?)"); insertQuery.setParameter(0, this.p.getNazwa()); insertQuery.setParameter(1, this.p.getCena()); insertQuery.setParameter(2, this.p.getLiczbaSztuk()); insertQuery.executeUpdate(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } this.p = new Produkt(); return "/faces/admin/produkty.xhtml"; } public String delete(Produkt p) { SessionFactory sf = SklepHibernateUtil.getSessionFactory(); Session session = sf.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); String hql = "DELETE FROM Produkt " + "WHERE id_produktu = :id"; Query query = session.createQuery(hql); query.setParameter("id", p.getIdProduktu()); query.executeUpdate(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } return "/faces/admin/produkty.xhtml"; } public String update(Produkt p) { this.p = p; return "/faces/admin/edit_produkt.xhtml"; } public String update() { SessionFactory sf = SklepHibernateUtil.getSessionFactory(); Session session = sf.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); String hql = "update Produkt set nazwa = :nazwa, cena = :cena, liczba_sztuk = :liczba_sztuk where id_produktu = :id"; Query query = session.createQuery(hql); query.setParameter("id", p.getIdProduktu()); query.setParameter("nazwa", p.getNazwa()); query.setParameter("cena", p.getCena()); query.setParameter("liczba_sztuk", p.getLiczbaSztuk()); // cos zrobic z ta kategoria query.executeUpdate(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } return "/faces/admin/produkty.xhtml"; } }