我需要在本机查询下面将其转换为“ JPA标准”构建器查询。
SELECT *
FROM (SELECT
communicat0_.event_mode_id AS event_mo1_0_,
communicat0_.created_by AS created_2_0_,
communicat0_.created_on AS created_3_0_,
communicat0_.updated_by AS updated_4_0_,
communicat0_.updated_on AS updated_5_0_,
communicat0_.event_code_id AS event_c12_0_,
communicat0_.event_status AS event_st6_0_,
communicat0_.pref_email AS pref_ema7_0_,
communicat0_.pref_hard_copy AS pref_har8_0_,
communicat0_.pref_letter_hard_copy AS pref_let9_0_,
communicat0_.pref_portal_app AS pref_po10_0_,
communicat0_.pref_sms AS pref_sm11_0_,
emailcommu2_.is_deleted AS emailDeleted,
smscommuni3_.is_deleted AS smsDeleted
FROM communication.communication_event communicat0_
INNER JOIN communication.event_master eventmaste1_
ON communicat0_.event_code_id = eventmaste1_.event_id
LEFT OUTER JOIN communication.email_communication_recipient emailcommu2_
ON communicat0_.event_mode_id = emailcommu2_.event_id
LEFT OUTER JOIN communication.sms_communication_recipient smscommuni3_
ON communicat0_.event_mode_id = smscommuni3_.event_id
WHERE communicat0_.event_mode_id = 1
AND communicat0_.event_status = 'Y') aaa
WHERE (aaa.emailDeleted = 'N'
OR aaa.emailDeleted IS NULL)
AND (aaa.smsDeleted = 'N'
OR aaa.smsDeleted IS NULL)
我尝试过的代码(不是有效的代码。在子查询表达式中发生编译错误)
CommunicationEvent event = new CommunicationEvent();
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<CommunicationEvent> mainCriteriaQuery = builder.createQuery(CommunicationEvent.class);
Root<CommunicationEvent> mainRoot = mainCriteriaQuery.from(CommunicationEvent.class);
Join<CommunicationEvent, EventMaster> eventMasterJoin = mainRoot.join(CommunicationEvent_.eventCodeId, JoinType.INNER);
Join<EventMaster, EmailTemplateMaster> emailTemplateJoin = eventMasterJoin.join(EventMaster_.emailTemplateMaster,JoinType.LEFT);
Join<EventMaster, SmsTemplateMaster> smsTemplateJoin = eventMasterJoin.join(EventMaster_.smsTemplateMaster,JoinType.LEFT);
//Sub Query
CriteriaQuery<Character> criteriaQuery = builder.createQuery(Character.class);
Subquery<Character> sub = criteriaQuery.subquery(Character.class);
Root<CommunicationEvent> subRoot = sub.from(CommunicationEvent.class);
Join<CommunicationEvent, EmailCommunicationRecipient> emailRecipientJoin = subRoot.join(CommunicationEvent_.emailCommunicationRecipient,JoinType.LEFT);
Join<CommunicationEvent, SMSCommunicationRecipient> smsRecipientJoin = subRoot.join(CommunicationEvent_.smsCommunicationRecipient,JoinType.LEFT);
Expression<Character> subQueryConcat = builder.(subRoot.get(SMSCommunicationRecipient_.isDeleted),
builder.lower(subRoot.get(EmailCommunicationRecipient_.isDeleted))));
sub.select(subQueryConcat).where(builder.equal(subRoot.get(CommunicationEvent_.eventId), eventId),
builder.equal(subRoot.get(CommunicationEvent_.eventStatus),CommunicationConstants.FLAG_Y));
criteriaQuery.select(sub);
/*.where(builder.equal(sub.get(SMSCommunicationRecipient_.isDeleted), CommunicationConstants.FLAG_N),
builder.equal(sub.get(EmailCommunicationRecipient_.isDeleted), CommunicationConstants.FLAG_N));*/
Query query = manager.createQuery(criteriaQuery);
event = (CommunicationEvent) query.getSingleResult();
/*String hql = "from Event E" + " left join fetch E.emailCommunicationRecipient ECR "
+ " left join fetch E.smsCommunicationRecipient SCR " + " where E.eventId = :eventId "
+ "and (ECR.isDeleted is null or ECR.isDeleted = 'N') "
+ "and (SCR.isDeleted is null or SCR.isDeleted = 'N') ";
TypedQuery<Event> query = manager.createQuery(hql, Event.class);
query.setParameter("eventId", eventId);
Event event = (Event) query.getSingleResult();*/
return event;