2016-01-15 51 views
0

我有一個彈簧引導JPA啓用以下項目的相關JPA註釋類(所有Groovy代碼):JPA查詢失蹤的別名和列(休眠)

@Entity 
abstract class Character { 
    @Id 
    String id; 

    String name; 

    @ElementCollection(targetClass = Episode) 
    @Enumerated(EnumType.ORDINAL) 
    Collection<Episode> appearsIn; 
} 

@Entity(name = "Human") 
public class Human extends Character { 
    String homePlanet; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "favorite_droid_id") 
    Droid favoriteDroid; 
} 

public enum Episode { 
    PHANTOM_MENACE, 
    ATTACK_OF_THE_CLONES, 
    REVENGE_OF_THE_SITH, 
    A_NEW_HOPE, 
    EMPIRE_STRIKES_BACK, 
    RETURN_OF_THE_JEDI, 
    THE_FORCE_AWAKENS 
} 

當我嘗試執行以下JPA查詢:

def query = em.createQuery("from Human h where h.appearsIn in (:episodes)"); 
query.setParameter("episodes", EnumSet.of(Episode.THE_FORCE_AWAKENS)); 
def result = query.getResultList(); 

生成的SQL語句似乎並不具備別名字符表或appears_in列名:

select human0_.id as id2_0_, human0_.name as name3_0_, human0_.favorite_droid_id as favorite6_0_, human0_.home_planet as home_pla5_0_ 
from character human0_ 
cross join character_appears_in appearsin1_ 
where human0_.dtype='Human' and human0_.id=appearsin1_.character_id and (. in (?)) 

我也嘗試過使用equals代替,具有相同的行爲:

from Human h where h.appearsIn = :episode 

產生如下SQL:

select human0_.id as id2_0_, human0_.name as name3_0_, human0_.favorite_droid_id as favorite6_0_, human0_.home_planet as home_pla5_0_ 
from character human0_ 
cross join character_appears_in appearsin1_ 
where human0_.dtype='Human' and human0_.id=appearsin1_.character_id and .=? 

任何幫助是極大的讚賞。

+1

你不能做「收集IN集合」。無效的JPQL。我本來期望JPA提供商給出這樣的消息 –

+0

你可以使用setParameterList進行收集IN –

+0

什麼「setParameterList」? –

回答

1

通過編寫h.appearsIn in (:episodes),您的查詢無效 - 正如@Neil Stockton指出的那樣,您所說的「collection in collection」中沒有任何意義。

您應而聲明一個「集合的成員變量」是這樣的:

select distinct h 
from Human h 
join h.appearsIn ai 
where ai in (:episodes) 

ai表示appearsIn單個元件(如一個迭代)。