使用@Query 和休眠更新 spring 数据 jpa 中的布尔值 [英] updating boolean value in spring data jpa using @Query, with hibernate

查看:20
本文介绍了使用@Query 和休眠更新 spring 数据 jpa 中的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经配置并运行了 spring-data 和 hibernate.我可以使用 spring-data 保存记录,但由于某种原因,我无法运行将更新表中所有布尔字段的查询.

I have spring-data and hibernate configured and running. I can save records using spring-data but for some reason I am not able to run query that will update all Boolean fields in a table.

我试过了:

@Query("update Content v set v.published = false where v.division = :division and v.section = :section")
void unPublishContent(@Param("division") String division, 
                         @Param("section") String section);

我也试过这个:

 @Query("update Content v set v.published = 0 where v.division = :division and v.section = :section")
void unPublishContent(@Param("division") String division, 
                         @Param("section") String section);

参数划分和部分实现,但表上没有变化.

Parameters division and section are coming true but no change on the table.

附言我也在用mysql数据库.

p.s. I am also using mysql database.

推荐答案

我使用的是 Spring 3.1 和 Spring JPA Data.我遇到了类似的问题.尝试更新 1 个查询中的多条记录时,我不断收到错误消息.

I'm using Spring 3.1 and Spring JPA Data. I was having a similar problem. I was constantly getting an error while trying to update multiple records in 1 query.

所以,我有这样的事情.

So, I had something like this.

@Query("UPDATE User u SET u.state = ?1 WHERE u.server.id = ?2")
public void updateAllUsers(long state, long serverid);

错误:

org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations

所以,在谷歌搜索了一段时间后,我发现你必须添加@Modifying.

So, after googling for a while, I found out that you had to add @Modifying.

@Modifying  
@Query("UPDATE User u SET u.state = ?1 WHERE u.server.id = ?2")
public void updateAllUsers(long state, long serverid);

但后来我收到以下错误:

But then I was getting the following error:

...    
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; 
nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
...

所以,我想我的问题现在是一个事务问题,我回到谷歌研究它,发现你现在必须添加@Transactional.看来@Modifying 也需要@Transactional.

So, I figured my problem was now a transaction problem and I went back to google to research it and found out that you have to add @Transactional now. It appears that @Modifying also requires @Transactional.

@Modifying  
@Transactional
@Query("UPDATE User u SET u.state = ?1 WHERE u.server.id = ?2")
public void updateAllUsers(long state, long serverid);

但后来出现以下错误:

No value for key [org.apache.commons.dbcp.BasicDataSource (...) ] bound to thread

我又用谷歌搜索了一段时间,得出的结论是我的配置是错误的,结果是正确的.我缺少一些 xml 配置.

Again I googled for a while and came to the conclusion that my configuration was wrong and it turned out to be true. I was missing some xml configs.

<beans:bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <beans:property name="entityManagerFactory" ref="entityManagerFactory"/>
</beans:bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

这是一段漫长的旅程,但我终于成功了.我希望这会对某人有所帮助,因为许多其他人通过他们精彩的博客、答案和评论帮助了我.

It was long journey but I finally got it working. I hope this will help someone, trying to "pay it forward" as many others have helped me with their wonderful blogs, answers and comments.

这篇关于使用@Query 和休眠更新 spring 数据 jpa 中的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