Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
    Bookmark Topic Watch Topic
  • New Topic

how to write the jpql to query against the one to many table

 
Peter Saw
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
I asked the question in my last post but not sure why no reply.
So I post it again as an standalone question.

I have 2 tables Customer and Contact. One customer can have multiple contacts.
So the relationship is one to many.

Part of Customer Entity:
@OneToMany(cascade = CascadeType.REFRESH, mappedBy="customer", fetch = FetchType.EAGER)
private Set<Contact> contacts;

Part of Contact Entity:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_ID")
private Customer customer;

@Column(name = "FIELD_NAME")
@NotNull
private String fieldName;

@Column(name = "FIELD_VALUE")
@Length(max = 100, message = "Maximum length allowed for user defined field is 100")
private String fieldValue;

Now I want to search out the customers based on the contact criterias.
For example, the criteria is fieldName='home number' and fieldValue='1234'

How to write such JPQL?
It could looks like,
select c from Customer c where c.contacts.fieldName='home number' and c.contacts.fieldValue='1234'
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question more than once.
 
    Bookmark Topic Watch Topic
  • New Topic