我无法获取此表来更新不兼容类型错误的bc。user表试图保存一组属性,但是我的sql语法似乎抛出了一个错误,导致这组属性被分解为单独的属性。我有两个类,User和Attributes,中间有一个多对多的表。
@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<>();
}用户类
@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)发布于 2020-05-27 17:04:02
由于您只发布了异常消息,而没有发布堆栈跟踪,因此我不确定异常到底是关于什么的。但这根本不会起作用。
您正在尝试执行JPQL UPDATE语句来设置集合。这不受支持。JPQL UPDATE在updated子句中只支持单值属性:
请参阅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语句,从而有效地执行更新。当然,这会稍微复杂一些,因为您必须确定插入哪些行,删除哪些行,以及更新哪些行。
https://stackoverflow.com/questions/62034215
复制相似问题