JPQL转换和子查询标准API相同呢
问题说明
有5个实体由一个简单的对象模型:
Have a simple object model made up of 5 entities:
- 公司
- 组织
- 地址
- 俱乐部
- 集团
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?
正确答案
本使用内部连接将选择与公司提供的拉链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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20