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

问题描述

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



我试过这个: p>

  @Query(update Content v set v.published = false where v.division =:division and v.section =:section )
void unPublishContent(@Param(division)字符串分割,
@Param(section)String section);

我也试过这个:

<$ p $ (分区,分区,分区和分区);
无效发布内容(@Param(分区 )字符串部分,
@Param(section)字符串部分);

参数分区和部分即将实现,但表格中没有任何变化。



ps我也在使用mysql数据库。

解决方案

我使用Spring 3.1和Spring JPA Data。我遇到了类似的问题。我试图在1个查询中更新多个记录时经常发生错误。



所以,我有这样的东西。

  @Query(UPDATE User u SET u.state =?1 WHERE u.server.id =?2)
public void updateAllUsers(long state,long服务器ID);

错误:

  org.hibernate.hql.QueryExecutionRequestException:不支持DML操作

所以,after我发现你必须添加@Modifying。

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

但是我收到以下错误:

  ... 
嵌套异常是org.springframework.dao.InvalidDataAccessApiUsageException:执行更新/删除查询;
嵌套异常是javax.persistence.TransactionRequiredException:执行更新/删除查询
...

所以,我认为我的问题现在是一个交易问题,我回到谷歌研究它,发现你现在必须添加@Transactional。看起来@Modifying也需要@Transactional。

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

但我得到以下错误:

 绑定到线程


我再次搜索了一下,得出结论:我的配置是错误的,结果是真的。

 < beans:bean class =org.springframework.orm.jpa.JpaTransactionManagerid = transactionManager的 > 
< beans:property name =entityManagerFactoryref =entityManagerFactory/>
< / beans:bean>

< tx:注解驱动的事务管理器=transactionManager/>

这是漫长的旅程,但我终于找到了工作。我希望这会帮助别人,试图付出代价,就像其他人帮助我的美妙的博客,答案和评论一样。


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.

I tried this:

@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);

I also tried this:

 @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.

p.s. I am also using mysql database.

解决方案

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);

Error:

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

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
...

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

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在hibernate中更新spring数据jpa中的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:22
查看更多