jpa,使用非法命名查询的风险? [英] jpa, risks of using an illegal named query?

查看:141
本文介绍了jpa,使用非法命名查询的风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个可以工作的NamedQuery,但是我在eclipse中有多个错误标记.现在我的查询可能不合法, 不可能做我打算在jpa中做的事情.因此,由于无论如何我都设法做到了,但我真的不知道它为什么起作用:我的问题是使用此方法的潜在风险是什么?

I made a NamedQuery that works but I have multiple error markers in eclipse. Now my query might not be legal, an answer of so (see comments) told me it wasn't possible to do what I intended to do in jpa. So, since I managed to do it anyway and I don't really have a clue why it works: my question is what are the underlying risks of using this :

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

根据:

不能用标识定义JOIN FETCH表达式

JOIN FETCH expressions cannot be defined with an identification

在:currentUser下:

Under :currentUser:

输入参数只能在查询的WHERE子句或HAVING子句中使用.

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

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

I didn't manage to get the result I want without it. Which is :

  • 获取最新的Thethread
  • 获取其集合中的当前用户投票.
  • Get the newest Thethread
  • Get only the current user vote in its collection.

如果您知道该怎么做,请成为我的客人.

If you know how to do that, please, be my guest.

实体就是这样:

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支持它,并且其他提供程序也应支持,如果它们支持JPA的2.1版本. 最新的JPA版本中的JOIN运算符是新的,在JPA 2.0(JavaEE 6)和较旧的JPA 1版本中都没有.

Your query is correct JPQL according to JPA 2.1 (JavaEE 7). Eclipselink supports it and other providers should too, if they support 2.1 version of JPA. The operator ON with JOIN is new with this latest JPA version, it was not present in neither JPA 2.0 (JavaEE 6) nor in older JPA 1 versions.

有关更多信息,请参见 EclipseLink Wiki . Wiki声明Eclipselink实现ON运算符,并且它在JPA 2.1草案中.我还检查了它是否也符合最终的JPA 2.1规范-并且在那里.

Here is more info from EclipseLink wiki. The wiki states that Eclipselink implements ON operator and that it is in draft JPA 2.1. I checked also that it is also in final JPA 2.1 specification - and it is there.

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

In order to use your query, you just need to ensure that your environment/application server supports JPA 2.1 (e.g. application server should support Java EE 7, such as Glassfish 4 or WildFly 8+)

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

It is not a problem that your IDE (Eclipse) gives warnings until your query works. Eclipse probably does not support JPA 2.1 syntax or your project must be somehow configured to support JPA 2.1 and not older versions of JPA. Try to look into project properties, under project facets, and ensure you have JPA in version 2.1

这篇关于jpa,使用非法命名查询的风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