【问题标题】:How can I make this sql query with jpa ?如何使用 jpa 进行此 sql 查询?
【发布时间】:2013-05-15 23:34:51
【问题描述】:

我正在尝试在我的实体中创建查询。

我在 mysql DB 中有以下表格:

 Categorie : id, nom ...

 Article : num_code_barre_, categorie ...  

在我的实体类别中:

...

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 30)
@Column(name = "nom")
private String nom;

...

在我的实体文章中:

...

@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "num_code_barre_")
private String numCodeBarre;
@Basic(optional = false)
@NotNull
@Column(name = "categorie")
private int categorie;

...

我已经在我的数据库中测试了这个 sql 查询,它的工作:

SELECT c.nom , COUNT(a.numCodeBarre)  FROM categorie c INNER JOIN article a ON c.id =        a.categorie GROUP BY a.categorie

但是当我尝试在我的类别实体中创建 namedQuery 时,我在输出中收到错误:“Synthax error parsing the query ...”

@NamedQuery(name = "Categorie.count", query = "SELECT c.nom , COUNT(a.numCodeBarre)  FROM categorie c INNER JOIN article a ON c.id = a.categorie GROUP BY a.categorie")

有人知道我该怎么做吗?

【问题讨论】:

    标签: jakarta-ee jpa glassfish ejb


    【解决方案1】:

    “ON”不是 JPA 1.0 或 JPA 2.0 中 JPQL FROM 子句的一部分。它仅在 JPA 2.1 中引入,我怀疑您是否正在使用它,因为尚未完全发布

    【讨论】:

      【解决方案2】:

      我不确定您的 SQL 为何会导致语法错误,但您可以尝试以下 JPQL 语句:

      @NamedQuery(name = "Categorie.count", query = "SELECT c.nom , COUNT(a.numCodeBarre)  FROM categorie as c, article as a WHERE c.id = a.categorie GROUP BY a.categorie")
      

      【讨论】:

        猜你喜欢
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多