标准JPA 2,Hibernate - 倍增连接

时间:2014-12-29 12:48:40

标签: hibernate jpa jpa-2.0 criteria-api

我一直在尝试进行以下查询,我尝试了但是我不知道从哪里开始。 考虑到每个表都有一个实体,我怎么能用Criteria Builder创建这个查询?

SELECT   *
        FROM   TCTIPort as CTIPort        
        INNER JOIN TCTIBoardType AS BoardType                    
        ON CTIPort.BoardType = BoardType.BoardType  

        LEFT JOIN TSysPort AS Port            
        ON CTIPort.Switch = Port.Switch AND        
           CTIPort.Machine = Port.Machine AND        
           CTIPort.Port =  Port.Channel

        LEFT JOIN TSysMachineId AS Mach        
        ON Mach.IdApp = Port.IdApp AND        
           Mach.IdTypeApp = Port.IdTypeApp        

        LEFT JOIN TUnPbxGenericValue AS Gen         
        ON CTIPort.Machine = Gen.Machine AND         
           CTIPort.Port = Gen.Port        

        LEFT JOIN TUnPbxGenericName AS GName         
        ON GName.IdGenericName = Gen.IdGenericName        

        LEFT JOIN TSysTypeOfApp STA        
        ON STA.IdTypeOfApp = Port.IdTypeApp  

        LEFT JOIN TSysApplication AS SysApp 
        ON SysApp.IdApp = Port.IdApp 
        AND SysApp.idTypeOfApp = Port.idTypeApp  

        WHERE        
            LTRIM(RTRIM(BoardClass)) IN ('MSI', 'TAP') AND        
            CTIPort.Switch = @Switch AND        
            CTIPort.Machine = @Machine AND        
            (CTIPort.Port = @Port OR @Port IS NULL) AND        
            (Mach.Machine LIKE @NameMachine OR      
            Gen.GenericValue LIKE @NameMachine OR       
            @NameMachine IS NULL)      
        ORDER BY     
            CTIPort.Port 

我应该为每列使用接口“JOIN”吗? 我如何将条件“AND”添加到连接中?

我搜索了一些例子,但它们只显示简单的查询(带有“和”,没有更多的表,最多2个)。

1 个答案:

答案 0 :(得分:2)

JPA 2.0不支持" JOIN + ON"。

您需要执行以下操作:

select p
from Person p
left join p.dogs d
where d.age = 15 and d.sex = 'MALE'

如果要在join子句中添加条件,则需要使用JPA 2.1