下面列出了org.hibernate.Query#executeUpdate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean updateSingle(int id,String signDetail, int signTimes, String lastSignPhoto){
Session session = getSession();
session.beginTransaction();
String hqlString="update SignInfo set signDetail=:signDetail , signTimes=:signTimes , lastSignPhoto=:lastSignPhoto where signID=:id";
Query query = session.createQuery(hqlString);
query.setString("signDetail", signDetail);
query.setInteger("signTimes", signTimes);
query.setString("lastSignPhoto", lastSignPhoto);
query.setInteger("id", id);
try {
query.executeUpdate();
session.getTransaction().commit();
return true;
} catch (Exception e) {
log.debug("update signInfo fail!! ");
e.printStackTrace();
return false;
}
}
/**
* Update the k8s_pods_containers table with number of Pods&Containers
* @param envId
* @param compName
* @param numOfPods
* @param parentCompName
* @param numOfCont
*/
public static void updatePodsAndContainers(final int envId, String compName, int numOfPods, String parentCompName, int numOfCont){
final java.sql.Date todayDate = new java.sql.Date(Calendar.getInstance().getTimeInMillis());;
final int compId = getComponentId(compName, parentCompName);
if(compId == 0){
logger.info("Component Name = " + compName + "; Parent Component Name = " + parentCompName + "; is not available in the DB");
return;
}
Session session = HibernateConfig.getSessionFactory().getCurrentSession();
Transaction txn = session.beginTransaction();
Query query = session.createQuery(HQLConstants.UPDATE_K8S_PODS_CONT_DETAILS);
query.setInteger("totalPods", numOfPods);
query.setInteger("totalContainers", numOfCont);
query.setLong("compId", compId);
query.setLong("environmentId", envId);
query.setDate("stsDate", todayDate);
query.executeUpdate();
txn.commit();
}
public void createDefaultTimingLocations() {
blockingClearAll();
Session s=HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
// sql to set the name and date
Query query = s.createSQLQuery("INSERT into TIMING_LOCATION (TIMING_LOCATION_ID, TIMING_LOCATION_NAME) values (:id, :name)");
query.setParameter("id", 1);
query.setParameter("name", "Start");
query.executeUpdate();
query.setParameter("id", 2);
query.setParameter("name", "Finish");
query.executeUpdate();
s.getTransaction().commit();
// Thread.dumpStack(); // who called this?
refreshTimingLocationList();
}
/**
* Remove
*/
public static void delete(String user, String token) throws DatabaseException {
log.debug("delete({}, {})", user, token);
String qs = "delete from LockToken lt where lt.user=:user and lt.token=:token";
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
tx = session.beginTransaction();
Query q = session.createQuery(qs);
q.setString("user", user);
q.setString("token", token);
q.executeUpdate();
HibernateUtil.commit(tx);
} catch (HibernateException e) {
HibernateUtil.rollback(tx);
throw new DatabaseException(e.getMessage(), e);
} finally {
HibernateUtil.close(session);
}
log.debug("delete: void");
}
@Transactional
public int revertAll(final String locale) {
Query query = sessionFactory.getCurrentSession().createQuery("update MessageBundleProperty set value = null where locale = :locale");
query.setString("locale", locale);
try {
return query.executeUpdate();
} catch (Exception e) {
log.warn("Cound not revert all MessageBundleProperty's " + e.getMessage(), e);
return 0;
}
}
@SuppressWarnings("unchecked")
public int freshCacheToDB(Ehcache cache) {
List<Integer> keys = cache.getKeys();
if (keys.size() <= 0) {
return 0;
}
Element e;
Integer views;
int i = 0;
String hql = "update ContentCount bean"
+ " set bean.views=bean.views+:views"
+ ",bean.viewsMonth=bean.viewsMonth+:views"
+ ",bean.viewsWeek=bean.viewsWeek+:views"
+ ",bean.viewsDay=bean.viewsDay+:views" + " where bean.id=:id";
Query query = getSession().createQuery(hql);
for (Integer id : keys) {
e = cache.get(id);
if (e != null) {
views = (Integer) e.getValue();
if (views != null) {
query.setParameter("views", views);
query.setParameter("id", id);
i += query.executeUpdate();
}
}
}
return i;
}
public boolean markMediaForConversion(final List<Long> mediaIds) {
final HibernateCallback<Integer> hcb = session -> {
Query q = session.createQuery("UPDATE MediaData SET location = 'CONVERTING' WHERE id in (:ids)");
q.setParameterList("ids", mediaIds);
return q.executeUpdate();
};
return getHibernateTemplate().execute(hcb).equals(mediaIds.size());
}
private static void truncateAllTables() {
Query q = session.createSQLQuery("truncate ip_range");
q.executeUpdate();
q = session.createSQLQuery("truncate ip_range");
q.executeUpdate();
q = session.createSQLQuery("truncate additional_item");
q.executeUpdate();
q = session.createSQLQuery("truncate asn");
q.executeUpdate();
q = session.createSQLQuery("truncate domain_name");
q.executeUpdate();
q = session.createSQLQuery("truncate entry_type");
q.executeUpdate();
q = session.createSQLQuery("truncate free_text");
q.executeUpdate();
q = session.createSQLQuery("truncate ip_range");
q.executeUpdate();
q = session.createSQLQuery("truncate job");
q.executeUpdate();
q = session.createSQLQuery("truncate job_type");
q.executeUpdate();
q = session.createSQLQuery("truncate log_entry");
q.executeUpdate();
q = session.createSQLQuery("truncate mail_job");
q.executeUpdate();
q = session.createSQLQuery("truncate mail_job_log_entry_mapping");
q.executeUpdate();
q = session.createSQLQuery("truncate organization");
q.executeUpdate();
q = session.createSQLQuery("truncate original_log_entry");
q.executeUpdate();
q = session.createSQLQuery("truncate prio");
q.executeUpdate();
}
/**
* Resets the passed context's default channel
*
*/
protected void resetPlacementDefaultChannel(String context, String placement) {
Session session = null;
try {
session = getSessionFactory().getCurrentSession();
Query query = session.createSQLQuery("update CHAT2_CHANNEL c set c.placementDefaultChannel = :channel, c.PLACEMENT_ID = NULL WHERE c.context = :context and c.PLACEMENT_ID = :placement");
query.setBoolean("channel", false);
query.setString("context", context);
query.setString("placement", placement);
query.executeUpdate();
} catch(Exception e) {
log.warn(e.getMessage());
}
}
/**
* Update the tps_service table with latest tps values.
*
*/
public static void updateTpsService(final int envId, int componentId, float tpsVaule, float latencyValue){
Session session = HibernateConfig.getSessionFactory().getCurrentSession();
Transaction txn = session.beginTransaction();
Query query = session.createQuery(HQLConstants.UPDATE_TPS_SERVICE_DETAILS);
query.setFloat("tpsVaule", tpsVaule);
query.setFloat("latencyValue", latencyValue);
query.setTimestamp("lastUpdateDate", new java.sql.Timestamp(System.currentTimeMillis()));
query.setLong("compId", componentId);
query.setLong("environmentId", envId);
query.executeUpdate();
txn.commit();
}
public boolean markMediaForConversion(final List<Long> mediaIds) {
final HibernateCallback<Integer> hcb = session -> {
Query q = session.createQuery("UPDATE MediaData SET location = 'CONVERTING' WHERE id in (:ids)");
q.setParameterList("ids", mediaIds);
return q.executeUpdate();
};
return getHibernateTemplate().execute(hcb).equals(mediaIds.size());
}
@Retryable
@Transactional(propagation = Propagation.REQUIRES_NEW)
public int updateEventsByIds(Set<Long> ids, NotificationEvent.Status status) {
Query query = genericDao.getNamedQuery(NotificationEvent.UPDATE_EVENT_STATUS_BY_IDS);
query.setParameterList(NotificationEvent.IDS_PARAM, ids);
query.setParameter(NotificationEvent.EVENT_STATUS_PARAM, status);
return query.executeUpdate();
}
public void deleteHost(String hostName, int footprintId) {
String hql = "delete from Host where name = :hostName and footprint.id = :footprintId";
Query query = HibernateUtil.getSession().createQuery(hql);
query.setParameter("hostName", hostName);
query.setParameter("footprintId", footprintId);
query.executeUpdate();
}
public void deleteResourceableId(Long resourceableId) {
Query query = genericDao.getNamedQuery(Course.DELETE_RESOURCEABLE_ID);
query.setParameter("resId", resourceableId);
query.executeUpdate();
}
public void UpdateByHql(String hql, Object[] args) {
Query query = getSession().createQuery(hql);
setParameter(query, args);
query.executeUpdate();
}
public void deleteByLecturerIds(List<Long> lecturerIds) {
Query query = genericDao.getNamedQuery(LecturerCourse.DELETE_LECTURERS_BY_LECTURER_IDS);
query.setParameterList("lecturerIds", lecturerIds);
query.executeUpdate();
}
/**
* Erase low functionality.
*
* @param aLowFunctionality
* the a low functionality
* @param profile
* the profile
*
* @throws EMFUserError
* the EMF user error
*
* @see it.eng.spagobi.analiticalmodel.functionalitytree.dao.ILowFunctionalityDAO#eraseLowFunctionality(it.eng.spagobi.analiticalmodel.functionalitytree.bo.LowFunctionality,
* it.eng.spago.security.IEngUserProfile)
*/
@Override
public void eraseLowFunctionality(LowFunctionality aLowFunctionality, IEngUserProfile profile) throws EMFUserError {
logger.debug("IN");
Session aSession = null;
Transaction tx = null;
try {
if (hasChild(aLowFunctionality.getId())) {
HashMap params = new HashMap();
params.put(PAGE, "BIObjectsPage");
// params.put(SpagoBIConstants.ACTOR,
// SpagoBIConstants.ADMIN_ACTOR);
params.put(SpagoBIConstants.OPERATION, SpagoBIConstants.FUNCTIONALITIES_OPERATION);
throw new EMFUserError(EMFErrorSeverity.ERROR, 1000, new Vector(), params);
}
aSession = getSession();
tx = aSession.beginTransaction();
SbiFunctions hibFunct = (SbiFunctions) aSession.load(SbiFunctions.class, aLowFunctionality.getId());
Set oldRoles = hibFunct.getSbiFuncRoles();
Iterator iterOldRoles = oldRoles.iterator();
while (iterOldRoles.hasNext()) {
SbiFuncRole role = (SbiFuncRole) iterOldRoles.next();
aSession.delete(role);
}
// update prog column in other functions
// String hqlUpdateProg =
// "update SbiFunctions s set s.prog = (s.prog - 1) where s.prog > "
// + hibFunct.getProg() + " and s.parentFunct.functId = " +
// hibFunct.getParentFunct().getFunctId();
if (hibFunct.getParentFunct() != null) {
String hqlUpdateProg = "update SbiFunctions s set s.prog = (s.prog - 1) where s.prog > ? " + " and s.parentFunct.functId = ?";
Query query = aSession.createQuery(hqlUpdateProg);
query.setInteger(0, hibFunct.getProg().intValue());
query.setInteger(1, hibFunct.getParentFunct().getFunctId().intValue());
query.executeUpdate();
}
aSession.delete(hibFunct);
tx.commit();
} catch (HibernateException he) {
logger.error("HibernateException", he);
if (tx != null)
tx.rollback();
throw new EMFUserError(EMFErrorSeverity.ERROR, 100);
} catch (EMFUserError emfue) {
if (tx != null)
tx.rollback();
throw emfue;
} catch (Exception e) {
logException(e);
if (tx != null)
tx.rollback();
throw new EMFUserError(EMFErrorSeverity.ERROR, 100);
} finally {
if (aSession != null)
if (aSession != null) {
if (aSession.isOpen()) {
aSession.close();
logger.debug("The [eraseLowFunctionality] occurs. LowFunctionality cache will be cleaned.");
this.clearCache();
}
logger.debug("OUT");
}
}
}
public void disableSynchronization(Long courseId) {
Query query = genericDao.getNamedQuery(Course.DISABLE_SYNCHRONIZATION);
query.setParameter("courseId", courseId);
query.executeUpdate();
}
/**
* 通过sql更新记录
*
* @param <T>
* @param query
* @return
*/
public int updateBySqlString(final String query) {
Query querys = getSession().createSQLQuery(query);
return querys.executeUpdate();
}
/**
* 执行HQL语句操作更新
*
* @param hql
* @return
*/
public Integer executeHql(String hql) {
Query q = getSession().createQuery(hql);
return q.executeUpdate();
}