gpt4 book ai didi

java - jpa,使用非法命名查询的风险?

转载 作者:行者123 更新时间:2023-11-30 03:15:39 24 4
gpt4 key购买 nike

我制作了一个可以运行的 NamedQuery,但我在 Eclipse 中有多个错误标记。现在我的查询可能不合法, answer of so (see comments)告诉me it wasn't possible做我打算在 jpa 做的事情。所以,既然我无论如何都设法做到了这一点,而且我真的不知道它为什么有效:我的问题是使用它的潜在风险是什么:

query = "SELECT t FROM Thethread t LEFT JOIN FETCH t.threadVotes tv ON tv.user1=:currentUser ORDER BY t.datePosted DESC"

下:

JOIN FETCH expressions cannot be defined with an identification

在:当前用户下:

Input parameters can only be used in the WHERE clause or HAVING clause of a query.

如果没有它,我就无法获得我想要的结果。这是:

  • 获取最新的Thethread
  • 获取其集合中的当前用户投票。

如果您知道如何做到这一点,请成为我的客人。

实体如下:

public class Thethread implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long idthread;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_posted")
private Date datePosted;

private int downvotes;

private int upvotes;

// bi-directional many-to-one association to ThreadVote
@OneToMany(mappedBy = "thethread")
private List<ThreadVote> threadVotes;
}

public class ThreadVote implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name = "id_votes_thread")
private int idVotesThread;

private int vote;

// bi-directional many-to-one association to Thethread
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "thread")
private Thethread thethread;

// bi-directional many-to-one association to User
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_user")
private User user1;
}

最佳答案

根据 JPA 2.1 (JavaEE 7),您的查询是正确的 JPQL。 Eclipselink 支持它,其他提供商也应该支持它,如果他们支持 2.1 版本的 JPA。带有 JOIN 的 ON 运算符是最新 JPA 版本中的新功能,它在 JPA 2.0 (JavaEE 6) 和旧版 JPA 1 版本中都不存在。

以下是来自 EclipseLink wiki 的更多信息。 wiki 指出 Eclipselink 实现了 ON 运算符,并且它位于 JPA 2.1 草案中。我还检查了它也在最终的 JPA 2.1 规范中 - 并且它就在那里。

为了使用您的查询,您只需确保您的环境/应用程序服务器支持 JPA 2.1(例如应用程序服务器应支持 Java EE 7,例如 Glassfish 4 或 WildFly 8+)

在查询正常运行之前,IDE (Eclipse) 会发出警告,这不是问题。 Eclipse 可能不支持 JPA 2.1 语法,或者您的项目必须以某种方式配置为支持 JPA 2.1 而不是旧版本的 JPA。尝试在项目方面查看项目属性,并确保您拥有版本 2.1 的 JPA

关于java - jpa,使用非法命名查询的风险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32705101/

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