JPA:将默认参数传递给findAll()方法

时间:2016-02-04 10:21:13

标签: java hibernate jpa

我有几个实体和jpa存储库。它看起来像:

事件:

public class Event{ 
@Column
private String name;

@Column
private String description;
}

地点:

公共课地点{

@Column
private String name;

@Column
private String description;

@Column
private Double lon;


@Column
private Double lat;
}

和他们的存储库:

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

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

}

效果很好。但后来我在每个实体调用tenantId

中添加了一个else字段

事件:

    public class Event{ 

    @Column
    private String tenantId;

    @Column
    private String name;

    @Column
    private String description;
    }

地点:

public class Place{ 

    @Column
    private String tenantId;

    @Column
    private String name;

    @Column
    private String description;

    @Column
    private Double lon;


    @Column
    private Double lat;
    }

但我的所有服务都使用方法findAll()。所以,问题是: 如何从“旧”方法findAll()实体获取只有tenantId =“1”或“2”,无所谓?它应该像findAllByTenantId(String tenantId)一样工作但它应该是'findAll()'。我能以某种方式注入'findAll()'tenantId params吗?

THX。

1 个答案:

答案 0 :(得分:0)

尝试覆盖实现:

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

JPA 条件 API 转换为以下查询:

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

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

JPA 条件 API 转换为以下查询:

select p from Place p where p.tenantId = ?1