我有以下型号;
这是UserEntity类:
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;
}
...
}
还有RuleEntity类:
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;
}
...
}
TeamEntity类:
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;
}
...
}
所以,鉴于我创建了以下模型:
/**
* <pre>
* Team ("team1")
* |
* Rule ("rule1")
* | |
* User ("john")
* </pre>
*/
/**
* <pre>
* Team ("team4")
* |
* Team ("team5")
* | |
* Rule ("rule4") Rule ("rule5")
* | |
* User ("paul") User("john")
* </pre>
*/
我正在尝试实施一种搜索,用户可以使用用户名,团队名称和规则名称进行搜索,因此3必须匹配才能返回任何结果。我目前有以下内容返回符合3个搜索字词的用户:
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
因此,使用上面的HQL查询,我希望结果不返回任何用户实体(因为没有team5,其中有rule1,其中rule1中包含john),而是返回'John'< / p>
有没有人知道如何调整上面的查询,以便它像我所描述的那样工作(即它只应在所有3个匹配时返回结果)?
答案 0 :(得分:3)
好的,这些是我对此的想法:
您有3个表:用户,规则和团队。 第一个表包含paul和john。 第二个表包含rule1,rule4,规则5.
这些表的连接返回john - rule1;约翰 - 规则5; (JOIN1)
第三个表包含team1,team4和team 5。 现在,您可以使用此表加入用户。 你得到john-team1; john-team 5.(JOIN2)
现在你要问USERS中有一个john(是)JOIN1中的rule1(是)和JOIN2中的team5(是)。你去吧。
您需要做的是加入团队规则。
从UserEntity中选择不同的用户作为用户
内部加入users.teams作为团队
内部联接user.rules作为规则1
内部联合teams.rules作为规则2
其中users.username如:john
和rules1.ruleName类似:rule1
和rules2.ruleName喜欢:rule1
和teams.name一样:team5。
但是在用户和团队中都有规则,这很奇怪。我会重新考虑这一点。
答案 1 :(得分:0)
@ManyToOne(nullable=true)
NOT @ManyToMany
select distinct [someCollection]
没有意义(需要select distinct [someEntity]
)