首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用spring data jpa更新多对多表,不兼容的类型

使用spring data jpa更新多对多表,不兼容的类型
EN

Stack Overflow用户
提问于 2020-05-27 09:57:05
回答 1查看 278关注 0票数 1

我无法获取此表来更新不兼容类型错误的bc。user表试图保存一组属性,但是我的sql语法似乎抛出了一个错误,导致这组属性被分解为单独的属性。我有两个类,User和Attributes,中间有一个多对多的表。

代码语言:javascript
复制
@Entity // This tells Hibernate to make a table out of this class
public class Attributes implements Serializable {



    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;



    @ManyToMany(mappedBy = "attributes",fetch = FetchType.LAZY)
    private Set<User> users = new HashSet<>();
}

用户类

代码语言:javascript
复制
@Entity // This tells Hibernate to make a table out of this class
public class User implements UserDetails, Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(name = "users_attributesXXX",
    joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
    inverseJoinColumns = @JoinColumn(name = "attributes_id",referencedColumnName = "id"))
    private Set<Attributes> attributes;
}

我尝试保存到我的存储库,使用下面的方法从前端获取属性集。

代码语言:javascript
复制
    @RequestMapping("/bulk_update")
    public String bulkUpdate(@RequestParam(value = "checkboxName", required = false) String[] checkboxValue, @RequestParam(value = "attributeName", required = false) Set<Attributes> attributeValue, HttpServletRequest request
    ) {

        for (String fun : checkboxValue){
            for ( Attributes fun2 : attributeValue){

                User user_to_update2 = userRepository.findUserById(Integer.parseInt(fun));

                userRepository.updateUserQRAttributes(user_to_update2.getId(), attributeValue);


        }
        }


        return "redirect:/manageusers";
    }

当我执行最后一个sql事务时,我得到了兼容的类型。这怎么可能呢?我似乎是在传一套。

代码语言:javascript
复制
 @Transactional
    @Modifying
    @Query("update User set attributes= :attributes where id = :id") // escape role,
        //mark the role param,
        //mark the role as a val which should be changed by the dependency which executes the query
    int updateUserQRAttributes(@Param("id") Long id, @Param("attributes") Set<Attributes> attributes);

这会抛出以下错误

代码语言:javascript
复制
Parameter value [gov.mycolorado.oauth2.models.Attributes@392defb9] did not match expected type [java.util.Set (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [gov.mycolorado.oauth2.models.Attributes@392defb9] did not match expected type [java.util.Set (n/a)]
代码语言:javascript
复制
21:35:54.290 [http-nio-5000-exec-6] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [gov.mycolorado.oauth2.models.Attributes@1630134d] did not match expected type [java.util.Set (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [gov.mycolorado.oauth2.models.Attributes@1630134d] did not match expected type [java.util.Set (n/a)]] with root cause
java.lang.IllegalArgumentException: Parameter value [gov.mycolorado.oauth2.models.Attributes@1630134d] did not match expected type [java.util.Set (n/a)]
    at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54)
    at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27)
    at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90)
    at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55)
    at org.hibernate.query.internal.QueryParameterBindingsImpl.expandListValuedParameters(QueryParameterBindingsImpl.java:636)
    at org.hibernate.query.internal.AbstractProducedQuery.doExecuteUpdate(AbstractProducedQuery.java:1616)
    at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1594)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:256)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
EN

回答 1

Stack Overflow用户

发布于 2020-05-27 17:04:02

由于您只发布了异常消息,而没有发布堆栈跟踪,因此我不确定异常到底是关于什么的。但这根本不会起作用。

您正在尝试执行JPQL UPDATE语句来设置集合。这不受支持。JPQL UPDATE在updated子句中只支持单值属性:

请参阅JPA规范的4.10节:

代码语言:javascript
复制
update_statement ::= update_clause [where_clause]
update_clause ::= UPDATE entity_name [[AS] identification_variable]
                     SET update_item {, update_item}*
update_item ::= [identification_variable.]{single_valued_embeddable_object_field.}*
                     {state_field | single_valued_object_field} = new_value

您可以加载实体,根据需要设置关系,并刷新更改,例如,通过关闭事务,基本上是正常的JPA工作流程。

或者,您也可以执行SQL语句,从而有效地执行更新。当然,这会稍微复杂一些,因为您必须确定插入哪些行,删除哪些行,以及更新哪些行。

另请参阅:https://stackoverflow.com/a/61956946/66686

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62034215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档