Spring data jpa如何过滤结果的子对象?
@Entity
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@OneToMany(mappedBy = "player")
private List<PlayerItem> playerItems;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<PlayerItem> getPlayerItems() {
return playerItems;
}
public void setPlayerItems(List<PlayerItem> playerItems) {
this.playerItems = playerItems;
}
}
@Entity
public class PlayerItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Boolean begin;
@ManyToOne
@JoinColumn(name = "play_id")
@JsonIgnore
private Player player;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getBegin() {
return begin;
}
public void setBegin(Boolean begin) {
this.begin = begin;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
public interface PlayerRepository extends JpaRepository<Player, Integer> {
List<Player> findByPlayerItemsName(String name);
}
@RestController
public class TestQcController {
@Resource
PlayerRepository repository;
@PostMapping("test")
public Object testQc() {
List<Player> players = repository.findByPlayerItemsName("dada");
return players;
}
}
JSON结果
[{
"id": 1,
"name": "haha",
"playerItems": [{
"id": 1,
"name": "dada",
"begin": false
}, {
"id": 2,
"name": "wawa",
"begin": true
}]
}]
预期:
[{
"id": 1,
"name": "haha",
"playerItems": [{
"id": 1,
"name": "dada",
"begin": false
}]
}]
实际:
[{
"id": 1,
"name": "haha",
"playerItems": [{
"id": 1,
"name": "dada",
"begin": false
}, {
"id": 2,
"name": "wawa",
"begin": true
}]
}]
答案 0 :(得分:0)
@OneToMany
@Where(clause = "name='data'")
private List<PlayerItem> dataPlayerItems;