Java Code Examples for javax.persistence.Query#executeUpdate()

The following examples show how to use javax.persistence.Query#executeUpdate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateQueryImpl.java    From zstack with Apache License 2.0 6 votes vote down vote up
@Transactional
private int _hardDelete() {
    DebugUtils.Assert(entityClass!=null, "entity class cannot be null");

    StringBuilder sb = new StringBuilder(String.format("DELETE FROM %s vo", entityClass.getSimpleName()));

    String where = where();
    if (where != null) {
        sb.append(String.format(" WHERE %s", where));
    }

    String sql = sb.toString();
    if (logger.isTraceEnabled()) {
        logger.trace(sql);
    }

    Query q = dbf.getEntityManager().createQuery(sql);

    if (where != null) {
        fillConditions(q);
    }

    int ret = q.executeUpdate();
    dbf.getEntityManager().flush();
    return ret;
}
 
Example 2
Source File: TestJPQLDelete.java    From HibernateTips with MIT License 6 votes vote down vote up
@Test
public void deleteBooks() {
	log.info("... deleteBooks ...");

	EntityManager em = emf.createEntityManager();
	em.getTransaction().begin();

	logBooks(em);
	
	Query query = em.createQuery("DELETE Book b");
	query.executeUpdate();

	logBooks(em);
	
	em.getTransaction().commit();
	em.close();
}
 
Example 3
Source File: TopicServiceBean.java    From bbs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 删除话题
 * @param topicId 话题Id
 * @return
 */
public Integer deleteTopic(Long topicId){
	int i = 0;
	Query delete = em.createQuery("delete from Topic o where o.id=?1")
	.setParameter(1, topicId);
	i = delete.executeUpdate();
	
	//删除评论
	Query comment_delete = em.createQuery("delete from Comment o where o.topicId=?1");
	comment_delete.setParameter(1, topicId);
	comment_delete.executeUpdate();
	
	//删除评论回复
	Query delete_reply = em.createQuery("delete from Reply o where o.topicId=?1");
	delete_reply.setParameter(1, topicId);
	delete_reply.executeUpdate();
			
	//删除提醒
	remindService.deleteRemindByTopicId(topicId);
	
	//删除收藏
	favoriteService.deleteFavoriteByTopicId(topicId);
	
	return i;
}
 
Example 4
Source File: ResetEjb.java    From testing_security_development_enterprise_systems with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void deleteEntities(Class<?> entity){

        if(entity == null || entity.getAnnotation(Entity.class) == null){
            throw new IllegalArgumentException("Invalid non-entity class");
        }

        String name = entity.getSimpleName();

        /*
            Note: we passed as input a Class<?> instead of a String to
            avoid SQL injection. However, being here just test code, it should
            not be a problem. But, as a good habit, always be paranoiac about
            security, above all when you have code that can delete the whole
            database...
         */

        Query query = em.createQuery("delete from " + name);
        query.executeUpdate();
    }
 
Example 5
Source File: TestJPQLUpdate.java    From HibernateTips with MIT License 6 votes vote down vote up
@Test
public void updateBookPrices() {
	log.info("... updateBookPrices ...");

	EntityManager em = emf.createEntityManager();
	em.getTransaction().begin();

	logBookPrices(em);
	
	Query query = em.createQuery("UPDATE Book b SET b.price = b.price*1.1");
	query.executeUpdate();

	logBookPrices(em);
	
	em.getTransaction().commit();
	em.close();
}
 
Example 6
Source File: AbstractJPADAO.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * If the key is null the delete action is skipped.
 *
 * @param key
 */
@Override
public void delete(K key) {
    if (key == null) {
        return;
    }

    Query query = entityManager.createQuery("DELETE FROM " + getEntityClass().getName() + " o WHERE o.id = :id");
    query.setParameter("id", key);
    query.executeUpdate();
}
 
Example 7
Source File: TopicServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 增加总评论数
 * @param topicId 话题Id
 * @param quantity 数量
 * @return
 */
public int addCommentTotal(Long topicId,Long quantity){
	Query query = em.createQuery("update Topic o set o.commentTotal=o.commentTotal+?1 where o.id=?2")
			.setParameter(1, quantity)
			.setParameter(2, topicId);
	return query.executeUpdate();
}
 
Example 8
Source File: AnswerServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 删除答案
 * @param questionId 问题Id
 * @param answerId 答案Id
 * @param cancelAdoptionUserName 取消采纳用户名称
 * @param cancelAdoptionPointLogObject 取消采纳用户退还悬赏积分日志
 * @param cancelAdoptionUserNameShareAmount 取消采纳用户退还分成金额
 * @param cancelAdoptionPaymentLogObject 取消采纳用户退还悬赏金额日志
 * @param point 扣减用户积分
 * @return
