gpt4 book ai didi

java - hql 与 @CollectionTable 联接

转载 作者:行者123 更新时间:2023-12-01 14:08:33 29 4
gpt4 key购买 nike

使用带有collection标签的域Service,如下所示:

@Entity
public class Service extends AbstractEntity<Long> {
private static final long serialVersionUID = 9116959642944725990L;

@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection(targetClass = java.lang.String.class)
@CollectionTable(name = "service_tags", joinColumns = @JoinColumn(name = "s_id"))
@Column(name = "tag")
@IndexColumn(name = "idx", base = 1)
private List<String> tags;
}

我想获取带有特定标签服务

我的总部是

@Query("select s from Service s join s.tags t where s.status=?1 and (t['tag'] IN (?3) or s.serviceType IN (?2)))
List<Service> getAllServicesByStatusTypeOrTags(ServiceStatus status, List<ServiceType> type, List<String> tags, TransactorType transactor);

但是出现以下异常:

Caused by: java.lang.IllegalArgumentException: java.lang.UnsupportedOperationException
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:73) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.fromQueryAnnotation(SimpleJpaQuery.java:132) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:114) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:160) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280) ~[spring-data-commons-core-1.4.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148) ~[spring-data-commons-core-1.4.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125) ~[spring-data-commons-core-1.4.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41) ~[spring-data-commons-core-1.4.0.RELEASE.jar:na]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
... 44 common frames omitted
Caused by: java.lang.UnsupportedOperationException: null
at org.hibernate.hql.ast.tree.IdentNode.resolveIndex(IdentNode.java:66) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.tree.IndexNode.resolve(IndexNode.java:94) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.HqlSqlWalker.processIndex(HqlSqlWalker.java:986) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4807) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1326) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4471) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4258) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2047) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1997) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1975) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1972) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1972) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:831) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:617) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770) ~[hibernate-core-3.6.6.Final.jar:3.6.6.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272) ~[hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]
at sun.reflect.GeneratedMethodAccessor24.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_43]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_43]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at com.sun.proxy.$Proxy43.createQuery(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor24.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_43]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_43]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at com.sun.proxy.$Proxy43.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:69) ~[spring-data-jpa-1.2.0.RELEASE.jar:na]
... 53 common frames omitted

最佳答案

我切换回Set<String>

@Entity
public class Service extends AbstractEntity<Long> {
private static final long serialVersionUID = 9116959642944725990L;
@ElementCollection(fetch = FetchType.EAGER, targetClass = java.lang.String.class)
@CollectionTable(name = "service_tags", joinColumns = @JoinColumn(name = "s_id"))
@Column(name = "tag")
private Set<String> tags;
}

下面的代码有效。

@Query("select s from Service s join s.tags t where s.status=?1 and (s.id IN (select st.id from Service st inner join st.tags tag where tag IN (?3)) or s.serviceType IN (?2)))
List<Service> getAllServicesByStatusTypeOrTags(ServiceStatus status, List<ServiceType> type, List<String> tags, TransactorType transactor);

关于java - hql 与 @CollectionTable 联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717479/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com