2013-10-08 2 views
3

В моем приложении Spring MVC (версия Hibernate: 4.1.7.final) У меня есть объект со списком тоскует, как следующий:JPQL запрос - Объект содержит список Longs

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

    ... 
} 

цель состоит в том, чтобы написать запрос для тех Фоос, список пуст или содержит заданное число, что-то вроде:

@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 является то, что целевые объекты не могут быть запрошены, сохранялось, сливались независимо от их родительского объекта. Они являются строго частными (зависимыми) объектами, такими же, как картирование @Embedded. Нет каскадного варианта на @ElementCollection, целевые объекты всегда сохраняются, сливаются, удаляются вместе со своим родителем. @ElementCollection все еще может использовать тип выборки и по умолчанию LAZY аналогичен другим сопоставлениям коллекции.

Смежные вопросы