简体   繁体   English

使用JPA在JPQL中是否可以替代本机查询的“ GROUP_CONCAT”?

[英]Is there an alternative for native query's 'GROUP_CONCAT' in JPQL with JPA?

I am trying to fetch profile menus of a profile using jpql with JPA. 我正在尝试使用jpql和JPA来获取配置文件的配置文件菜单。 My 'Profile' and 'ProfileMenus' entities have many to one relationship. 我的“个人资料”和“ ProfileMenus”实体具有多对一关系。

I have tried looking into these answers but couldn't find any working solution. 我尝试调查这些答案,但找不到任何有效的解决方案。

How to add non-standardized sql functions in Spring Boot application? 如何在Spring Boot应用程序中添加非标准化的sql函数?

Registering a SQL function with JPA and Hibernate 向JPA和Hibernate注册SQL函数

https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/ https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/

I also went through this link and seems to have same problem as mine, How to register non-standarized SQL functions manually in Spring Boot application? 我也经历了这个链接,似乎和我的一样, 如何在Spring Boot应用程序中手动注册非标准的SQL函数?

When using native query I can fetch my data using the query below: 使用本机查询时,我可以使用以下查询获取数据:

"SELECT
 GROUP_CONCAT(pm.user_menu_id SEPARATOR ',')
 AS profile_menu_ids,
 p.description
 FROM profile p
 LEFT JOIN profile_menu pm ON p.id = pm.profile_id
 WHERE
 p.id =:profileId
 AND
 pm.status = 'Y'
 GROUP BY p.id"

The above query provides me data as, 上面的查询为我提供了以下数据:

|profile_menu_ids|description  |
|----------------|-------------|
|4,5             |admin profile|

Is there any way or alternatives in jpql with JPA to obtain result as above? 使用JPA在jpql中是否有任何方法或替代方法可以得到如上所述的结果?

You may consider using FluentJPA , which supports any custom function: 您可以考虑使用FluentJPA ,它支持任何自定义功能:

public ProfileMenuGroup getMenuIdsByProfile(int profileId) {
    FluentQuery query = FluentJPA.SQL((Profile p,
                                       ProfileMenu pm) -> {
        String menuIds = alias(GROUP_CONCAT(pm.getUserMenuId(), ","),
                                            ProfileMenuGroup::getProfileMenuIds);
        String description = alias(p.getDescription(), ProfileMenuGroup::getDescription);

        SELECT(menuIds, description);
        FROM(p).LEFT_JOIN(pm).ON(p == pm.getProfile());
        WHERE(p.getId() == profileId && pm.getStatus() == "Y");
        GROUP(BY(p.getId()));
    });
    return query.createQuery(em, ProfileMenuGroup.class).getSingleResult();
}

the query produces the following SQL ( profileId is auto bound): 查询产生以下SQL( profileId是自动绑定的):

SELECT GROUP_CONCAT(t1.user_menu_id SEPARATOR ',') AS profile_menu_ids,
                                    t0.description AS description 
FROM profile t0  LEFT JOIN profile_menu t1  ON (t0.id = t1.profile_id) 
WHERE ((t0.id = ?1) AND (t1.status = 'Y')) 
GROUP BY  t0.id

Given the following type declarations: 给定以下类型声明:

@Entity
@Data // lombok
@Table(name = "profile")
public class Profile {
    @Id
    private int id;

    private String description;
}

@Entity
@Data // lombok
@Table(name = "profile_menu")
public class ProfileMenu {
    @Id
    private int id;

    @ManyToOne
    @JoinColumn(name = "profile_id")
    private Profile profile;

    private int userMenuId;

    private String status;
}

@Tuple
@Data // lombok
public class ProfileMenuGroup {

    private String profileMenuIds;

    private String description;
}

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

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