import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Persisted_Item {
private static Persisted_Item instance = null;
private Session session;
private Persisted_Item() {
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection(
"jdbc:mysql://stolas69.servequake.com/items", "sepi3", "1qazse4");
// Hibernate configuration
Configuration cfg = new Configuration();
// cfg.addResource("Item.hbm.xml");
cfg.addClass(Item.class).setProperty("hibernate.dialect",
"org.hibernate.dialect.MySQLInnoDBDialect");
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
cfg.setProperty("show_sql", "true");
SessionFactory factory = cfg.buildSessionFactory();
session = factory.openSession(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace(System.err);
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
public static Persisted_Item getInstance() {
if (instance == null) {
instance = new Persisted_Item();
}
return instance;
}
public String insertItem(Item item) throws Exception {
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(item);
// tx.commit();
session.flush();
tx.commit();
// session.close();
return item.getId();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
}
}
public void changePrice(String id, double price) throws Exception {
if (this.idExists(id)) {
Transaction tx = null;
try {
tx = session.beginTransaction();
Item item = (Item) session.load(Item.class, id);
item.setPrice(price);
session.update(item);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
}
}
}
public void changeDescription(String id, String description)
throws Exception {
if (this.idExists(id)) {
Transaction tx = null;
try {
tx = session.beginTransaction();
Item item = (Item) session.load(Item.class, id);
item.setDescription(description);
session.update(item);
tx.commit();
System.out.println("object changed");
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
}
}
}
public void changeLocation(String id, String location) throws Exception {
if (this.idExists(id)) {
Transaction tx = null;
try {
tx = session.beginTransaction();
Item item = (Item) session.load(Item.class, id);
item.setLocation(location);
session.update(item);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
}
}
}
public Item getItem(String id) {
Item item = (Item) session.load(Item.class, id);
return item;
}
public Set<Item> getAllItems() {
Iterator<Item> i = this.getIterator();
Set<Item> set = new HashSet<Item>();
while (i.hasNext()) {
Item item = i.next();
set.add(item);
}
return set;
}
@SuppressWarnings("unchecked")
public Iterator<Item> getIterator() {
String s = "from Item";
Query q = session.createQuery(s);
List<Item> allItems = q.list();
return allItems.iterator();
}
@SuppressWarnings("unchecked")
public List<Item> searchProductByName(String description) {
String s = "from i in class Item where i.description=" + "'"
+ description + "'";
Query q = session.createQuery(s);
return q.list();
}
public boolean idExists(String id) {
if (session.get(Item.class, id) != null)
return true;
return false;
}
public void deleteItem(String id) {
Transaction tx = null;
tx = session.beginTransaction();
Object item = session.load(Item.class, id);
session.delete(item);
session.flush();
tx.commit();
}
}