按标准进行动态搜索 [英] Dynamic search by criteria
问题描述
我正在使用 QueryDSL
和 Spring Data Jpa
,我想执行一些动态搜索。
我遵循这个回答,并且使用 BooleanBuilder
但在我的情况下,我必须进行连接。
所以我如何让它在 player ,
player_team
,团队
,并且我有关于球员姓名和球队名称的可选参数?
________ ___________________ _______
|玩家| | player_team | |团队|
| ------ | | ---------------- | | ------- |
| id | | player_team_id(pk)| | id |
|名称| | player_id(fk)| |名称|
------ | team_id(fk)| -------
-----------
player.java
@Entity
@Table(...)
public class Player implements Java .io.Serializable {
private Integer idPlayer;
私人字符串namePlayer;
私人设置< PlayerTeam> player_teams = new HashSet< PlayerTeam>(0);
...
}
team.java
@Entity
@Table(...)
public class Team实现java.io.Serializable {
private Integer idTeam;
private String nameTeam;
私人设置< PlayerTeam> player_teams = new HashSet< PlayerTeam>(0);
...
}
player_team.java
@Entity
@Table(...)
公共类PlayerTeam实现java.io.Serializable {
private Integer idPlayerTeam;
私人团队;
私人球员paleyr;
...
}
对于每个domaine,我都有类似的仓库:
public interface PlayerRespository扩展JpaRepository< Player,Integer>,QueryDslPredicateExecutor< Player> {
}
你不会把额外的属性放到PlayerTeam中,它不应该被建模为一个实体。关于它的条件是
player.namePlayer.eq(...)
和
新的JPASubQuery()。from (playerTeam)
.where(playerTeam.player.eq(player),palyerTeam.team.name.eq(...))
.exists()
I am using QueryDSL
with Spring Data Jpa
and i want execute some dynamic search.
I follow this Answer and it's okey with BooleanBuilder
But in my case I have to make joins.
So how can i make it if i have 3 joins on player
, player_team
, team
and i have optional parameters on the name of player and name of his team ?
________ ___________________ _______
| player | | player_team | | team |
|------ | |---------------- | |-------|
| id | | player_team_id (pk) | | id |
| name | | player_id (fk) | | name |
------ | team_id (fk) | -------
-----------
player.java
@Entity
@Table(...)
public class Player implements java.io.Serializable {
private Integer idPlayer ;
private String namePlayer;
private Set<PlayerTeam> player_teams = new HashSet<PlayerTeam>(0);
...
}
team.java
@Entity
@Table(...)
public class Team implements java.io.Serializable {
private Integer idTeam ;
private String nameTeam;
private Set<PlayerTeam> player_teams = new HashSet<PlayerTeam>(0);
...
}
player_team.java
@Entity
@Table(...)
public class PlayerTeam implements java.io.Serializable {
private Integer idPlayerTeam ;
private Team team;
private Player paleyr;
...
}
and for each domaine i have respository like that :
public interface PlayerRespository extends JpaRepository<Player, Integer>, QueryDslPredicateExecutor<Player> {
}
If you don't put extra properties into PlayerTeam it shouldn't be modeled as an entity. Concerning the conditions it would be
player.namePlayer.eq(...)
and
new JPASubQuery().from(playerTeam)
.where(playerTeam.player.eq(player), palyerTeam.team.name.eq(...))
.exists()
这篇关于按标准进行动态搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!