Search code examples
javaspringhibernatehql

Referencing a join table with no code in a hibernate query


I have searched everything I can think of and read the hibernate docs and haven't been able to answer this question, so I'm turning here.

I have an InboundFaxQueue class. It has a many to many relationship with my InboundFaxDocumentInfo class. So, InboundFaxQueue has the following definition.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "InboundFaxQueue_InboundFaxDocumentInfos",
           joinColumns = @JoinColumn(name = "inbound_fax_queue_id", nullable = false),
           inverseJoinColumns = @JoinColumn(name = "document_id", nullable = false))
@Where(clause = PROCESSED_FILTER)
private final Set<InboundFaxDocumentInfo> faxDocumentInfos = new HashSet<>();

So now I want to write a query that will return the InboundFaxQueue object with a particular document_id. My last clumsy attempt, which wouldn't compile, is this:

<query name="FaxDocument.getFaxQueuesContainingDocument">
    <![CDATA[
    SELECT ifq
      FROM com.casenet.domain.docmgmt.InboundFaxQueue ifq
            WHERE ifq.faxDocumentInfos.document_id = :documentId
    ]]>
</query>

Not sure where to go next.


Solution

  • After lots of trial and error, here's the query that finally worked.

    <query name="FaxDocument.getFaxQueuesContainingDocument">
        <![CDATA[
            SELECT ifq
            FROM InboundFaxQueue AS ifq
            JOIN ifq.faxDocumentInfos AS fdi
                WHERE fdi.id = :documentId
        ]]>
    </query>
    

    Thanks for all who helped.