首页 \ 问答 \ 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)

如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)

请建议如何为给定的SQL查询编写JPA EntityManager createQuery

select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'

Please suggest how can I write the JPA EntityManager createQuery for the given SQL Query:

select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'
更新时间:2023-03-21 12:03

最满意答案

您可以使用jpa的本机SQL查询:

Query q = em.createNativeQuery("select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'");

List<Object[]> result= q.getResultList();

// for each line retrieved
for (Object[] currentLine : result) {
    System.out.println("Allocated=" + currentLine[0]
                    + ", Bench=" + currentLine[1] ;
}

不保证,但你可以测试。


You can use a native sql query with jpa :

Query q = em.createNativeQuery("select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'");

List<Object[]> result= q.getResultList();

// for each line retrieved
for (Object[] currentLine : result) {
    System.out.println("Allocated=" + currentLine[0]
                    + ", Bench=" + currentLine[1] ;
}

No guarantee but you may test.

相关问答

更多
  • 为什么要使用JPA而不是直接在Java文件上编写SQL查询(即直接写入JDBC)? 某些项目需要工程师更多地关注对象模型,而不是用于访问数据存储的实际SQL查询。 这个问题实际上可以解释为 为什么要使用ORM框架? 在不同的情况下可以有不同的答案。 大多数项目可以从领域模型中受益,持久性是第二个关注点。 使用JPA(实现)或大多数其他ORM框架,可以将所有实体(即数据库中的表)模型化为Java中的类。 此外,还可以将行为嵌入这些类中,从而实现行为丰富的域模型。 这个模型中的实体可以有多种用途,包括替换跨层传 ...
  • Criteria criteria = session.createCriteria(Customers.class); criteria.add(Restrictions.eq("Country", "Germany")); criteria.add(Restrictions.or( Restrictions.eq("City", "Berlin"), Restrictions.eq("Ci ...
  • 本机查询执行成功,JSR 317规范的第3.8.15节有助于理解语法,除了使用我的代码进行命中和试验。 The native query executed successfully, The Section 3.8.15 of JSR 317 specification is helpful to understand the syntax, besides hit and trials with my code.
  • 由于您已经在实体中定义了多对多关系,因此无需再次在查询中调用联接。 您可以将查询更改为这样的内容 SELECT c FROM Dejavnosti WHERE c.produktis.naziv LIKE ? OR c.naziv LIKE ? Since you've already defined your many-to-many relationship in your entities, there's no need to call out the join in you query agai ...
  • 您需要创建与表对应的正确实体。 一个。 Business_Account实体 湾 Work_Location是另一个 C。 您可以选择为a之间的连接创建单独的连接表。 和b。 我会建议,但它是你的偏好。 基于a。 和b。 尝试这些方面的东西 - String countyName="Dublin"; final TypedQuery query = entityManager .createQuery( "Select dist ...
  • 要在JPA中执行本机SQL查询,您必须执行以下操作: EntityManager manager = getEntityManager(); Query query = manager.createNativeQuery("SELECT * FROM user WHERE user.id = :id AND user.enabled = :enabled;"); query.setParameter("id", 3); query.setParameter("enabled", 1); Object[] ...
  • 使用 SELECT x FROM Payment x WHERE x.amount > (SELECT AVG(p.amount) from Payment p) 你的子查询是 SELECT AVG(x.amount) from Payment , x未定义。 Use SELECT x FROM Payment x WHERE x.amount > (SELECT AVG(p.amount) from Payment p) Your subquery is SELECT AVG(x.amount) ...
  • 您可以像这样调用JPQL查询, public interface LoanReportRepository extends JpaRepository , JpaSpecificationExecutor { public final static String GET_LOAN_REPORTS = "SELECT lr FROM LoanReport lr WHERE product = :product"; @Query(GET_LOAN_ ...
  • 我不知道你的数据,但这是我认为正在发生的事情 - 您有以下渴望的关联 document -> project (manyToOne is eager by default) document -> states project -> documents project -> users user -> ... (this is not shown in question, but there could be other eager associations) 用它的相应项目加载文档后 - 获取所有项 ...
  • 您可以使用jpa的本机SQL查询: Query q = em.createNativeQuery("select sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated, sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench from userbean where Organizational_Unit = 'SIDG Java' ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)