spring 是否可以将实体集合传递给IN和NOT IN子句?

nqwrtyyt  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(78)

根据Spring Data JPA文档(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation),可以将实体集合传递给INNOT IN子句。
x1c 0d1x的数据
Age是一个实体。然而,在实践中,这似乎是不可能的。
克隆GitHub repo,或者只是复制并粘贴下面的实体来复制。下面是一个示例:

import jakarta.persistence.*;

@Entity
@Table(name = "specialties")
public class Specialty {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "name")
    private String name;
}
import jakarta.persistence.*;

import java.util.Set;

@Entity
@Table(name = "vets")
public class Vet {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "vet_specialties",
            joinColumns = @JoinColumn(name = "vet_id"),
            inverseJoinColumns = @JoinColumn(name = "specialty_id")
    )
    private Set<Specialty> specialties;

    public void addSpecialty(Specialty specialty) {
        this.specialties.add(specialty);
    }
}
import com.example.demo.entity.Specialty;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SpecialtyRepository extends JpaRepository<Specialty, Long> {
}

字符串
关键时刻:

import com.example.demo.entity.Specialty;
import com.example.demo.entity.Vet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Collection;
import java.util.List;

public interface VetRepository extends JpaRepository<Vet, Long> {

    @Query("select v from Vet v where v.specialties in ?1")
    List<Vet> findBySpecialtiesIn(Collection<Specialty> specialties);

}


如你所见,我正试着按专业来接兽医。
下面是对它的测试:

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private final SpecialtyRepository specialtyRepository;
    private final VetRepository vetRepository;

    public DemoApplication(SpecialtyRepository specialtyRepository,
                           VetRepository vetRepository) {
        this.specialtyRepository = specialtyRepository;
        this.vetRepository = vetRepository;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        Set<Specialty> specialties = new HashSet<>();
        specialties.add(new Specialty());
        specialties.add(new Specialty());
        specialties.add(new Specialty());
        specialties.add(new Specialty());

        specialtyRepository.saveAll(specialties);

        Vet vet = new Vet();

        specialties.forEach(vet::addSpecialty);

        vetRepository.save(vet);

        vetRepository.findBySpecialtiesIn(specialties);
    }
}


我得到以下错误:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:770) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:751) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.2.jar:3.0.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.2.jar:3.0.2]
    at com.example.demo.DemoApplication.main(DemoApplication.java:23) ~[main/:na]
Caused by: org.hibernate.NotYetImplementedFor6Exception: org.hibernate.query.sqm.sql.internal.PluralValuedSimplePathInterpretation
    at org.hibernate.query.sqm.sql.internal.PluralValuedSimplePathInterpretation.accept(PluralValuedSimplePathInterpretation.java:73) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitInListPredicate(AbstractSqlAstTranslator.java:5189) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.tree.predicate.InListPredicate.accept(InListPredicate.java:66) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitWhereClause(AbstractSqlAstTranslator.java:1650) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitQuerySpec(AbstractSqlAstTranslator.java:1596) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.dialect.PostgreSQLSqlAstTranslator.visitQuerySpec(PostgreSQLSqlAstTranslator.java:103) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.tree.select.QuerySpec.accept(QuerySpec.java:129) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitSelectStatement(AbstractSqlAstTranslator.java:834) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.translateSelect(AbstractSqlAstTranslator.java:707) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.translate(AbstractSqlAstTranslator.java:645) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:407) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:300) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at org.hibernate.query.Query.getResultList(Query.java:94) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:401) ~[spring-orm-6.0.4.jar:6.0.4]
    at jdk.proxy2/jdk.proxy2.$Proxy101.getResultList(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:127) ~[spring-data-jpa-3.0.1.jar:3.0.1]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90) ~[spring-data-jpa-3.0.1.jar:3.0.1]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:148) ~[spring-data-jpa-3.0.1.jar:3.0.1]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:136) ~[spring-data-jpa-3.0.1.jar:3.0.1]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136) ~[spring-data-commons-3.0.1.jar:3.0.1]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120) ~[spring-data-commons-3.0.1.jar:3.0.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-3.0.1.jar:3.0.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) ~[spring-data-commons-3.0.1.jar:3.0.1]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:77) ~[spring-data-commons-3.0.1.jar:3.0.1]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-6.0.4.jar:6.0.4]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-6.0.4.jar:6.0.4]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-6.0.4.jar:6.0.4]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-6.0.4.jar:6.0.4]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:134) ~[spring-data-jpa-3.0.1.jar:3.0.1]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.4.jar:6.0.4]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:218) ~[spring-aop-6.0.4.jar:6.0.4]
    at jdk.proxy2/jdk.proxy2.$Proxy97.findBySpecialtiesIn(Unknown Source) ~[na:na]
    at com.example.demo.DemoApplication.run(DemoApplication.java:42) ~[main/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:767) ~[spring-boot-3.0.2.jar:3.0.2]
    ... 5 common frames omitted


我的问题很简单。这只是文档中的错别字,还是我做错了什么?

xxslljrj

xxslljrj1#

你不明白对吧,如何使用INNOT IN你不能用它来ManyToMany关系:要使用INNOT IN,你应该有这样的东西:


的数据



**回答你的问题是:你做错了什么

相关问题