Data domain (kind of email model) :
- Message(id,subject,messageAttachment) OneToMany MessageAttachment(messageId,attachmentSHA1,message)
- Message(id,subject,messageAddress) OneToMany MessageAddress(messageId,email,type,message)
(@ManyToOne and OneToMany are bi-directionals)
My goal is to do :
SELECT
MessageAttachment.attachmentSHA1,
Message.subject,
MessageAddress.email
FROM MessageAttachment
JOIN FETCH Message {ON clause should be generated based on the domain}
JOIN FETCH MessageAttachment {ON clause should be generated based on the domain}
WHERE type='ReplyTo'
I want of course to avoid the N+1 problem (so I want just one select).
And I need the Criteria API to add dynamically specifications to the query.
So, something like that :
void go() {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<MessageAttachment> query = criteriaBuilder.createQuery(MessageAttachment.class);
Root<MessageAttachment> root = query.from(MessageAttachment.class);
Fetch<Message, MessageAttachment> fetch = root.fetch("message").fetch("messageAddress");
//query.select(root).where(criteriaBuilder.equal(root.get("message").get("messageAddress").get("type"), "ReplyTo"));
List<MessageAttachment> l = em.createQuery(query).setMaxResults(2).getResultList();
System.err.println(l);
}
If I remove the comment, I get this Exception : Caused by: java.lang.IllegalStateException: Illegal attempt to dereference path source [null.message.messageAttachment] of basic type