Example usage for org.hibernate Query setInteger

List of usage examples for org.hibernate Query setInteger

Introduction

In this page you can find the example usage for org.hibernate Query setInteger.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setInteger(String name, int val) 

Source Link

Document

Bind a named int-valued parameter.

Usage

From source file:com.mycompany.desktopinlamninguppgift2.repository.DeveloperRepository.java

public void deleteDeveloper(int developerId) {

    Session session = NewHibernateUtil.getSession();
    session.beginTransaction();//ww w  .jav a2 s  .  co  m

    Query query = session.createQuery("delete from Game where developer = :id");
    query.setInteger("id", developerId);

    query.executeUpdate();

    Query q = session.createQuery("delete from Developer where developerId = :id");
    q.setInteger("id", developerId);

    q.executeUpdate();
    session.getTransaction().commit();
    session.close();
}

From source file:com.mycompany.desktopinlamninguppgift2.repository.GameRepository.java

public List<Game> getAllGames(int developerId) {

    Session session = NewHibernateUtil.getSession();
    session.beginTransaction();// w  ww  . j a v  a 2  s.co  m

    Query q = session.createQuery("from Game where developer = :id");
    q.setInteger("id", developerId);

    List<Game> allGames = q.list();

    return allGames;

}

From source file:com.mycompany.desktopinlamninguppgift2.repository.GameRepository.java

public void updateGame(Game game) {

    Session session = NewHibernateUtil.getSession();
    session.beginTransaction();//ww w. j a va  2  s . c  o m

    Query q = session.createQuery(
            "update Game set gameName = :newName, yearOfRelease = :newYear, genre = :newGenre where gameid = :id");
    q.setParameter("newName", game.getGameName());
    q.setParameter("newYear", game.getYearOfRelease());
    q.setParameter("newGenre", game.getGenre());
    q.setInteger("id", game.getGameId());

    q.executeUpdate();

    session.getTransaction().commit();
    session.close();

}

From source file:com.mycompany.desktopinlamninguppgift2.repository.GameRepository.java

public void deleteGame(int gameId) {

    Session session = NewHibernateUtil.getSession();
    session.beginTransaction();//from   w w  w . j  a  v a2s . c  o m

    Query q = session.createQuery("delete from Game where gameID = :id");
    q.setInteger("id", gameId);
    q.executeUpdate();
    session.getTransaction().commit();
    session.close();
}

From source file:com.mypkg.repository.repoimpl.EmployeeRepositoryImplementation.java

