I am learning EJB,JPA and have very basic doubt
I have 3 Tables A,B,C
A - ID, Name
B - ID, Name
C - ID, A_ID, B_ID
When i create a Entity Class from Database, i get 3 EJB classes with JPA stuff.
Now in my Managed bean i get either A.Name or B.Name and i need to find the matching entries using C.
Normal SQL Query will look like (may not be the best query)
SELECT a.name FROM schema.A a, schema.B b, schema.C c where b.Name='ABC' and c.B_ID=b.ID and a.ID = c.A_ID;
Now where do i do the above query in my classes.
I came across @SecondaryTable but could not understand how exactly its used.
I also saw em.createQuery( SQL query).getResultList().
Now is the above the best way or is there something available in EJB/JPA which is better.
UPDATE 1:
I was trying to execute the query in em.CreateQuery
em.CreateQuery(SELECT a.name FROM A a, B b, C c where b.Name='ABC' and c.B_ID=b.ID and a.ID = c.A_ID).getResultList();
but i get following error in my GlassFish Server (i am using EclipeLink JPA)
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
Position:
Error Code: 0
Call: SELECT t0.given_name FROM test.a t3, test.a_b t2, test.b t1, test.a t0 WHERE ((((t1.Name = ?) AND (t2.b_id = t1.id.t1.id)) AND (t0.id = )) AND (t3.id = t2.a_id))
bind => [1 parameter bound]
Query: ReportQuery(referenceClass=Consultant sql="SELECT t0.given_name FROM test.a t3, test.a_b t2, test.b t1, test.a t0 WHERE ((((t1.Name = ?) AND (t2.b_id = t1.id.t1.id)) AND (t0.id = )) AND (t3.id = t2.a_id))")
Now why is the SQl statement is messed up in the error log
1.There is an extra entry test.a t0
2.t2.b_id = t1.id.t1.id
3.t0.id=
How does the error log SQL statement gets generated.