简体   繁体   中英

How to achieve Join With Temporary Table using Spring JPA/Hibernate

Here is my scenario, I have 3 Entities/Table, lets say, MainEntity, EntityA and EntityB. Their Properties and relation looks as below.

MainEntity [ entityID, entityName, entityType ]

EntityA [ entityID, entityName, typeId ]

EntityB [ typeId, entityType, ]

In SQL the output could be achieved as below.

Select mainEntity.* from MainEntity as mainEnity INNER JOIN (SELECT A.entityID, A.entityName B.entityType FROM EntityA as A LEFT JOIN EntityB as B on A.typeId=B.typeId) as Temp ON mainEnity.entityID = Temp.enityID AND mainEnity.entityName = Temp.entityName AND mainEnity.typeId = Temp.typeId;

Can some one please help with equivalent Hibernate code for above SQL using criteria builder.

HQL is very similar to SQL. One big difference is that it doesn't support subqueries in the from clause though, but most of the time you don't need that. Your query could be rewritten to the following:

SELECT mainEntity 
FROM MainEntity AS mainEnity 
JOIN EntityA AS A ON mainEnity.entityID = A.enityID AND mainEnity.entityName = A.entityName
JOIN EntityB AS B ON mainEnity .typeId = B.typeId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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