spring boot JPA IN子句不起作用

时间:2017-03-13 17:06:56

标签: spring-boot

我在CrudRepository中有以下API

public static final String getVendorOrder ="select new com.meha.ps.order.dto.VendorOrder(orderItem.vendorItemId"
        + ",order.orderId"
        + ",order.createTime"
        + ",order.modTime "
        + ",org.orgId"
        + ",org.orgType"
        + ") from Order order inner join order.OrderItem orderItem on order.orderId=orderItem.orderId"
        + " inner join Org org on org.orgId=orderItem.vendorId"
        + " where orderItem.vendorItemId in :vendorItemIds";

@Query(getVendorOrder)
List<VendorOrder> findVendorOrder(@Param("vendorItemIds") List<Integer> vendorItemIds);

在启动时遇到以下错误:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 193 [select new com.meha.ps.order.dto.VendorOrder(orderItem.vendorItemId,order.orderId,order.createTime,order.modTime ,org.orgId,org.orgType) from com.meha.ps.order.entities.Order order inner join order.OrderItem orderItem on order.orderId=orderItem.orderId inner join Org org on org.orgId=orderItem.vendorId where orderItem.vendorItemId in :vendorItemIds]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1679) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at com.sun.proxy.$Proxy114.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86) ~[spring-data-jpa-1.10.6.RELEASE.jar:na]
... 95 common frames omitted

引起:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:第1行附近的订单,第193列[select new com.meha.ps.order.dto.VendorOrder(orderItem.vendorItemId,order.orderId) ,order.createTime,order.modTime,org.orgId,org.orgType)来自com.meha.ps.order.entities.Order命令内连接order.OrderItem orderItem on order.orderId = orderItem.orderId inner join org org on org .orgId = orderItem.vendorId其中orderItem.vendorItemId in:vendorItemIds]     在org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:115)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:76)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)~ [hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894)〜[hibernate-core-5.0.11.Final.jar:5.0.11.Final]     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)〜[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]     ...省略了102个常见帧

1 个答案:

答案 0 :(得分:0)

如果某些实体之间没有直接映射,那么我建议一直使用旧样式连接(查询的其余部分似乎没问题):

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>MMChat</non-jta-data-source>
    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    </properties>
</persistence-unit>