我是新手
我正在尝试将此Postgres查询传递给JPA/JPQL
SELECT
DISTINCT(srv.code) AS Serv_Cod,
srv.id AS Serv_id,
srv.description AS Serv_Desc
FROM db.Category AS cat
join db.Classification AS cla ON cat.id = cla.cat_id
join db.Service AS srv ON srv.id = cla.srv_id
WHERE cat.id = 10
ORDER BY srv.id;
现在,我想编写相同的查询,我有具有相同名称表的实体。
分类
@Entity
@Table(name = "Classification", schema = "db")
@Audited
public class Classification implements Identifiable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "srv_id", nullable = true)
private Service service;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cat_id", nullable = true)
private Category category;
....
}
服务
@Entity
@Table(name = "Service", schema = "db")
@Audited
public class Service implements Identifiable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "code", nullable = false)
private String code;
@Column(name = "description", nullable = false)
private String description;
....
}
我在看书,但我很困惑。。。
我不知道怎么写
ON
对于
JOIN
,并为列/字段建立DISTINCT。
Long myID = 25L;
this.em.createQuery("SELECT NEW SomeDto(srv.id, srv.code, srv.description)"
+ " FROM Classification cla"
+ "JOIN cla·cat_id cat"
+ "JOIN cla·srv_id srv"
+ "WHERE cat.id = :id"
,BaseObjectDto.class).setParameter("id", myID).getResultList();
感谢您的宝贵帮助。