如果网址为Service
,则collection
标记如下:
@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;
}
我希望Service
获得某些tag
。
我的hql是
@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
答案 0 :(得分:1)
我切换回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);