Gopi Lal
Gopi Lal

Reputation: 427

Using @ManyToOne in where condition of JPQL JPA?

I'm working on JPA and I got stuck with syntax or right approach to write JPQL, I have entities: department and employee,

@Entity
Department

@OneToMany
Employee
-----------------------------------------

@Entity
Employee

@ManyToOne
Department

Now I have DOA where I am just adding condition dynamically to the JPQL query for where condition

I need to pull the employee who belong to XYZ department

So my JPQL looks like this

select * from employee where department='PK_OF_DEPT'

NOTE : PK_OF_DEPT is the PK (some sequence)

The error which I am getting is syntax error unknown column department, I tried giving department.id = still not working invalid path error,

NOTE : Id is the attribute name in java object

Can someone help me to get the right syntax, NOTE : I can't use FIND method since it need to be in that DOA which Is used dynamically for listing

Upvotes: 1

Views: 1142

Answers (2)

Angad Bansode
Angad Bansode

Reputation: 923

Try this one :

@Query("select d.dname,d.deptno,e.ename,e.eno from Department d join d.employees e where d.id = ?1")
List<Object[]> getDepartmentAndEmployeeDetailsCustomQuery(int departId);

Upvotes: 1

HBo
HBo

Reputation: 633

There's not much information here, but try this

from employee left join fetch employee.department where department.id='PK_OF_DEPT'

Upvotes: 1

Related Questions