gpt4 book ai didi

java - Hibernate查询语言: Getting a SQL Syntax Error for my Query

转载 作者:行者123 更新时间:2023-12-01 17:16:06 24 4
gpt4 key购买 nike

我已经检查了很多相关问题,但那里的答案还没有解决我的问题。

我有通过 Hibernate 映射的实体 UserCharacter

@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = { "username" }))
public class User {
@Id
@NotEmpty
@Column(name = "username")
private String username;

...
}

@Entity
@Table(name = "character", uniqueConstraints = @UniqueConstraint(columnNames = { "charId" }))
public class Character {
...

@NotEmpty
@ManyToOne
@JoinColumn(name = "username", referencedColumnName = "username")
private User user;

...
}

我想选择具有特定用户名的所有字符,因此我使用的查询如下所示:

private final static String RETRIEVE_CHARACTERS_FOR_USER = "select c from Character as c inner join User as u where u.username = :paramUsername";

我的代码如下所示:

result = session.createQuery(RETRIEVE_CHARACTERS_FOR_USER, Character.class)
.setParameter(PARAM_USERNAME, user.getUsername()).getResultList();

我还尝试删除外键,使用String username而不是User user并只查询c.username,所以我我什至不确定问题是否与 Hibernate 有关...

希望你能帮助我! :)

最佳答案

这应该有效:

private final static String RETRIEVE_CHARACTERS_FOR_USER = "select c from Character c where c.user.username = :paramUsername";

关于java - Hibernate查询语言: Getting a SQL Syntax Error for my Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61384510/

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