我在启用Spring-Boot JPA的项目(All Groovy Code)中有以下相关的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语句似乎没有Character表的别名或者出现的列名:
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而不是in,具有相同的行为:
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 .=?
非常感谢任何帮助。
答案 0 :(得分:1)
您的查询无效 - 正如@Neil Stockton指出的那样,通过编写h.appearsIn in (:episodes)
,您正在说&#34;集合中的集合&#34;没有意义。
你应该声明一个&#34;集合成员变量&#34;像这样:
select distinct h
from Human h
join h.appearsIn ai
where ai in (:episodes)
ai
表示appearsIn
的单个元素(就像迭代器一样)。