• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JPQL转换和子查询标准API相同呢

用户头像
it1352
帮助1

问题说明

有5个实体由一个简单的对象模型:

Have a simple object model made up of 5 entities:

    1. 公司
    1. 组织
    1. 地址
    1. 俱乐部
    1. 集团

学新通
学新通

A公司与单个组织有关。 (组和俱乐部也与一个组织相关 - 这些都是单向的,这意味着组织中不包含它的主人的引用)。一个组织可以有0个或多个地址(ES)。

A Company is associated with a single Organization. (Group and Club are also associated with a single Organization - these are unidirectional, meaning the Organization does not contain a reference to its owner). An Organization may have 0 or more Address(es).

一个子查询可用于基于特定拉链code,这是一个地址的一个属性来访问公司的对象。
这里是可以与特定的拉链code访问这些公司JPQL查询。

A subquery can be used to access Company objects based on a specific zipcode, which is an attribute of an Address. Here is a JPQL query that can access those companies with a specific zipcode.

@Query(从公司P,组织组织那里器选择p
  (p.organization = org.id)和存在(从地址的广告,其中选择1
  拉链code =:拉链code和ad.organization = org.id))

@Query("select p from Company p, Organization org where (p.organization = org.id) and exists ( select 1 from Address ad where zipcode = :zipcode and ad.organization = org.id)")

如何能在相同的事情来使用JPA标准API做了什么?

How can the same thing be done using the JPA Criteria API?

正确答案

#1

本使用内部连接将选择与公司提供的拉链code,这是你想要的吗?

This will select companies with provided zipcode using inner join, is this what you wanted?

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Company> cq = cb.createQuery(Company.class);
Root<Organization> root = cq.from(Organization.class);
Join<Organization, Address> adr = root.join("addresses");
cq.select(root.get("company"));
cq.where(cb.equal(adr.get("zipcode"), zipcode));

您将不能够通过组织访问公司,如果它不具有它的性质。如果你的公司要组织一个参考,那么你可以让单向映射,双向映射添加以下code到组织类:

You won't be able to access Company via Organization, if it doesn't have a property for it. If your Company have a reference to Organization, then you can make unidirectional mapping to bidirectional mapping adding following code to Organization class:

@OneToOne(mappedBy="organization") //Providing the name of property in Company 
private Company company;

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgabfi
系列文章
更多 icon
同类精品
更多 icon
继续加载