jpa, risks of using an illegal named query?

大城市里の小女人 提交于 2019-12-11 14:42:22

问题


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"

Under on:

JOIN FETCH expressions cannot be defined with an identification

Under :currentUser:

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 :

  • Get the newest 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;
    }

回答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.

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.

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+)

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



来源:https://stackoverflow.com/questions/32705101/jpa-risks-of-using-an-illegal-named-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!