Reputation: 589
I am trying to build a query with Hibernate Criteria.
I have two tables (Table A and Table B) and they are MANY - TO - MANY connected.
Table B has a property, id, that I would like to query to filter for Table A.
So for example :
This is my TableA class referencing TableB:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinTable(name = "tableb", joinColumns = {@JoinColumn(name = DATABASE_COLUMN_ID, nullable = false, updatable = false) }, inverseJoinColumns = {@JoinColumn(
name = TABLE_B, nullable = true, updatable = false) })
private Set<TableB> tableB;
I want to query all Table A´s that have one Table B with the id of 123.
The code that I wanted to write was something like this:
criteria.add(Restrictions.eq("name", "some name"));
...
criteria.add(Restrictions.eq("tableB.id", "123"));
Unfortunately I do get an Exception like this:
Caused by: org.hibernate.QueryException: could not resolve property: tableB.id of: TableA.
Who knows what I did wrong ?
Upvotes: 0
Views: 433
Reputation: 270
You can create alias on tableB and then use the criteria. Something like criteria.createAlias("tableB", "tableb");
then use criteria.add(Restrictions.eq("tableb.id", "123"));
Upvotes: 1