2

I want to annotate following structure:

I have this query:


SELECT A.*, BES.*, BES_2.*
INNER JOIN BES ON A.a = BES.a AND A.b = BES.b
INNER JOIN BES AS BES_2 ON A.a = BES_2.a AND A.b = BES_2.b
WHERE (BES.c = N'foo') AND (BES_2.c = N'bar')

I have the entities Job (representing A) and JobEndPoint (representing BES). The Job object should contain two JobEndPoint which map like a one-to-one relation. I need two JOIN the table two times checking for the same values only differed by the column "c" which I check in the WHERE statement.


@OneToOne
private JobEndPoint from;

@OneToOne
private JobEndPoint to;

My problem is now that the database columns and the object fields differ a lot and I don't know how to add the WHERE statement.

1 Answer 1

0

Create a JPA repository, and type a custom @Query.

I assume you already linked the parent and JobEndPoint classes over a and b fields. (To do that, define a multiple-column id on JobEndPoint and specify joinColumns in the parent class.)

@Query("SELECT u FROM parent 
        LEFT JOIN u.from bes
        LEFT JOIN u.to bes2
        WHERE bes.c = 'foo' 
        AND bes2.c = 'bar'")
Set<Parent> findMatchingParents()

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.