*/
public Integer deleteAnswer(Long questionId,Long answerId,String cancelAdoptionUserName,Object cancelAdoptionPointLogObject,BigDecimal cancelAdoptionUserNameShareAmount,Object cancelAdoptionPaymentLogObject,Long point){
	//删除答案
	Query delete = em.createQuery("delete from Answer o where o.id=?1");
	delete.setParameter(1, answerId);
	int i = delete.executeUpdate();
	if(i >0){
		//悬赏退还
		this.rewardReturn(cancelAdoptionUserName,cancelAdoptionPointLogObject,cancelAdoptionUserNameShareAmount,cancelAdoptionPaymentLogObject,point);
		
		if(cancelAdoptionUserName != null){
			//修改问题的采纳答案Id
			questionService.updateAdoptionAnswerId(questionId, 0L);
			
			//删除平台分成
			platformShareService.deleteQuestionRewardPlatformShare(questionId, cancelAdoptionUserName);
		}
		
		
	}
	
	
	//删除答案回复
	Query delete_reply = em.createQuery("delete from AnswerReply o where o.answerId=?1");
	delete_reply.setParameter(1, answerId);
	delete_reply.executeUpdate();
	
	questionService.subtractAnswerTotal(questionId,1L);
	return i;
}
 
Example 9
Source File: BaseDao.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
/**
 * 执行HQL语句,一般为delete、update类型
 * @param hql
 */
public void executeHQL(String hql, Object...params){
    Query query = em.createQuery(hql);
    if(params != null && params.length > 0) {
        for(int i = 0; i < params.length; i++){
            query.setParameter(i + 1, params[i]); // 从1开始,非0
        }
    }
    
    query.executeUpdate();
    em.flush();
    em.clear();        
}
 
Example 10
Source File: UserServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 修改用户头像
 * @param userName 用户名称
 * @param avatarName 头像名称
 */
public Integer updateUserAvatar(String userName,String avatarName){
	Query query = em.createQuery("update User o set " +
			" o.avatarName=?1, o.userVersion=o.userVersion+1 where o.userName=?2")
	.setParameter(1, avatarName)
	.setParameter(2, userName);
	return query.executeUpdate();
}
 
Example 11
Source File: SubscriptionDAO.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
public void removeAllSubscriptions(DocumentRevision pDocR) {
    Query query = em.createQuery("DELETE FROM StateChangeSubscription s WHERE s.observedDocumentRevision = :docR");
    query.setParameter("docR", pDocR);
    query.executeUpdate();

    Query query2 = em.createQuery("DELETE FROM IterationChangeSubscription s WHERE s.observedDocumentRevision = :docR");
    query2.setParameter("docR", pDocR);
    query2.executeUpdate();
}
 
Example 12
Source File: LinksServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 修改友情链接
 * @param links
 * @return
 */
@CacheEvict(value="findAllLinks_cache",allEntries=true)
public Integer updateLinks(Links links){
	Query query = em.createQuery("update Links o set o.name=?1,o.website=?2, o.sort=?3,o.image=?4 where o.id=?5")
		.setParameter(1, links.getName())
		.setParameter(2, links.getWebsite())
		.setParameter(3, links.getSort())
		.setParameter(4, links.getImage())
		.setParameter(5, links.getId());
		int i = query.executeUpdate();
	return i;
}
 
Example 13
Source File: SystemNotifyServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 删除系统通知
 * @param systemNotifyId 系统通知Id
 */
public Integer deleteSystemNotify(Long systemNotifyId){
	int i = 0;
	Query delete = em.createQuery("delete from SystemNotify o where o.id=?1")
	.setParameter(1,systemNotifyId);
	i = delete.executeUpdate();
	
	if(i >0){
		//删除订阅系统通知
		this.deleteSubscriptionSystemNotify(systemNotifyId);
	}
	
	return i;
}
 
Example 14
Source File: UserRoleServiceBean.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 删除用户角色
 * @param userRoleId 用户角色Id
 */
@CacheEvict(value="userRoleServiceBean_cache",allEntries=true)
public int deleteUserRole(String userRoleId){
	Query delete = em.createQuery("delete from UserRole o where o.id=?1")
		.setParameter(1, userRoleId);
	return delete.executeUpdate();
}
 
