简体   繁体   English

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

[英]jpa, risks of using an illegal named query?

I made a NamedQuery that works but I have multiple error markers in eclipse. 我做了一个可以工作的NamedQuery,但是我在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. 现在我的查询可能不合法,对此的回答(请参阅评论)告诉我,无法执行我打算在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"

Under on: 下开:

JOIN FETCH expressions cannot be defined with an identification JOIN FETCH表达式不能用标识定义

Under :currentUser: 在:currentUser下:

Input parameters can only be used in the WHERE clause or HAVING clause of a query. 输入参数只能在查询的WHERE子句或HAVING子句中使用。

I didn't manage to get the result I want without it. 没有它,我无法获得想要的结果。 Which is : 这是:

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

If you know how to do that, please, be my guest. 如果您知道该怎么做,请成为我的客人。

The entities are as such : 实体是这样的:

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;
    }

Your query is correct JPQL according to JPA 2.1 (JavaEE 7). 根据JPA 2.1(JavaEE 7),您的查询是正确的JPQL。 Eclipselink supports it and other providers should too, if they support 2.1 version of JPA. Eclipselink支持它,并且其他提供程序也应支持,如果它们支持JPA的2.1版本。 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. 此最新JPA版本中的JOIN运算符是新的,JPA 2.0(JavaEE 6)和较旧的JPA 1版本均未提供。

Here is more info from EclipseLink wiki . 这是EclipseLink Wiki的更多信息。 The wiki states that Eclipselink implements ON operator and that it is in draft JPA 2.1. Wiki声明Eclipselink实现ON运算符,并且它在JPA 2.1草案中。 I checked also that it is also in final JPA 2.1 specification - and it is there. 我还检查了它是否也符合最终的JPA 2.1规范-并且在那里。

In order to use your query, you just need to ensure that your environment/application server supports JPA 2.1 (eg application server should support Java EE 7, such as Glassfish 4 or WildFly 8+) 为了使用您的查询,您只需要确保您的环境/应用服务器支持JPA 2.1(例如,应用服务器应支持Java EE 7,例如Glassfish 4或WildFly 8+)。

It is not a problem that your IDE (Eclipse) gives warnings until your query works. 在查询工作之前,IDE(Eclipse)发出警告是没有问题的。 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. Eclipse可能不支持JPA 2.1语法,或者您的项目必须以某种方式配置为支持JPA 2.1,而不是较旧的JPA版本。 Try to look into project properties, under project facets, and ensure you have JPA in version 2.1 尝试查看项目方面下的项目属性,并确保您拥有版本2.1中的JPA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM