Hibernate查询语言中的LEFT JOIN

Phi*_*hil 6 hibernate left-join

我试图在Hibernate查询语言中进行LEFT JOIN,在MySQL中我可以这样做:

select*from day_timetable_timeslots t LEFT JOIN golfnine_date_time_entity d ON d.start_time = t.start_time

在我的表day_timetable_timeslots中,我有一整天的时间间隔,以15分钟为增量.例如.00:00:00,00:15:00,00:00:00,...到一天结束.因此它显示了所有匹配的golfnine_date_time_entity的时间间隔,但我似乎无法弄清楚如何在Hibernate查询语言中执行此操作.

好吧,我可以通过以下HQL进行LEFT JOIN.

从DayTimetableTimeslots o left outer join o.bookings book中选择o.id,book.id,其中o.id> 0

我不知道为什么我必须把o.id> 0放在那里,但它确实有效.

但是,我只想选择book.id,其中预订有条件.我试过了:

从DayTimetableTimeslots o left outer join o.bookings book中选择o.id,book.id,其中o.id> 0和book.dateTime.startDate>'2010-01-01'

但是这不能正常工作,它只显示一些DayTimetableTimeslots,但不是全部,因为它应该这样做.

我基本上想在HQL中执行这个mysql查询.

选择t.id作为start_time_sequence,t.start_time作为all_start_time,d.*来自day_timetable_timeslots t LEFT JOIN golfnine_date_time_entity d ON d.start_time = t.start_time AND t.customer_id = d.customer_id AND d.start_date ='2010-01-24 '在哪里.customer_id = 11

谢谢,菲利普


在mysql中,我可以执行以下操作,它向我显示所有预订与其开始时间.所有开始时间都存储在day_timetable_timeslots中.

选择t.start_time,d.id from day_timetable_timeslots t LEFT JOIN golfnine_date_time_entity d ON d.start_time = t.start_time

'00:00:00', NULL
'00:15:00', NULL
'00:30:00', NULL
'00:45:00', NULL
'01:00:00', '443'
'01:15:00', NULL
'01:30:00', NULL
'01:45:00', NULL
'02:00:00', '444'
Run Code Online (Sandbox Code Playgroud)

...整天,它将golfnine_date_time_entity的id与day_timetable_timeslots中的时间相匹配.

这个mysql查询的好处是我可以在预订上设置一些标准,例如.

选择t.id,t.start_time,d.id from day_timetable_timeslots t LEFT JOIN golfnine_date_time_entity d ON d.start_time = t.start_time AND t.customer_id = d.customer_id AND d.start_date ='2010-01-24'whhere t. customer_id = 11

我明白了

... lots of data then
'31', '07:15:00', NULL
'32', '07:30:00', NULL
'33', '07:45:00', NULL
'34', '08:00:00', '501'
'35', '08:15:00', NULL
'36', '08:30:00', NULL
'37', '08:45:00', NULL
'38', '09:00:00', NULL
'39', '09:15:00', NULL
... lots more data
Run Code Online (Sandbox Code Playgroud)

所以它只显示指定日期的预订和客户ID.

在HQL中很难做到这一点......


这是我想要的HQL连接.

从DayTimetableTimeslots o,LegacyDateTimeEntity b中选择o.id,b.id,其中b.customer.id = 11,b.startDate ='2010-02-07',o.startTime = b.startTime

给出了这个SQL.

select
    daytimetab0_.id as col_0_0_,
    legacydate1_.id as col_1_0_ 
from
    day_timetable_timeslots daytimetab0_,
    golfnine_date_time_entity legacydate1_ 
where
    legacydate1_.customer_id=11 
    and legacydate1_.start_date='2010-02-07' 
    and daytimetab0_.start_time=legacydate1_.start_time
Run Code Online (Sandbox Code Playgroud)

但是 - 它只返回1行,因为只有一个golfnine_date_time_entity匹配,我想要返回所有day_timetable_timeslots行.

所以我尝试了HQL.

选择o.id,o.bookings.size来自DayTimetableTimeslots o left join o.bookings book left join book.dateTime dt with dt.customer.id = 11 and dt.startDate ='2010-02-29'其中1 = 1

遗憾的是,似乎忽略了表达方式.

它返回此SQL.

select
    daytimetab0_.id as col_0_0_,
    (select
        count(bookings3_.timeslot_id) 
    from
        golfnine_booking bookings3_ 
    where
        daytimetab0_.id=bookings3_.timeslot_id) as col_1_0_ 
from
    day_timetable_timeslots daytimetab0_ 
left outer join
    golfnine_booking bookings1_ 
        on daytimetab0_.id=bookings1_.timeslot_id 
left outer join
    golfnine_date_time_entity legacydate2_ 
        on bookings1_.date_time_id=legacydate2_.id 
        and (
            legacydate2_.customer_id=11 
            and legacydate2_.start_date='2010-02-29'
        ) 
where
    1=1
Run Code Online (Sandbox Code Playgroud)

其中只是加入表之间的所有匹配关系并忽略legacydate2_.customer_id = 11和legacydate2_.start_date ='2010-02-29'


我发现这似乎有效.注意我引用了with子句中的dt2.

选择不同的o.startTime,dt2.id,book.uniqueDateTimeResource来自DayTimetableTimeslots o left join o.bookings book with book.deleted = 0 left join book.dateTime dt2 with dt2.startDate ='2010-01-19'and dt2.deleted = 0

RMo*_*sey 1

我可以想到您遇到的两个潜在问题:

  1. 您的 java 和 SQL 代码之间存在日期/时间精度问题。您可以尝试稍微调整日期,看看是否显示正确的值。

  2. 您的休眠映射不正确。看起来你总是加入多个列?你的模式让我有点困惑。您是否使用 @JoinColumns 注释来指定关联中的多个列(如果您使用注释),或者在 XML 映射文件中使用等效项?