简体   繁体   English

JPAQuery(jpql)从抽象类到具体类的铺设连接

[英]JPAQuery (jpql) laying join from abstract class to concrete class

Maybe simple question : 也许是简单的问题:

abstract class Contact

class ContactOffice extends Contact

class ContactUser extends Contact

This is the setup, now I'm trying to search for a specific name of office. 这是设置,现在我正在尝试搜索办公室的特定名称。

new JPAQuery().from(recipient).innerJoin(recipient.recipient(),QContact.contact)

Now I need to lay a link between Contact and ContactOffice. 现在,我需要在Contact和ContactOffice之间建立链接。

I tried : 我试过了 :

.innerJoin(recipient.recipient(),QContactOffice.contactOffice._super)

But this gives this output : 但这给出了以下输出:

  inner join treat(notaRecipient.recipient as ContactOffice) as contactOffice

where he complains about the ( 他抱怨的地方(

any suggestions would be gratefull. 任何建议将不胜感激。

Apparently seems this issue still a little unexplored, or unimplemented. 显然,这个问题仍然有待探索或实施。

The solution I opted for is doing a native query in the Repository. 我选择的解决方案是在存储库中进行本机查询。
Advantage is that I have the full query under control and normally this would be the fastest way, as I can also skip the T_CONTACT and directly go to T_CONTACT_USER and T_CONTACT_OFFICE, because the id's are unique. 优点是我可以控制完整的查询,通常这是最快的方法,因为我也可以跳过T_CONTACT并直接转到T_CONTACT_USER和T_CONTACT_OFFICE,因为ID是唯一的。

Edit : 编辑:

While I first got some good results, I got a problem with the in clause because this native query was actually a subquery and could return more then 2000 objects, and the DB driver doesn't allow this. 当我第一次得到一些好的结果时,我遇到了in子句的问题,因为该本机查询实际上是一个子查询,可以返回2000个以上的对象,并且数据库驱动程序不允许这样做。

But there is also good news . 但也有个好消息

I made the JpaSubQuery to work, you need to make the path's otherwise you can't query with the BooleanBuilder as mine example later on. 我使JpaSubQuery可以工作,您需要进行路径设置,否则以后将无法使用BooleanBuilder查询。

new JPASubQuery().from(recipient)
                    .innerJoin(recipient.nota(), QNota.nota)
                    .leftJoin(recipient.recipient().as(QContactUser.class).user(), QLightUser.lightUser)
                    .leftJoin(recipient.recipient().as(QContactOffice.class).office(), QOffice.office)
                    .leftJoin(recipient.recipient().as(QContactExternal.class).external(), QExternal.external)
                    .where(predicateBuilder).distinct()
                    .list(recipient.nota())));

As you can see I can make the links correct and this is how I search : 如您所见,我可以使链接正确,这就是我的搜索方式:

predicateBuilder.and(recipient.recipient().as(QContactUser.class).user().firstName.containsIgnoreCase(query.getToUser())
                        .or(recipient.recipient().as(QContactUser.class).user().lastName.containsIgnoreCase(query.getToUser())));

Because now the complete query is going to the DB I don't have to worry about the 2000 limit, only performance issues. 因为现在完整的查询将发送到数据库,所以我不必担心2000的限制,而只需担心性能问题。
But, if you ever need to link an abstract class or interface with implementation, this is how it could be done. 但是,如果您需要将抽象类或接口与实现链接,则可以这样做。

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

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