Java JPA Inner JOIN with WHERE statement
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 o开发者_如何学Gobject fields differ a lot and I don't know how to add the WHERE statement.
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()
精彩评论