I have the following model;
- I have users and rules and teams
- a user can be added to 0, 1 or more rules
- a user can be added to 0, 1 or more teams
- a rule can be added to one team only, but a team can contain many distinct rules
- a rule can contain 0, 1 or more users
Here is the UserEntity class:
class UserEntity {
private String username;
private List<TeamEntity> teams;
private List<RuleEntity> rules;
@Column(name = "username", nullable = false, unique = true)
public String getUsername() {
return username;
}
@ManyToMany(mappedBy="users" , fetch = FetchType.LAZY)
public List<RuleEntity> getRules() {
return rules;
}
@ManyToMany(mappedBy="users" , fetch = FetchType.LAZY)
public List<TeamEntity> getTeams() {
return teams;
}
...
}
And the RuleEntity class:
class RuleEntity {
private String name;
private List<UserEntity> users;
private TeamEntity ownerTeam;
@Column(name = "name", nullable = false)
public String getRuleName() {
return ruleName;
}
@ManyToOne
@JoinColumn(name=TeamEntity.TEAM_ID, nullable = false)
public TeamEntity getOwnerTeam() {
return ownerTeam;
}
@ManyToMany (fetch = FetchType.LAZY)
@JoinTable(name= "RULE_USER" ,joinColumns=@JoinColumn
(name=RuleEntity.RULE_ID, referencedColumnName="ID", insertable = true, updatable = false, nullable = false),
inverseJoinColumns=@JoinColumn
(name=UserEntity.USER_ID, referencedColumnName="ID", insertable = true, updatable = false, nullable = false),
uniqueConstraints = @UniqueConstraint(columnNames = {RuleEntity.RULE_ID, UserEntity.USER_ID}))
public List<UserEntity> getUsers() {
return users;
}
...
}
And the TeamEntity class:
class TeamEntity {
private String name
private List<UserEntity> users;
private List<RuleEntity> rules;
@ManyToMany (fetch = FetchType.LAZY)
@JoinTable(name= TeamEntity.TEAM_USER_TABLE,joinColumns=@JoinColumn(name=TeamEntity.TEAM_ID, referencedColumnName="ID",
insertable = true, updatable = false, nullable = false),
inverseJoinColumns=@JoinColumn(name=TeamEntity.USER_ID, referencedColumnName="ID",
insertable = true, updatable = false, nullable = false),
uniqueConstraints = @UniqueConstraint(columnNames = {TeamEntity.TEAM_ID, TeamEntity.USER_ID}))
public List<UserEntity> getUsers() {
return users;
}
@OneToMany (mappedBy = "ownerTeam", cascade = {CascadeType.ALL}, orphanRemoval=true)
public List<RuleEntity> getRules() {
return rules;
}
...
}
So, given I have the following model created:
/**
* <pre>
* Team ("team1")
* |
* Rule ("rule1")
* | |
* User ("john")
* </pre>
*/
/**
* <pre>
* Team ("team4")
* |
* Team ("team5")
* | |
* Rule ("rule4") Rule ("rule5")
* | |
* User ("paul") User("john")
* </pre>
*/
I am trying to implement a search whereby a user can search using a username, a team name and a rule name, i..e so the 3 have to match in order to return any results. I currently have the following to return users matching the 3 search terms:
select distinct users from UserEntity as users inner join users.rules as rules inner join users.teams as teams where users.username like :john and rules.ruleName like :rule1 and teams.name like :team5
So using the HQL query above, I would expect the result to return no user entities (because there is no team5 with a rule1 in it, where the rule1 has john in it), but instead it is returning 'John'
Does anyone know how to tweak the above query so that it works as I've described (i.e. it should only return a result when all 3 match)?