且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JPA通过查询联接表

更新时间:2023-02-03 16:23:57

您需要

You need a short introduction in JPQL, but I will try to quickly explain some missing parts:

在JPQL查询中,您不写JOIN条件(即ON表达式),而是从实体图开始浏览实体图(在下面以StudentWithGraduate实体开头):

In a JPQL query you do not write which are the JOIN conditions (i.e the ON expresions), and instead you navigate through your Entity Graph, beginning with an entity (below I begin with the StudentWithGraduate entity):

SELECT p.name, o.graduate FROM StudentWithGraduate o
    LEFT JOIN o.student s
    LEFT JOIN o.subject p
    WHERE s.surname=:name

:name"被称为命名参数,它可以帮助您 SQL注入.为了为其设置一个值,您可以编写以下内容:

The ":name" is called a named parameter, which helps you agains SQL injections. In order to set a value for it, you write the following:

Query query = em.createQuery(aboveQuery);
query.setParameter("name", parameterValue);
//....the rest of parameters + getResultList();