예제 #1
0
  public FacturaAlmacen actualiza(FacturaAlmacen otraFactura, Usuario usuario)
      throws NoEstaAbiertaException {
    FacturaAlmacen factura =
        (FacturaAlmacen) currentSession().get(FacturaAlmacen.class, otraFactura.getId());
    switch (factura.getEstatus().getNombre()) {
      case Constantes.ABIERTA:
        Session session = currentSession();
        factura.setVersion(otraFactura.getVersion());
        factura.setFecha(otraFactura.getFecha());
        factura.setComentarios(otraFactura.getComentarios());
        factura.setIva(otraFactura.getIva());
        factura.setTotal(otraFactura.getTotal());
        factura.setCliente(otraFactura.getCliente());
        Date fecha = new Date();
        factura.setFechaModificacion(fecha);
        session.update(factura);

        audita(factura, usuario, Constantes.ACTUALIZAR, fecha);

        session.flush();
        return factura;
      default:
        throw new NoEstaAbiertaException("No se puede actualizar una factura que no este abierta");
    }
  }
예제 #2
0
  public void saveEvent(Event event) {
    Event event1 = getEvent(event);

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      if (event1 == null) {
        Transaction transaction = session.beginTransaction();
        try {
          session.save(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      } else {
        Transaction transaction = session.beginTransaction();
        try {
          session.update(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
  }
  @Override
  public void update(ItemConviteInstrutorPerfil itemConvite) throws DAOException {
    Session session = sessionFactory.getCurrentSession();
    try {
      session.update(itemConvite);
    } catch (HibernateException e) {

      Logger.getLogger(HibernateItemConviteInstrutorPerfilDAO.class.getName())
          .log(Level.SEVERE, null, e);
      throw new DAOException(MessageHelper.getMessage("consultas.update.error"));
    }
  }
예제 #4
0
 public void updateObject(Object entity) {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     session.update(entity);
     session.flush();
     transaction.commit();
   } finally {
     if (session.isOpen()) {
       session.disconnect();
       session.close();
     }
   }
 }
예제 #5
0
  @Test
  public void test3Update() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    User user = (User) session.get(User.class, 1);
    user.setName("updated name");
    user.setAge(100);
    session.update(user);

    tx.commit();
    session.close();
  }
예제 #6
0
  private void addPersonToEvent(Long personId, Long eventId) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Person aPerson =
        (Person)
            session
                .createQuery("select p from Person p left join fetch p.events where p.id = :pid")
                .setParameter("pid", personId)
                .uniqueResult(); // Eager fetch the collection so we can use it detached

    Event anEvent = (Event) session.load(Event.class, eventId);
    // If we want to handle it bidirectional and detached, we also need to load this
    // collection with an eager outer-join fetch, this time with Criteria and not HQL:
    /*
    Event anEvent = (Event) session
            .createCriteria(Event.class).setFetchMode("participants", FetchMode.JOIN)
            .add( Expression.eq("id", eventId) )
            .uniqueResult(); // Eager fetch the colleciton so we can use it detached
    */

    session.getTransaction().commit();

    // End of first unit of work

    aPerson.getEvents().add(anEvent); // aPerson is detached
    // or bidirectional safety method, setting both sides: aPerson.addToEvent(anEvent);

    // Begin second unit of work

    Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
    session2.beginTransaction();

    session2.update(aPerson); // Reattachment of aPerson

    session2.getTransaction().commit();
  }
예제 #7
0
  private void tick() throws Exception {
    long time = System.currentTimeMillis();
    for (Session session : new ArrayList<>(this.sessions.values())) {
      session.update(time);
    }

    for (String address : this.ipSec.keySet()) {
      int count = this.ipSec.get(address);
      if (count >= this.packetLimit) {
        this.blockAddress(address);
      }
    }
    this.ipSec.clear();

    if ((this.ticks & 0b1111) == 0) {
      double diff = Math.max(50d, (double) time - this.lastMeasure);
      this.streamOption("bandwidth", this.sendBytes / diff + ";" + this.receiveBytes / diff);
      this.lastMeasure = time;
      this.sendBytes = 0;
      this.receiveBytes = 0;

      if (!this.block.isEmpty()) {
        long now = System.currentTimeMillis();
        for (String address : new ArrayList<>(this.block.keySet())) {
          long timeout = this.block.get(address);
          if (timeout <= now) {
            this.block.remove(address);
            this.getLogger().notice("Unblocked " + address);
          } else {
            break;
          }
        }
      }
    }

    ++this.ticks;
  }
예제 #8
0
  public boolean saveUser(User user) {
    boolean result = false;

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    try {
      session.update(user);
      result = true;
      session.flush();
      transaction.commit();
    } catch (StaleObjectStateException sose) {
      System.out.println("StaleObjectException");
      transaction.rollback();
    } catch (HibernateException e) {
      e.printStackTrace();
      transaction.rollback();
      result = false;
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }
    return result;
  }