2013-10-08 84 views
3

名單在我的Spring MVC應用程序(休眠版本:4.1.7.final)我有多頭列表的實體,就像下面的一個:JPQL查詢 - 實體包含多頭

@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; 

    ... 
} 

的目標是編寫那些FOOS該列表爲空或包含給定數量的查詢,像這樣:

@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) 

這是怎麼回事?我怎樣才能做到這一點?

+1

看起來很像這樣:https://hibernate.atlassian.net/browse/HHH-6686 –

回答

2

要實現您想要的查詢類型(關於@ElementCollection的內容),您需要更改爲@OneToMany關聯。 這是我的支持(來自JPA: When to choose Multivalued Association vs. Element Collection Mapping

使用的侷限性的@ElementCollection代替@OneToMany是目標對象不能被查詢,依然存在,獨立於它們的父對象的合併。它們是嚴格私有的(從屬)對象,與映射相同。在@ElementCollection上沒有級聯選項,目標對象總是被持久化,合併,與父級一起移除。 @ElementCollection仍然可以使用獲取類型,並且默認爲LAZY與其他集合映射相同。