Reputation: 1860
I have a m:m relationship between EntityOne
and EntityTwo
public class EntityTwo{
private String name;
@ManyToMany //config here
private List<EntityOne> entities;
}
public class EntityOne{
private String description;
@ManyToMany //config here
private List<EntityTwo> entities;
}
How can I create a method for EntityOne
repository without using the @Query
annotation for fetching all the EntityOne
rows based on EntityTwo
's name ? I've tried something like :
List<EntityOne> findAllByEntityTwoName(String name)
but it doesn't work.
Upvotes: 1
Views: 428
Reputation: 3703
You can use Spring data query methods to achieve your task. Basically, spring data generates a query based on some rules you define your method with.
@Repository
public interface EntityRepository extends JpaRepository<EntityOne, UUID> {
List<EntityOne> findAllByEntities_Name(String name);
}
In the findAllByEntities_Name()
method, you are using query method along with Property Expressions to create a search by name property in entites(EntityTwo
) field from EntityOne
Upvotes: 1
Reputation: 2011
Let's say you need to query EntityOne
based on the field name
in EntityTwo
.
You can create a method like this: findAllBy
+ Name of EntityTwo
defined in EntityOne
+ EntityTwo
attribute names, based on which you need to filter
So in you case it would be
List<EntityOne> findAllByEntitiesName(String name);
Upvotes: 1