繁体   English   中英

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

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

Schedule实体与Market实体以及其他一些“简单”属性具有一对一关系。

这是我的ScheduleRepository:

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

以编程方式调用该方法时,“ findByMarket”方法可以正常工作。 但是,当直接从Web应用程序( http:// localhost:8080 / schedule / search / findByMarket调用时 ,请求类型必须为GET。

我的问题是如何使用GET传递Market JSON对象? 使用POST不会有问题,但是findXxx方法必须使用GET。 我试图通过类似的东西:

?market={marketId:60}

在查询字符串中,但无济于事。

不知道您的控制器是什么样子,我会假设,如果您想在get上传递marketId,它将看起来像。

?marketId = 60

和您的方法看起来像。 您使用的方法将处理与JSON之间的转换。

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

在文档中,引用了使用@Param批注,因此您可以通过提供查询参数来调用rest服务。 这里有一个例子:

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);

}

我最终使用了@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM