Mysql 有没有办法使用JPA查询更新集合类型列?

Mysql 有没有办法使用JPA查询更新集合类型列?,mysql,spring,spring-data-jpa,querydsl,Mysql,Spring,Spring Data Jpa,Querydsl,我要做的是通过JPA查询更新表中的字段。 我用set type设置了MySQL表 create table staff ( id BIGINT, ... roles SET('A', 'B', 'C') not null, ... ) 和阶级 public class Staff { private Long id; @Converter(someConverterHere.class) private Set<Role> role

我要做的是通过JPA查询更新表中的字段。 我用set type设置了MySQL表

create table staff (
   id    BIGINT,
...
   roles  SET('A', 'B', 'C')    not null,
...
)
和阶级

public class Staff {
    private Long id;
    @Converter(someConverterHere.class)
    private Set<Role> roles;
}
jpa查询生成器似乎会验证newRoles中的所有单个值,并将其类型与Staff.roles类型进行比较,后者不能为true(Role和Set)

有什么办法解决这个问题吗?我找不到任何解决办法

依赖性

  • org.springframework.data:spring数据jpa:2.3.2.RELEASE
=====================================================

临时解决方案: 我将参数类型从一个集合更改为另一个集合,它可以工作。现在,当使用QueryDsl时,这个问题就成了问题。使用querydsl的“set”,此问题再次发生:(

Querydsl

// newRoles : Set<Role>
// Error: IllegalArgumentException
query.set(QStaff.Staff.roles, newRoles));

// Of course, compile error
query.set(QStaff.Staff.roles, Set.of(newRoles)));
//新角色:设置
//错误:IllegalArgumentException
query.set(QStaff.Staff.roles,newRoles));
//当然,编译错误
query.set(QStaff.Staff.roles,set.of(newRoles));

自定义类型需要由JPA实现中的自定义类型映射。对于Hibernate,这是通过用户类型界面实现的。为了通过QueryDSL使用扩展类型,需要为其创建自定义表达式类型。此外,对于要支持的任何操作(如set contains、equality),都必须注册自定义函数。可以这样做,但不使用常规API。您最好对元模型进行非规范化,并使用
@ElementCollection
。然而,如果你坚持要开始工作,这里有一些建议:

  • 是一个广泛流行的项目,它为Hibernate实现了许多自定义类型。它没有实现SET,但您可能可以使用数组的实现作为基础
  • 是一个为某些自定义类型扩展QueryDSL元模型生成的项目。对于集合,您可能也应该模仿该存储库中的任何内容
Caused by: java.lang.IllegalArgumentException: Parameter value [A] 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:1629)
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1612)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:238)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:154)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 81 common frames omitted
// newRoles : Set<Role>
// Error: IllegalArgumentException
query.set(QStaff.Staff.roles, newRoles));

// Of course, compile error
query.set(QStaff.Staff.roles, Set.of(newRoles)));