0

I have several entities and jpa repositories to them. It looks like:

Event:

public class Event{ 
@Column
private String name;

@Column
private String description;
}

Place:

public class Place{

@Column
private String name;

@Column
private String description;

@Column
private Double lon;


@Column
private Double lat;
}

And repositories to them:

public interface EventRepository extends JpaRepository<Event, String>, JpaSpecificationExecutor<Event> {
}

public interface PlaceRepository extends JpaRepository<Place, String>, JpaSpecificationExecutor<Place> {

}

It work well. But then I added one else field in each entity calls tenantId

Event:

    public class Event{ 

    @Column
    private String tenantId;

    @Column
    private String name;

    @Column
    private String description;
    }

Place:

public class Place{ 

    @Column
    private String tenantId;

    @Column
    private String name;

    @Column
    private String description;

    @Column
    private Double lon;


    @Column
    private Double lat;
    }

But all my service works with method findAll(). So, the question is: How can I get from "old" method findAll() entities only with tenantId = "1" or "2", doesnt matter? It should be work like findAllByTenantId(String tenantId) but it should be 'findAll()'. Can I inject somehow into 'findAll()' tenantId params?

Thx.

1 Answers1

0

Try to override the implementation :

public interface EventRepository extends JpaRepository<Event, String>, JpaSpecificationExecutor<Event> {
    List<Event> findAll(String tenantId);  // findAllByTenantId
}

JPA criteria API translates into the following query:

select e from Event e where e.tenantId = ?1

And

public interface PlaceRepository extends JpaRepository<Place, String>, JpaSpecificationExecutor<Place> {
    List<Place> findAll(String tenantId);   // findAllByTenantId
}

JPA criteria API translates into the following query:

select p from Place p where p.tenantId = ?1
Jimmy
  • 995
  • 9
  • 18