Example 15
Source File: JPAGroupDAO.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public void clearADynMembers(final Group group) {
    Query delete = entityManager().createNativeQuery("DELETE FROM " + ADYNMEMB_TABLE + " WHERE group_id=?");
    delete.setParameter(1, group.getKey());
    delete.executeUpdate();
}
 
Example 16
Source File: UserServiceBean.java    From bbs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 修改用户
 * @param user 用户
 * @param add_userInputValue 添加注册功能项用户输入值集合
 * @param delete_userInputValueIdList 删除注册功能项用户输入值Id集合
 * @param userRoleGroupList 用户角色组集合
 */
public Integer updateUser(User user, List<UserInputValue> add_userInputValue,List<Long> delete_userInputValueIdList, List<UserRoleGroup> userRoleGroupList){

	Query query = em.createQuery("update User o set " +
			" o.nickname=?1, o.allowUserDynamic=?2, o.password=?3,o.email=?4, o.issue=?5," +
			" o.answer=?6,  o.state=?7, " +
			" o.remarks=?8,o.mobile=?9,o.realNameAuthentication=?10,o.securityDigest=?11, o.userVersion=o.userVersion+1 where o.id=?12 and o.userVersion=?13")
	.setParameter(1, user.getNickname())//呢称
	.setParameter(2, user.getAllowUserDynamic())//允许显示用户动态 
	.setParameter(3, user.getPassword())//密码
	.setParameter(4, user.getEmail())//Email地址
	.setParameter(5, user.getIssue())//密码提示问题
	.setParameter(6, user.getAnswer())//密码提示答案
	.setParameter(7, user.getState())//用户状态
	.setParameter(8, user.getRemarks())//备注
	.setParameter(9, user.getMobile())//手机
	.setParameter(10, user.isRealNameAuthentication())//实名认证
	.setParameter(11, user.getSecurityDigest())//安全摘要
	.setParameter(12, user.getId())//Id
	.setParameter(13, user.getUserVersion());//版本号
	
	
	
	int i = query.executeUpdate();
	
	if(add_userInputValue != null && add_userInputValue.size() >0){	
		for(UserInputValue userInputValue : add_userInputValue){
			userInputValue.setUserId(user.getId());
			this.save(userInputValue);
		}
	}
	
	if(delete_userInputValueIdList != null && delete_userInputValueIdList.size() >0){
		Query delete = em.createQuery("delete from UserInputValue o where o.id in(:id)")
				.setParameter("id", delete_userInputValueIdList);
				delete.executeUpdate();
	}
	
	userRoleService.updateUserRoleGroup(user.getUserName(), userRoleGroupList);
	
	
	return i;
}
 
Example 17
Source File: DatabasesTest.java    From joynr with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {

    System.out.println("Tear down");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Query deleteBounceProxiesQuery = em.createQuery("DELETE FROM BounceProxyEntity");
    int deletedRows = deleteBounceProxiesQuery.executeUpdate();
    System.out.println("Deleted " + deletedRows + " bounce proxies");

    Query deleteBounceProxyInformationsQuery = em.createQuery("DELETE FROM BounceProxyInformationEntity");
    deletedRows = deleteBounceProxyInformationsQuery.executeUpdate();
    System.out.println("Deleted " + deletedRows + " bounce proxy informations");

    Query deleteChannelsQuery = em.createQuery("DELETE FROM ChannelEntity");
    deletedRows = deleteChannelsQuery.executeUpdate();

    System.out.println("Deleted " + deletedRows + " channels");

    tx.commit();
}
 
Example 18
Source File: UserRoleServiceBean.java    From bbs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 删除用户角色组
 * @param userNameList 用户名称集合
 */
public int deleteUserRoleGroup(List<String> userNameList){
	Query delete = em.createQuery("delete from UserRoleGroup o where o.userName in(:userName)")
		.setParameter("userName", userNameList);
	return delete.executeUpdate();
}
 
Example 19
Source File: ActionRowDeleteAll.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
private <T extends JpaObject> Integer delete(Business business, Class<T> cls, List<String> ids) throws Exception {
	EntityManager em = business.entityManagerContainer().get(cls);
	Query query = em.createQuery("delete from " + cls.getName() + " o where o.id in :ids");
	query.setParameter("ids", ids);
	return query.executeUpdate();
}
 
Example 20
Source File: TopicIndexServiceBean.java    From bbs with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * 删除所有话题索引变化标记
 */
public Integer deleteAllIndex(){
	Query query = em.createNativeQuery("truncate table topicindex");
	
	return query.executeUpdate();
	
}