1

Recently, I have started learning/using Spring Boot and came across the concept of query building from method names. I want to know how a method name is translated to a query string and how all the boilerplate code communicating with a database is created by Spring, still a newbie here so these things get me excited. Tried the documentation but it only says how to use them and what the keywords are. I am talking namely about the Repository and CrudRepository interfaces.

Let's take the following code for example:

@Entity
public final class Team implements Serializable {

    @Id
    @Column(name = "TEAM_ID")
    private Long id;

    @NotNull
    private String teamName;

    @OneToMany(mappedBy = "team")
    private Set<Player> setOfPlayers;

    //getters and setters
}

@Entity
public final class Player implements Serializable{

    @Id
    private Long playerId;

    @NotNull
    private String playerName;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "TEAM_ID", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Team team;

    //getters and setters
}


public interface PlayerRepository extends CrudRepository<Player, Long> {

Player findByPlayerId(Long id);
List<Player> findByTeamOrderByPlayerName(Long teamId);

}

What I want to understand is how Spring is making use of the keywords such as FindBy orderBy and how builds a query finds the corresponding tables and executes it and how any Spring mappings involved work. Also, how relationships between entities are handled.

Pardon me, if my question is formulated badly or too broad, I tried looking at the documentation but couldn't find answer to my questions. Any reference where I can read about all that or/and the actual implementation of the query build functionality would be a great help!

Thanks !

0 Answers0