使用@Query和休眠更新spring数据jpa中的布尔值 [英] updating boolean value in spring data jpa using @Query, with hibernate
问题描述
我已经配置并运行了 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);
然后我收到以下错误:
...
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query;
nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
...
所以,我认为我的问题现在是事务问题,然后我回到 google 进行研究,发现您现在必须添加 @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);
但后来我收到以下错误:
but then I got the following error:
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屋!