Am的user&user_dealer表具有一对多的关系。请找到下表结构。
用户表
id email username
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
679 test@test.com sathis
680 test@test.com ram
680 test@test.com ram
680 test@test.com ram
681 test@test.com Steve
681 test@test.com Steve
681 test@test.com Steve
681 test@test.com Steve
681 test@test.com Steve
user_dealer表
user_id dealer_id
679 J039
679 J080
679 J320
679 J54L
679 K31P
679 L05C
679 T030
679 V150
680 J039
680 J080
680 J320
681 ABC12
681 CD123
681 opo123
681 qw123
681 ieu12
如果我给出用户名(来自用户表),那么它应该检查代码(来自user_dealer表),同一个Dealer_id应该检查是否为其他user_id(来自user_dealer表)提供了该用户名,并且还应返回该user_ids
例如。
id username
679 sathis
680 ram
上表中的,如果我输入sathis作为输入。我应该得到ram作为输出结果。因为Sathis的Dealer_id也向Ram展示了。
请找到用户的模型类
@Getter
@Setter
@Entity
@Table(name = "users")
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = {"username"})
@ValidDsdPermissions
@ValidApprovalPrivileges
public class User implements Identifiable<Long>, LazilyLoadable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("userLink")
private Long id;
@NotEmpty(message = "{error.model.user.attribute.username.required}")
@Column(unique = true, nullable = false)
private String username;
@NotEmpty(message = "{error.model.user.attribute.email.required}")
@Email(message = "{error.model.user.attribute.email.format.invalid}")
@Column(nullable = false)
private String email;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "user_dealers", joinColumns = @JoinColumn(name = "user_id"))
@BatchSize(size=20)
@JsonSerialize(using = DealersSerializer.class)
@JsonDeserialize(using = DealersDeserializer.class)
private Set<Dealer> dealers = new HashSet<Dealer>();
}
请找到经销商的型号类别
@Getter
@Setter
@NoArgsConstructor
@Embeddable
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode
public class Dealer {
@Column(name = "dealer_id")
private String dealerId;
@Column(name = "dsd_id")
private Long dsdId;
}
我尝试使用简单的SQL查询及其按预期的工作方式。请在下面找到查询。
select id, username
from security_development.users
inner join (
select distinct user_id
from security_development.user_dealers
inner join (
select dealer_id
from security_development.user_dealers
inner join security_development.users ON security_development.user_dealers.user_id = security_development.users.id
where username = 'sathis'
) t on t.dealer_id = security_development.user_dealers.dealer_id
) t2 on t2.user_id = security_development.users.id
请找到下面的休眠查询,其中未考虑/比较dealer_id
(应该进行比较)
"SELECT DISTINCT u FROM User u INNER JOIN u.dealers d, User v INNER JOIN v.dealers e WHERE v.username = :username AND u.role.id in (3,2) AND d.dealerId = e.dealerId"
如何将上述普通sql查询转换为Hibernate查询?