@SuppressWarnings("unchecked") private boolean tryToCastToCurrentObject( Object entity, Class<T> classToCast, InterceptorOperations interceptorOperations) { try { T t2 = (T) entity; // entity instanceof t /* if (entity instanceof GeoDocument){*/ switch (interceptorOperations.name()) { case "SAVE": saves.add(entity); break; case "DELETE": deletes.add(entity); break; case "INSERT": inserts.add(entity); break; case "UPDATE": updates.add(entity); break; default: return false; } /*}*/ return true; } catch (Exception e) { logger.warn(e.getMessage(), e); return false; } }
public static void main(String[] args) { Session session = null; try { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Contact contact; String SQL_QUERY = "select c.id, c.firstName from Contact c where c.id >= 3"; Query query = session.createQuery(SQL_QUERY); Iterator it = query.iterate(); while (it.hasNext()) { Object[] row = (Object[]) it.next(); System.out.println("------------------------------------------------------"); System.out.println("ID : " + row[0]); System.out.println("First Name : " + row[1]); } System.out.println("Done"); } catch (Exception e) { // tx.rollback(); System.out.println(e.getMessage()); } finally { // session.flush(); session.close(); } }
/* Gets the LoginID ignoring case. */ private Login findByIdOracleInsensitive(org.openiam.idm.srvc.auth.dto.LoginId id) { log.debug("findByIdOracleInsensitive.."); String select = " select /*+ INDEX(IDX_LOGIN_UPPER) */ " + " SERVICE_ID, LOGIN, MANAGED_SYS_ID, IDENTITY_TYPE, CANONICAL_NAME, USER_ID, PASSWORD, " + " PWD_EQUIVALENT_TOKEN, PWD_CHANGED, PWD_EXP, RESET_PWD, FIRST_TIME_LOGIN, IS_LOCKED, STATUS, " + " GRACE_PERIOD, CREATE_DATE, CREATED_BY, CURRENT_LOGIN_HOST, AUTH_FAIL_COUNT, LAST_AUTH_ATTEMPT, " + " LAST_LOGIN, IS_DEFAULT, PWD_CHANGE_COUNT, LAST_LOGIN_IP, PREV_LOGIN, PREV_LOGIN_IP, " + " PSWD_RESET_TOKEN, PSWD_RESET_TOKEN_EXP, LOGIN_ATTR_IN_TARGET_SYS " + " FROM LOGIN " + " WHERE SERVICE_ID = :serviceId AND UPPER(LOGIN) = :login AND MANAGED_SYS_ID = :managedSysId "; log.debug("SQL to get Identity: " + select); Session session = sessionFactory.getCurrentSession(); SQLQuery qry = session.createSQLQuery(select); qry.addEntity(Login.class); qry.setString("serviceId", id.getDomainId()); qry.setString("login", id.getLogin().toUpperCase()); qry.setString("managedSysId", id.getManagedSysId()); try { return (Login) qry.uniqueResult(); } catch (Exception e) { log.error(e.toString()); } return null; }
@Override public void save(Group group) throws TransactionFailException { Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); if (group.getId() == null) { session.persist(group); } else { session.saveOrUpdate(group); } tx.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (!tx.wasCommitted()) { tx.rollback(); session.close(); throw new TransactionFailException(); } session.close(); } }
@Override public void delete(Group group) throws TransactionFailException { Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); session.delete(group); tx.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (!tx.wasCommitted()) { tx.rollback(); session.close(); throw new TransactionFailException(); } session.close(); } }
static { try { sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); } catch (Exception e) { System.out.println("cannot get hibernate factory " + e.getMessage()); throw new RuntimeException("cannot create session factory " + e.getMessage()); } }
public void controller(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { hsession = HibernateUtil.currentSession(); sessione = request.getSession(); tx = hsession.beginTransaction(); String action = request.getParameter("action"); if (action == null) action = "home"; /*if(action.equals("login")) login(request, response);*/ Users u = (Users) sessione.getAttribute("user"); if (u == null) { throw new LoginException(); } else if (u.getTipo().intValue() < 2) { throw new LoginException(); } if (action.equals("home")) accedi(request, response); else if (action.equals("chiudi")) chiudiBilancio(request, response); } catch (LoginException e) { Avviso err = new Avviso(e.getMessage()); request.setAttribute("err", err); nextview = "/./error.jsp"; } catch (Exception e) { Avviso err = new Avviso("Errore indefinito. Ricorda di non usare il tasto REFRESH di Explorer!"); request.setAttribute("err", err); System.out.println(e.getMessage()); nextview = "/./error.jsp"; } finally { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextview); if (dispatcher != null) { response.encodeURL(nextview); dispatcher.forward(request, response); } HibernateUtil.closeSession(); } }
public static void main(String[] args) { Session session = null; try { session = HibernateUtility.getSession(); // Create new instance of Contact and set values in it by reading them from form object System.out.println("Inserting Record"); Transaction tx = session.beginTransaction(); Contact contact1 = new Contact(); contact1.setId(1L); contact1.setFirstName("poornima"); contact1.setLastName("baby"); contact1.setEmail("*****@*****.**"); session.save(contact1); Contact contact2 = new Contact(); contact2.setId(2L); contact2.setFirstName("Raj"); contact2.setLastName("Kapoor"); contact2.setEmail("*****@*****.**"); session.save(contact2); Contact contact3 = new Contact(); contact3.setId(3l); contact3.setFirstName("Boney"); contact3.setLastName("Kapoor"); contact3.setEmail("*****@*****.**"); session.save(contact3); Contact contact4 = new Contact(); contact4.setId(4l); contact4.setFirstName("Raj"); contact4.setLastName("babbar"); contact4.setEmail("*****@*****.**"); session.save(contact4); tx.commit(); System.out.println("Done"); } catch (Exception e) { // tx.rollback(); System.out.println(e.getMessage()); } finally { // Actual contact insertion will happen at this step session.flush(); session.close(); } }
@Override public Group findById(Integer id) { Session session = null; Group group = null; try { session = sessionFactory.openSession(); group = (Group) session.get(Group.class, id); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return group; }
@Override public List<Group> findAllGroups() { Session session = null; List<Group> groups = new ArrayList<>(); try { session = sessionFactory.openSession(); Query query = session.createQuery("SELECT g FROM Groups g"); groups.addAll(query.list()); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return groups; }
public int deleteByPK(int idControlHoras) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); transaction.begin(); Query q = session.createQuery("delete from ControlHoras where idControlHoras = :id"); q.setInteger("id", idControlHoras); int rows = q.executeUpdate(); transaction.commit(); return rows; } catch (Exception e) { System.out.println("Error borrando control de horas..."); e.printStackTrace(); transaction.rollback(); return 0; } finally { session.close(); } }
@Test public void test1Save() { // init data User user = new User(); user.setName("foo"); user.setAge(25); SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { session.save(user); tx.commit(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } finally { session.close(); } }