package com.xiaopeng; import com.xiaopeng.POJO.XpUserEntity; import com.xiaopeng.POJO.XpUserGroupEntity; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import java.util.LinkedList; import java.util.List; public class DatabaseHelper { private static SessionFactory factory; public DatabaseHelper() { try{ factory = new Configuration().configure().buildSessionFactory(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } } //Add or update group-user-relationships into database public void updateRelationship(LinkedList list){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); for(XpUserGroupEntity relationship:list){ session.saveOrUpdate(relationship); } tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } //Get all users from database public LinkedList getAllUsers(){ Session session = factory.openSession(); Transaction tx = null; LinkedList list = new LinkedList<>(); try{ tx = session.beginTransaction(); List users = session.createQuery("FROM XpUserEntity").list(); list = new LinkedList < XpUserEntity > (users); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } return list; } public void updateGoups(LinkedList userList) { } }