Insert Method
Copy codeThe Code is as follows:
Public void insert (Object o) {Session session = HibernateSessionFactory. currentSession (); Transaction t = session. beginTransaction (); session. save (o); t. commit (); HibernateSessionFactory. clossSession ();}
Delete Method
Copy codeThe Code is as follows:
Public void delete (Object o, Serializable id) {Session session = HibernateSessionFactory. currentSession (): Transaction t = session. beginTransaction (); Object o = session. get (o. class, id); if (o! = Null) {session. delete (o);} t. commit (); HibernateSessionFactory. clossSession ();}
Update Method
Copy codeThe Code is as follows:
Public void update (Object o, Serializable id) {Session session = HibernateSessionFactory. currentSession (); Transaction t = session. beginTransaction (); session. update (o, id); t. commit (); HibernateSessionFactory. clossSession ();}
General select method based on HQL
Copy codeThe Code is as follows:
Public ArrayList select (String SQL) {Session session = HibernateSessionFactory. currentSession (); Query query = createQuery (SQL); List list = query. list (); HibernateSessionFactory. clossSession (); return (ArrayList) list ;}
SQL-based general select Method
Copy codeThe Code is as follows:
Public ArrayList select (String SQL) throws Exception {Session session = HibernateSessionFactory. currentSession (); Connection con = session. connection (); PreparedStatement pstmt = con. preparedStatement (SQL); ResultSet rs = pstmt.exe cuteQuery (); ResultSetMetaData rsmd = rs. getMetaData (); Hashtable ht = null; ArrayList array = new ArrayList (); while (rs. next () {ht = new Hashtable (); for (int I = 0; I <rsmd. getColumnCount (); I ++) {ht. put (rsmd. getColumnName (I + 1), rs. getObject (I + 1);} array. add (ht);} HibernateSessionFactory. clossSession (); return array ;}