[英]Updating many to many tables with spring data jpa, incompatible types
我无法让此表更新不兼容类型错误的 bc。 用户表尝试保存一组属性,但我的 sql 语法似乎引发了一个错误,导致一组属性被分解为单个属性。 我有这两个类,用户和属性,它们之间有一个多对多的表。
@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<>();
}
用户 Class
@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;
}
我尝试保存到我的存储库,使用下面的这种方法从前端获取一组属性。
@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 交易时,我得到了兼容的类型。 这怎么可能? 我似乎在传递一个集合。
@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);
这引发了错误
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)]
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)
由于您仅发布了异常消息而不是堆栈跟踪,因此我不确定异常到底是关于什么的。 但这根本行不通。
您正在尝试执行 JPQL UPDATE
语句来设置集合。 这是不支持的。 JPQL UPDATE
仅支持更新子句中的单值属性:
请参阅 JPA 规范的第 4.10 节:
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 语句。 这当然会稍微复杂一些,因为您必须确定要插入哪些行、要删除哪些行以及要更新哪些行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.