gpt4 book ai didi

java - JPA 查询缺少别名和列(Hibernate)

转载 作者:行者123 更新时间:2023-12-01 10:34:59 25 4
gpt4 key购买 nike

我在支持 Spring-Boot 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语句似乎没有Character表的别名或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 而不是 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 .=?

非常感谢任何帮助。

最佳答案

您的查询无效 - 正如 @Neil Stockton 指出的,通过在 (:episodes) 中写入 h.appearsIn 您说的是“集合中的集合”,这是没有意义的。

你应该像这样声明一个“集合成员变量”:

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

ai 表示 appearsIn 的单个元素(如迭代器)。

关于java - JPA 查询缺少别名和列(Hibernate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34813979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com