How does spring data jpa filter sub-objects?

has not been solved yet. I found a similar problem on https://stackoverflow.com/que... on stackoverflow. I tried it myself and found that it could not filter the subresult set, but only parent. The filtered parent brought out all the sub-objects,

.
@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
    }]
}]

my query condition is that the child object name equal to = "dada", only restricts the parent object, but the child object list does not restrict according to this condition

what should I do if I want to restrict sub-objects?


your repository is the returned List < Player >, and the program returns the Player list, and then you take the playerItem of the OneToMany under the player, which is all the associated playerItems under this player, and the program runs smoothly.

the effect you want can't be written in this way. You have to select all the name from the PlayerItem side to be the List of data < PlayerItem >, and then set into the player.

Menu