@Override
public void addEmployee(int id, String emp_name, int dept_id) {
    Configuration cf = new Configuration();
    cf.configure("hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session hsession = sf.openSession();
    Transaction tx = hsession.beginTransaction();
    Query query = hsession.createSQLQuery("INSERT INTO EMPLOYEE VALUES (:id,:ename,:deptid)");
    query.setInteger("id", id);
    query.setString("ename", emp_name);
    query.setInteger("deptid", dept_id);
    query.executeUpdate();// w  w  w.  j  av  a2s.  c om
    tx.commit();

}

From source file:com.nec.harvest.service.impl.CashServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override/* www.j  a  v a2  s  .co m*/
public boolean updateCashByOrgCodeAndDate(Session session, User user, JSONCash jSONCash, String monthly)
        throws ServiceException {
    if (user == null) {
        throw new IllegalArgumentException("user object must not be null or empty");
    }
    if (jSONCash == null) {
        throw new IllegalArgumentException("jSONCash must not be null or empty");
    }
    if (monthly == null) {
        throw new IllegalArgumentException("monthly must not be null or empty");
    }

    boolean isUpdated = Boolean.FALSE;
    try {
        Query query = repository.getNamedQuery(session, SqlConstants.SQL_UPDATE_CASH_BY_ORGANIZATION_AND_MONTH);
        query.setDouble("syokenHanbai", jSONCash.getSyokenHanbai());
        query.setDouble("prepaidHanbai", jSONCash.getPrepaidHanbai());
        query.setDouble("sonotaAzukari", jSONCash.getSonotaAzukari());
        query.setDouble("urikakeKaisyu", jSONCash.getUrikakeKaisyu());
        query.setDouble("kabusoku", jSONCash.getKabusoku());
        query.setDouble("soukingaku", jSONCash.getSoukingaku());
        query.setString("rem", jSONCash.getRem());

        // Logged-in user's code
        String userCode = user.getUsrCode();
        query.setString("tanCode", userCode);
        query.setString("APInf2", userCode);
        query.setString("stfCodeU", userCode);

        String version = null;
        try {
            Version productVersion = ProductHelper.getProductInfor();
            if (productVersion != null) {
                version = productVersion.getProjectVersion();
            }
        } catch (IOException ex) {
            // TODO
        }
        query.setString("prdNoU", version);
        query.setInteger("updNo", jSONCash.getUpdNo() + 1);
        query.setString("strCode", user.getOrganization().getStrCode());
        query.setString("getSudo", monthly);
        query.setDate("srDate", jSONCash.getSrDate());

        // Updating the record by using SQL for given changed information
        int numberOfRecordUpdated = query.executeUpdate();
        isUpdated = numberOfRecordUpdated > 0;
    } catch (HibernateException ex) {
        throw new ServiceException("An exception occured while update cash data ", ex);
    }
    return isUpdated;
}

From source file:com.nec.harvest.service.impl.InventoryServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  ww  w .  j a v a 2s .c o  m
public boolean updateByOrgCodeAndMonthlyAndCtgCode(Session session, String strCode, String monthly,
        JSONInventory jsonInventory) throws ServiceException {
    if (StringUtils.isEmpty(strCode)) {
        throw new IllegalArgumentException("Organization must not be null or empty");
    }
    if (StringUtils.isEmpty(monthly)) {
        throw new IllegalArgumentException("Monthly must not be null or empty");
    }
    if (jsonInventory == null) {
        throw new IllegalArgumentException("Inventory must not be null or empty");
    }

    boolean isUpdated = Boolean.FALSE;
    try {
        StringBuilder sqlUpdate = new StringBuilder();
        sqlUpdate.append(" UPDATE Inventory c");
        sqlUpdate.append(" SET c.kingaku=:kingaku, ");
        sqlUpdate.append(
                " c.updNo = :newUpdNo, c.tanCode = :tanCode, c.APInf2 =:apInf2, c.stfCodeU = :stfCodeU, c.prdNoU = :prdNoU, c.timeU = NOW()");
        sqlUpdate.append(
                " WHERE c.pk.organization.strCode = :strCode AND c.pk.getSudo = :getSudo AND c.pk.category.ctgCode = :ctgCode");
        Query query = repository.getQuery(session, sqlUpdate.toString());

        String version = null;
        try {
            Version productVersion = ProductHelper.getProductInfor();
            if (productVersion != null) {
                version = productVersion.getProjectVersion();
            }
        } catch (IOException ex) {
            logger.warn(ex.getMessage(), ex);
        }
        query.setDouble("kingaku", jsonInventory.getKingaku());
        query.setInteger("newUpdNo", jsonInventory.getUpdNo() + 1);

        // 
        User user = AuthenticatedUserDetails.getUserPrincipal();
        query.setString("apInf2", user.getUsrCode());
        query.setString("tanCode", user.getUsrCode());
        query.setString("stfCodeU", user.getUsrCode());
        query.setString("prdNoU", version);
        query.setString("strCode", strCode);
        query.setString("getSudo", monthly);
        query.setString("ctgCode", jsonInventory.getCtgCode());

        int result = query.executeUpdate();
        isUpdated = result > 0;
    } catch (HibernateException ex) {
        throw new ServiceException("An exception occured while update inventory data ", ex);
    }
    return isUpdated;
}

From source file:com.nec.harvest.service.impl.MonthlySalesServiceImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  ww  w .  j av  a  2 s  .c  om
public int findByOrgCodeAndMonth(String strCode, String month, Integer... ugKbns) throws ServiceException {
    if (StringUtils.isEmpty(strCode)) {
        throw new IllegalArgumentException("Organization's code must not be null or empty");
    }
    if (StringUtils.isEmpty(month)) {
        throw new IllegalArgumentException("Month must not be null or empty");
    }

    final Session session = HibernateSessionManager.getSession();
    Transaction tx = null;

    Long result = 0L;
    try {
        tx = session.beginTransaction();
        Integer ugKbn = ugKbns.length > 0 ? ugKbns[0] : 2;
        Query namedQuery = repository.getNamedQuery(session,
                SqlConstants.SQL_FIND_MONTHLY_SALES_BY_ORGANIZATION_AND_MONTH);
        namedQuery.setString("strCode", strCode);
        namedQuery.setString("getSudo", month);
        namedQuery.setInteger("ugKbn", ugKbn);

        result = (Long) namedQuery.uniqueResult();
        tx.commit();
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException(
                "An exception occured while count sales change for organization " + strCode + " month " + month,
                ex);
    } finally {
        HibernateSessionManager.closeSession(session);
    }
    return result.intValue();
}

From source file:com.nec.harvest.service.impl.PurchaseServiceImlp.java

License:Open Source License

@Override
public int updatePurchasesByOrgCodeAndMonth(JSONPurchase[] listPurchase, String usrCode, String monthly,
        String orgCode) throws ServiceException {
    if (ArrayUtils.isEmpty(listPurchase)) {
        throw new IllegalArgumentException("ListPurchase must not be null or empty");
    }//from  w  w  w.j av  a 2s .c om

    final Session session = HibernateSessionManager.getSession();
    Transaction tx = null;

    int isUpdated = 0;
    try {
        String version = null;
        try {
            Version productVersion = ProductHelper.getProductInfor();
            if (productVersion != null) {
                version = productVersion.getProjectVersion();
            }
        } catch (IOException ex) {
            logger.warn(ex.getMessage());
        }

        tx = session.beginTransaction();
        Query query = repository.getNamedQuery(session, SqlConstants.SQL_UPDATE_PURCHASE_DATA);

        Set<String> setFlagConfirm = new HashSet<String>();
        for (JSONPurchase jsonPurchase : listPurchase) {
            setFlagConfirm.add(jsonPurchase.getSrsCode() + ":" + jsonPurchase.getCtgCode() + ":"
                    + jsonPurchase.getWakuNum() + ":" + jsonPurchase.getGnrKbn1());

            //
            if (jsonPurchase.getSrDate() != null) {
                String kingaku = StringUtils.isEmpty(jsonPurchase.getKingaku()) ? "0"
                        : jsonPurchase.getKingaku();
                query.setString("kingaku", kingaku);
                query.setString("gnrKbn1", jsonPurchase.getGnrKbn1());
                query.setString("tanCode", usrCode);
                query.setString("APInf2", usrCode);
                query.setString("stfCodeU", usrCode);
                query.setString("prdNoU", version);
                query.setString("strCode", orgCode);
                query.setString("getSudo", monthly);
                query.setDate("srDate", jsonPurchase.getSrDate());
                query.setInteger("updNo", jsonPurchase.getUpdNo());
                query.setString("srsCode", jsonPurchase.getSrsCode());
                query.setString("ctgCode", jsonPurchase.getCtgCode());
                query.setString("wakuNum", jsonPurchase.getWakuNum());

                // Updating the record by using SQL for given changed information
                int numberOfRecordUpdated = query.executeUpdate();
                if (numberOfRecordUpdated == 0) { // Save Error CM.UPD.M03
                    Criterion criterion = Restrictions.and(
                            Restrictions.eq("pk.vendor.srsCode", jsonPurchase.getSrsCode()),
                            Restrictions.eq("delKbn", Constants.STATUS_ACTIVE),
                            Restrictions.eq("pk.organization.strCode", orgCode),
                            Restrictions.eq("pk.category.ctgCode", jsonPurchase.getCtgCode()),
                            Restrictions.eq("pk.wakuNum", jsonPurchase.getWakuNum()),
                            Restrictions.eq("pk.getSudo", monthly),
                            Restrictions.eq("pk.srDate", jsonPurchase.getSrDate()));
                    Criteria criteria = repository.getCriteria(session, Purchase.class);
                    criteria.add(criterion);

                    String strErrorMsg = null;
                    List<Purchase> purchase = repository.findByCriteria(criteria);
                    if (purchase.size() == 0) {
                        strErrorMsg = "Update fail !!!!!";
                        isUpdated = 2;
                    } else {
                        strErrorMsg = "Concurrent database update error record has been changed or deleted by another user. Please try again from menu screen.";
                        isUpdated = 1;
                    }
                    logger.warn(strErrorMsg);
                    break;
                }
            }
        }

        // Update flag Confirm
        if (isUpdated == 0) {
            query = repository.getNamedQuery(session, SqlConstants.SQL_UPDATE_FLAGCONFIRM_DATA);
            Iterator<String> iterator = setFlagConfirm.iterator();
            while (iterator.hasNext()) {
                String string = (String) iterator.next();
                String[] purchase = string.split(":");
                query.setParameter("gnrKbn1", purchase[3]);
                query.setParameter("srsCode", purchase[0]);
                query.setParameter("ctgCode", purchase[1]);
                query.setParameter("wakuNum", purchase[2]);
                query.setString("strCode", orgCode);
                query.setString("getSudo", monthly);

                int numberOfRecordUpdated = query.executeUpdate();
                if (numberOfRecordUpdated == 0) {
                    isUpdated = 1;
                    break;
                }
            }
        }

        if (isUpdated == 0) {
            tx.commit();
        } else {
            tx.rollback();
        }
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException("Hibernate runtime exception when check data available", ex);
    } finally {
        HibernateSessionManager.closeSession(session);
    }
    return isUpdated;
}

From source file:com.netsteadfast.greenstep.base.dao.BaseDAO.java

License:Apache License

/**
 * for public QueryResult getList... doInHibernate
 * @param query       JPA-Style : from TB_ACCOUNT where account = ?0 
 * @param position   JPA-Style : "0", "1" .....
 * @param params//from   w w  w  . j av a  2 s. c o m
 */
@SuppressWarnings("rawtypes")
private void setQueryParams(Query query, String position, Object params) {
    if (params instanceof java.lang.String) {
        query.setString(position, (java.lang.String) params);
        return;
    }
    if (params instanceof java.lang.Character) {
        query.setCharacter(position, (java.lang.Character) params);
        return;
    }
    if (params instanceof java.lang.Double) {
        query.setDouble(position, (java.lang.Double) params);
        return;
    }
    if (params instanceof java.lang.Byte) {
        query.setByte(position, (java.lang.Byte) params);
        return;
    }
    if (params instanceof java.lang.Integer) {
        query.setInteger(position, (java.lang.Integer) params);
        return;
    }
    if (params instanceof java.lang.Long) {
        query.setLong(position, (java.lang.Long) params);
        return;
    }
    if (params instanceof java.lang.Boolean) {
        query.setBoolean(position, (java.lang.Boolean) params);
        return;
    }
    if (params instanceof java.math.BigDecimal) {
        query.setBigDecimal(position, (java.math.BigDecimal) params);
        return;
    }
    if (params instanceof java.util.Date) {
        query.setDate(position, (java.util.Date) params);
        return;
    }
    if (params instanceof java.util.List) {
        List listParams = (List) params;
        this.setQueryParamsOfList(query, position, listParams);
        return;
    }
}