Reputation: 8043
I have a question about hibernate @ManyToMany relationship. First, suppose I have User
and Position
with ManyToMany
relationship. Here is Position
collection in the User
:
@JoinTable(name = "user_position", joinColumns = {
@JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "position_id", referencedColumnName = "id")
})
@ManyToMany(fetch = FetchType.LAZY)
@OrderBy("id ASC")
private Collection<Position> positionCollection;
I have used @OrderBy("id ASC")
, so when I call user.getPositionCollection()
, the result will be sort on id
. Now my question is how can I add condition to collection? For example something like this:
@OrderBy("id ASC")
@Condition("id > 10")
private Collection<Position> positionCollection;
Edit:
I tested @Where
annotation in this way(Note deleted
is boolean):
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@Where(clause = "deleted = false")
private Collection<Position> positionCollection;
This returns me an error Unknown column 'positi0_.false' in 'where clause'
Upvotes: 2
Views: 2329
Reputation: 89
the @Where annotation takes native SQL as a string, so assuming your boolean field is stored in your database as an int or bit, you would need to do @Where(clause = "deleted = 0")
Upvotes: 0
Reputation: 383
Your problem is that false is being recognized as a column name.
Try @Where(clause = "deleted = 'false'")
Good luck!
Upvotes: 1
Reputation: 23562
I am not aware of such a config. But you can take a look at Hibernate filters.
Upvotes: 1