[英]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.