Java JPQL查询-实体包含一个long列表

Java JPQL查询-实体包含一个long列表,java,mysql,hibernate,jpql,Java,Mysql,Hibernate,Jpql,在我的Spring MVC应用程序(Hibernate版本:4.1.7.final)中,我有一个实体,其中有一个long列表,如下所示: @Entity @Table(name = "foo") public class Foo { @Id private Long id; @ElementCollection @CollectionTable( name = "foo_numbers", joinColumns

在我的Spring MVC应用程序(Hibernate版本:4.1.7.final)中,我有一个实体,其中有一个long列表,如下所示:

@Entity
@Table(name = "foo")
public class Foo {
    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(
            name = "foo_numbers",
            joinColumns = {@JoinColumn(
                    name = "foo_id",
                    referencedColumnName = "id")})
    private Collection<Long> numbers;

    ...
}
@实体
@表(name=“foo”)
公开课Foo{
@身份证
私人长id;
@元素集合
@收集表(
name=“foo_编号”,
joinColumns={@JoinColumn(
name=“foo_id”,
referencedColumnName=“id”)})
私人收藏号码;
...
}
目标是为列表为空或包含给定数字的foo编写查询,如:

@Query("SELECT f FROM Foo AS f WHERE f.numbers IS EMPTY OR (:num) MEMBER OF f.numbers")
Collection<Foo> findTheRightFoos(@Param("num") Long num);
@Query(“从Foo中选择f作为f,其中f.numbers为空或f.numbers的(:num)成员”)
集合findTheRightFoos(@Param(“num”)Long num);
但我遇到了以下问题:

@Query("SELECT f FROM Foo AS f WHERE (:num) MEMBER OF f.numbers")
Collection<Foo> findFoos_1(@Param("num") Long num);
//org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
//Hibernate log: select fooba0_.id as id9_ from foo fooba0_ cross join foo_numbers numbers1_ where fooba0_.id=numbers1_.foo and (? in (.))

@Query("SELECT f FROM Foo AS f WHERE (:num) IN f.numbers")
Collection<Foo> findFoos_2(@Param("num") Long num);
//Can't start the app, got the following exception:
//antlr.NoViableAltException: unexpected end of subtree ...
//... Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [SELECT c FROM com.acme.Foo AS f WHERE (:num) IN f.numbers]

@Query("SELECT f FROM Foo AS f WHERE f.numbers = (:num)")
Collection<Foo> findFoos_3(@Param("num") Long num);
//org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [3870] did not match expected type [java.util.Collection]; nested exception is java.lang.IllegalArgumentException: Parameter value [3870] did not match expected type [java.util.Collection]

@Query("SELECT f FROM Foo AS f WHERE f.numbers IS EMPTY")
Collection<Foo> findFoos_4();
//Can't start the app, got the following exception:
//antlr.NoViableAltException: unexpected end of subtree ...
//... Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [SELECT c FROM com.acme.Foo AS f WHERE f.numbers IS EMPTY]

@Query("SELECT f FROM Foo AS f WHERE f.numbers IS NULL")
Collection<Foo> findFoos_5();
//org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null)' at line 1; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null)' at line 1
//Hibernate log: select fooba0_.id as id9_ from foo fooba0_ cross join foo_numbers numbers1_ where fooba0_.id=numbers1_.foo and (. is null)

@Query("SELECT f FROM Foo AS f WHERE f.numbers = NULL")
Collection<Foo> findFoos_6();
//org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null)' at line 1; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null)' at line 1
//Hibernate log same as 'findFoos_5': select fooba0_.id as id9_ from foo fooba0_ cross join foo_numbers numbers1_ where fooba0_.id=numbers1_.foo and (. is null)
@Query(“从Foo中选择f作为f,其中(:num)是f.numbers的成员”)
集合findFoos_1(@Param(“num”)Long num);
//org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以获取第1行中使用的正确语法(接近“))”;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以获取第1行中使用的正确语法(接近“))”
//休眠日志:从foo fooba0\uu交叉连接foo\u numbers numbers1\uu中选择fooba0\u0.id作为id9\uo,其中fooba0\u0.id=numbers1\u0.foo和(?in(.))
@查询(“从Foo中选择f作为f,其中(:num)在f.numbers中”)
收集findFoos_2(@Param(“num”)Long num);
//无法启动应用程序,出现以下异常:
//antlr.NoviableException:子树意外结束。。。
//... 原因:org.hibernate.hql.internal.ast.QuerySyntaxException:子树意外结束[从com.acme.Foo中选择c作为f,其中(:num)在f.numbers中]
@查询(“从Foo中选择f作为f,其中f.numbers=(:num)”)
收集findFoos_3(@Param(“num”)Long num);
//org.springframework.dao.InvalidDataAccessApiUsageException:参数值[3870]与预期类型[java.util.Collection]不匹配;嵌套异常为java.lang.IllegalArgumentException:参数值[3870]与预期类型[java.util.Collection]不匹配
@查询(“从Foo中选择f作为f,其中f.number为空”)
收集findFoos_4();
//无法启动应用程序,出现以下异常:
//antlr.NoviableException:子树意外结束。。。
//... 原因:org.hibernate.hql.internal.ast.QuerySyntaxException:子树意外结束[从com.acme.Foo中选择c作为f,其中f.numbers为空]
@查询(“从Foo中选择f作为f,其中f.number为NULL”)
收集findFoos_5();
//org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解第1行“is null”附近使用的正确语法;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以了解第1行“is null)”附近使用的正确语法
//休眠日志:从foo fooba0\uu交叉连接foo\u numbers numbers1\u0中选择fooba0\u0.id作为id9\u0,其中fooba0\u0.id=numbers1\u0.foo和(.null)
@查询(“从Foo中选择f作为f,其中f.numbers=NULL”)
收集findFoos_6();
//org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解第1行“is null”附近使用的正确语法;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.sqlgrammareexception:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以了解第1行“is null)”附近使用的正确语法
//休眠日志与“findFoos_5”相同:从foo fooba0中选择fooba0_u0.id作为id9_0交叉连接foo_0数字numbers1,其中fooba0_0.id=numbers1_u.foo和(.null)

怎么了?我该怎么做?

要实现您想要的查询类型(关于
@ElementCollection
的内容),您需要更改为
@OneToMany
关联。 这是我的支持(来自)


使用
@ElementCollection
而不是
@OneToMany
的局限性在于,目标对象无法独立于其父对象进行查询、持久化和合并。它们是严格私有(依赖)的对象,与
@Embedded
映射相同。
@ElementCollection
上没有级联选项,目标对象始终与其父对象一起保持、合并和删除
@ElementCollection
仍然可以使用提取类型,并且默认设置为与其他集合映射相同的延迟类型。

正如guido提到的,这是由于以下错误造成的:

我已经试过了,现在在5.4.11版中肯定可以用了,看起来很像这样: