If I have something like this
@Entity
public class Facility {
...
@ManyToOne
@JoinColumn(name="CUSTOMER_FK")
private Customer customer;
...
}
does my @NameQuery like this
@NamedQuery(name="Facility.findByCustomerId", query="select c from Facility c where c.customer=:customer_fk")
or like this
@NamedQuery(name="Facility.findByCustomerId", query="select c from Facility c where c.CUSTOMER_FK=:customer_fk")
You need to stop thinking foreign keys and to start thinking object when using JPQL. Let’s take a closer look:
Does your
Facilityhave aCUSTOMER_FKproperty? No, so the above query is incorrect. Next one:Syntacticly, this one is correct. But you’re still not thinking object. Here, the query expect you to pass an instance of customer, not a FK. I’d thus rewrite it like this (it is the same query but it communicates IMO the intention much better and I’d actually avoid any
foo_fkconvention, you don’t really manipulate FK with JPA):And if you really want to find by id, you should navigate through the association and write: