Criteria Builder JOIN不起作用

时间:2018-06-28 11:21:12

标签: hibernate jpa

我不是“条件”构建器中的以下转换查询。

 select * from communication.event_master event inner join communication.email_communication_recipient email

打开event.event_id = email.event_id内部加入communication.sms_communication_recipient短信 开启event.event_id = sms.event_id

代码:

@SuppressWarnings("unchecked")
@Override
  public Event retrieveCommunicationEvent(Integer eventId) throws CommunicationException {

    List<Predicate> conditions = new ArrayList<>();
    CriteriaBuilder builder = manager.getCriteriaBuilder();
    CriteriaQuery<Event> criteriaQuery = builder.createQuery(Event.class);
    Root<Event> root = criteriaQuery.from(Event.class);
    //Join<Event, EmailTemplateMaster> emailTemplateMaster = root.join(Event_.emailTemplateMaster, JoinType.INNER);
    /*Fetch<Event, EmailCommunicationRecipient> fetchEmailRecipient = root.fetch(Event_.emailCommunicationRecipient, JoinType.LEFT);
    Fetch<Event, SMSCommunicationRecipient> fetchSMSRecipient = root.fetch(Event_.smsCommunicationRecipient, JoinType.LEFT);*/
    //root.fetch(Event_.eventTrigger,JoinType.LEFT);

    SetJoin<Event, EmailCommunicationRecipient> fetchEmailRecipient = root.join(Event_.emailCommunicationRecipient, JoinType.LEFT);
    SetJoin<Event, SMSCommunicationRecipient> fetchSMSRecipient = root.join(Event_.smsCommunicationRecipient, JoinType.LEFT);

    conditions.add(builder.equal(root.get(Event_.eventId), eventId));
    conditions.add(builder.equal(fetchEmailRecipient.get(EmailCommunicationRecipient_.isDeleted), CommunicationConstants.FLAG_N));
    conditions.add(builder.equal(fetchSMSRecipient.get(SMSCommunicationRecipient_.isDeleted), CommunicationConstants.FLAG_N));

        criteriaQuery.select(root).where(conditions.toArray(new Predicate[conditions.size()]));
        Query query = manager.createQuery(criteriaQuery);
        List<Event> eventList = query.getResultList();
        Event event = null;
        if (null != eventList && !eventList.isEmpty()) {
            event = eventList.get(0);
        }
        return event;

} 

