簡體   English   中英

JPQL 查詢 - 實體包含多頭列表

[英]JPQL query - Entity contains a list of Longs

在我的 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;

    ...
}

目標是為那些列表為空或包含給定數字的 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("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)

那有什么問題? 我該怎么做?

要實現您想要的查詢類型(在@ElementCollection的內容上),您需要更改為@OneToMany關聯。 這是我的支持(來自JPA:何時選擇多值關聯與元素集合映射

使用@ElementCollection而不是@OneToMany的局限性在於無法獨立於其父對象查詢、持久化和合並目標對象。 它們是嚴格私有(依賴)的對象,與@Embedded映射相同。 @ElementCollection上沒有級聯選項,目標對象始終與其父對象一起持久化、合並、刪除。 @ElementCollection仍然可以使用 fetch 類型,並且與其他集合映射一樣默認為 LAZY。

正如 guido 提到的,這是由於這個錯誤: https : //hibernate.atlassian.net/browse/HHH-6686

我已經試過了,它現在在 5.4.11 版中肯定有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM