I have two entities:
User
(it has fieldString userName
);School
(it has fieldLong id
);
In School
I have:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = User.class)
@JoinColumn(name = "schoolId")
private List<User> users;
In User
I have:
@ManyToOne(optional = false, targetEntity = School.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "schoolId", referencedColumnName = "id", nullable = false)
private School school;
I also have SchoolRepository
:
public interface SchoolRepositoryextends CrudRepository<School, Long> {
}
And I want to make a request to database by JPA
query method names. For example, I want to find all users with name John
in school with id = 1
.
How can I do this via SchoolRepository
?
The only thing that comes to my mind is findByIdAndUsersUserName
, but it gives me the exception that there's no field named users
in my schools
table.
NOTE:
of course, it is possible via @Query
but I wonder if there's a way to do it via method names
.