生成的休眠查询:

  Hibernate: select event0_.event_id as event_id1_4_, event0_.created_by as created_2_4_, event0_.created_on as created_3_4_, event0_.updated_by as updated_4_4_, event0_.updated_on as updated_5_4_, event0_.email_template_followed as email_te6_4_, event0_.event_code as event_co7_4_, event0_.event_module_identifier as event_mo8_4_, event0_.event_name as event_na9_4_, event0_.is_deleted as is_dele10_4_, event0_.event_template_recipient as event_t11_4_, event0_.pref_email as pref_em12_4_, event0_.pref_hard_copy as pref_ha13_4_, event0_.pref_sms as pref_sm14_4_, event0_.sms_template_followed as sms_tem15_4_, event0_.trigger_point as trigger16_4_ from communication.event_master event0_ left outer join communication.email_communication_recipient emailcommu1_ on event0_.event_id=emailcommu1_.event_id left outer join communication.sms_communication_recipient smscommuni2_ on event0_.event_id=smscommuni2_.event_id where event0_.event_id=41 and emailcommu1_.is_deleted=? and smscommuni2_.is_deleted=?

   Hibernate: select smstemplat0_.event_id as event_id8_4_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_0_, smstemplat0_.sms_template_identifier as sms_temp1_8_1_, smstemplat0_.created_by as created_2_8_1_, smstemplat0_.created_on as created_3_8_1_, smstemplat0_.updated_by as updated_4_8_1_, smstemplat0_.updated_on as updated_5_8_1_, smstemplat0_.event_id as event_id8_8_1_, smstemplat0_.is_deleted as is_delet6_8_1_, smstemplat0_.sms_template_text as sms_temp7_8_1_ from communication.sms_template_master smstemplat0_ where smstemplat0_.event_id=?

  Hibernate: select emailtempl0_.event_id as event_i10_4_0_, emailtempl0_.email_template_identifier as email_te1_3_0_, emailtempl0_.email_template_identifier as email_te1_3_1_, emailtempl0_.created_by as created_2_3_1_, emailtempl0_.created_on as created_3_3_1_, emailtempl0_.updated_by as updated_4_3_1_, emailtempl0_.updated_on as updated_5_3_1_, emailtempl0_.email_attachment_type as email_at6_3_1_, emailtempl0_.email_subject_name as email_su7_3_1_, emailtempl0_.email_template_text as email_te8_3_1_, emailtempl0_.event_id as event_i10_3_1_, emailtempl0_.is_deleted as is_delet9_3_1_ from communication.email_template_master emailtempl0_ where emailtempl0_.event_id=?

   Hibernate: select smscommuni0_.event_id as event_id7_4_0_, smscommuni0_.sms_recipient_id as sms_reci1_7_0_, smscommuni0_.sms_recipient_id as sms_reci1_7_1_, smscommuni0_.created_by as created_2_7_1_, smscommuni0_.created_on as created_3_7_1_, smscommuni0_.updated_by as updated_4_7_1_, smscommuni0_.updated_on as updated_5_7_1_, smscommuni0_.event_id as event_id7_7_1_, smscommuni0_.is_deleted as is_delet6_7_1_, smscommuni0_.recipient_group_id as recipien8_7_1_, communicat1_.reference_data_id as referenc1_9_2_, communicat1_.created_by as created_2_9_2_, communicat1_.created_on as created_3_9_2_, communicat1_.updated_by as updated_4_9_2_, communicat1_.updated_on as updated_5_9_2_, communicat1_.category as category6_9_2_, communicat1_.description as descript7_9_2_, communicat1_.effective_end_date as effectiv8_9_2_, communicat1_.effective_start_date as effectiv9_9_2_, communicat1_.is_deleted as is_dele10_9_2_, communicat1_.link_id as link_id11_9_2_, communicat1_.reference_code as referen12_9_2_, communicat1_.reference_subtype as referen13_9_2_, communicat1_.reference_type as referen14_9_2_, communicat1_.reference_value as referen15_9_2_, communicat2_.reference_data_id as referenc1_9_3_, communicat2_.created_by as created_2_9_3_, communicat2_.created_on as created_3_9_3_, communicat2_.updated_by as updated_4_9_3_, communicat2_.updated_on as updated_5_9_3_, communicat2_.category as category6_9_3_, communicat2_.description as descript7_9_3_, communicat2_.effective_end_date as effectiv8_9_3_, communicat2_.effective_start_date as effectiv9_9_3_, communicat2_.is_deleted as is_dele10_9_3_, communicat2_.link_id as link_id11_9_3_, communicat2_.reference_code as referen12_9_3_, communicat2_.reference_subtype as referen13_9_3_, communicat2_.reference_type as referen14_9_3_, communicat2_.reference_value as referen15_9_3_ from communication.sms_communication_recipient smscommuni0_ left outer join ref_data.reference_data_master communicat1_ on smscommuni0_.recipient_group_id=communicat1_.reference_data_id left outer join ref_data.reference_data_master communicat2_ on communicat1_.link_id=communicat2_.reference_data_id where smscommuni0_.event_id=?

    Hibernate: select emailcommu0_.event_id as event_id8_4_0_, emailcommu0_.email_recepient_id as email_re1_2_0_, emailcommu0_.email_recepient_id as email_re1_2_1_, emailcommu0_.created_by as created_2_2_1_, emailcommu0_.created_on as created_3_2_1_, emailcommu0_.updated_by as updated_4_2_1_, emailcommu0_.updated_on as updated_5_2_1_, emailcommu0_.event_id as event_id8_2_1_, emailcommu0_.is_deleted as is_delet6_2_1_, emailcommu0_.recipient_group_id as recipien9_2_1_, emailcommu0_.recipient_type_id as recipien7_2_1_, communicat1_.reference_data_id as referenc1_9_2_, communicat1_.created_by as created_2_9_2_, communicat1_.created_on as created_3_9_2_, communicat1_.updated_by as updated_4_9_2_, communicat1_.updated_on as updated_5_9_2_, communicat1_.category as category6_9_2_, communicat1_.description as descript7_9_2_, communicat1_.effective_end_date as effectiv8_9_2_, communicat1_.effective_start_date as effectiv9_9_2_, communicat1_.is_deleted as is_dele10_9_2_, communicat1_.link_id as link_id11_9_2_, communicat1_.reference_code as referen12_9_2_, communicat1_.reference_subtype as referen13_9_2_, communicat1_.reference_type as referen14_9_2_, communicat1_.reference_value as referen15_9_2_, communicat2_.reference_data_id as referenc1_9_3_, communicat2_.created_by as created_2_9_3_, communicat2_.created_on as created_3_9_3_, communicat2_.updated_by as updated_4_9_3_, communicat2_.updated_on as updated_5_9_3_, communicat2_.category as category6_9_3_, communicat2_.description as descript7_9_3_, communicat2_.effective_end_date as effectiv8_9_3_, communicat2_.effective_start_date as effectiv9_9_3_, communicat2_.is_deleted as is_dele10_9_3_, communicat2_.link_id as link_id11_9_3_, communicat2_.reference_code as referen12_9_3_, communicat2_.reference_subtype as referen13_9_3_, communicat2_.reference_type as referen14_9_3_, communicat2_.reference_value as referen15_9_3_ from communication.email_communication_recipient emailcommu0_ left outer join ref_data.reference_data_master communicat1_ on emailcommu0_.recipient_group_id=communicat1_.reference_data_id left outer join ref_data.reference_data_master communicat2_ on communicat1_.link_id=communicat2_.reference_data_id where emailcommu0_.event_id=? 

0 个答案:

没有答案