简体   繁体   English

Spring Data Repository(JPA)-使用实体关系的findByXXX

[英]Spring Data Repository (JPA) - findByXXX using entity relation

A Schedule entity has a one to one relationship with a Market entity as well as some other "simple" properties. Schedule实体与Market实体以及其他一些“简单”属性具有一对一关系。

Here is my ScheduleRepository: 这是我的ScheduleRepository:

@RepositoryRestResource(path = "schedule")
public interface ScheduleRepository extends JpaRepository<Schedule, Long>
{
    Collection<Schedule> findByMarket(Market market);
}

"findByMarket" method works fine when invoking the method programmatically. 以编程方式调用该方法时,“ findByMarket”方法可以正常工作。 However, when invoking directly from a web application ( http://localhost:8080/schedule/search/findByMarket ), the request type must be GET. 但是,当直接从Web应用程序( http:// localhost:8080 / schedule / search / findByMarket调用时 ,请求类型必须为GET。

My question is how do I pass a Market JSON object using GET? 我的问题是如何使用GET传递Market JSON对象? Using POST wouldn't be an issues but findXxx methods must use GET. 使用POST不会有问题,但是findXxx方法必须使用GET。 I tried passing something like: 我试图通过类似的东西:

?market={marketId:60}

in the querystring but to no avail. 在查询字符串中,但无济于事。

Not knowing what your controller looks like, I would assume that if you wanted to pass marketId on a get it would look like. 不知道您的控制器是什么样子,我会假设,如果您想在get上传递marketId,它将看起来像。

?marketId=60 ?marketId = 60

And your method would look like. 和您的方法看起来像。 The method you use will handle converting to and from JSON. 您使用的方法将处理与JSON之间的转换。

@Get
@Path("/query")
@Produces({"application/xml", "application/json"})
public Todo whatEverNameYouLike(@PathParam("marketId") String marketId)

In the documentation it is referenced to use the @Param annotation, so you can call to your rest service giving a query parameter. 在文档中,引用了使用@Param批注,因此您可以通过提供查询参数来调用rest服务。 Here you have an example: 这里有一个例子:

package hello;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

I ended up using a @Query annotation, like jdepedro suggested: 我最终使用了@Query注释,例如jdepedro建议:

@Query("select s from Schedule s where s.market.marketId = :marketId and s.locale.localeId = :localeId and s.offline >= :offline order by s.placement, s.slot, s.online")
    Collection<Schedule> findByMarket(@Param("marketId") Integer marketId, @Param("localeId") Integer localeId, @Param("offline") Date offline);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data / Neo4J存储库:findByXXX返回Map,而不是Entity - Spring Data / Neo4J Repository : findByXXX returns Map, not Entity 我可以使用 Spring Data JPA 处理多个实体类的单个 JPA 存储库接口吗? - Can I have a single JPA repository interface handling multiple entity classes using Spring Data JPA? Spring 引导数据 JPA 进行延迟加载 - 不是在关系上而是在加载的实体上? - Spring Boot Data JPA doing lazy loading - not on a relation but on the loaded entity? 在Spring Data JPA存储库中使用自定义名称 - Using custom names in a Spring Data JPA Repository 在Spring Data JPA Repository中使用EntityGraph进行过滤 - Filtering using EntityGraph in Spring Data JPA Repository Spring-Data-Jpa 存储库 - 实体列名称上的下划线 - Spring-Data-Jpa Repository - Underscore on Entity Column Name Spring Data JPA存储库返回父类的实体 - Spring data JPA repository returns entity of parent class 如何使用 Spring JPA 存储库按多个字段过滤实体? - How to filter an entity by multiple fields using a Spring JPA Repository? Spring数据jpa存储库注入失败,在Spring启动时使用@Autowired - spring data jpa repository inject fails using @Autowired in spring boot JPA 存储库找不到与当前实体相关的现有实体 - JPA Repository does not find existing entity with relation to the current entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM