3

私の 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)

それの何がいけないの?どうやってやるの?

4

2 に答える 2

2

( の内容に対して) 希望する種類のクエリを実現するには@ElementCollection、関連付けに変更する必要があります@OneToMany 。これが私のサポートです(JPAから:Multivalued Association vs. Element Collection Mappingを選択する場合

@ElementCollectiona の代わりにan を使用することの制限は@OneToMany、親オブジェクトとは無関係にターゲット オブジェクトをクエリ、永続化、マージできないことです。@Embeddedこれらは、マッピングと同じように、厳密に個人所有 (依存) のオブジェクトです。にはカスケード オプションはありません@ElementCollection。ターゲット オブジェクトは常に永続化され、マージされ、親と一緒に削除されます。@ElementCollectionフェッチ タイプを引き続き使用でき、他のコレクション マッピングと同じようにデフォルトで LAZY に設定されます。

于 2014-02-27T15:21:19.283 に答える