Java Spring数据存储库(JPA)-使用实体关系查找dbyxxx
附表实体与市场实体以及其他一些“简单”属性存在一对一关系 这是我的ScheduleRepository:Java Spring数据存储库(JPA)-使用实体关系查找dbyxxx,java,spring,Java,Spring,附表实体与市场实体以及其他一些“简单”属性存在一对一关系 这是我的ScheduleRepository: @RepositoryRestResource(path = "schedule") public interface ScheduleRepository extends JpaRepository<Schedule, Long> { Collection<Schedule> findByMarket(Market market); } 在查询字符串中,但
@RepositoryRestResource(path = "schedule")
public interface ScheduleRepository extends JpaRepository<Schedule, Long>
{
Collection<Schedule> findByMarket(Market market);
}
在查询字符串中,但没有结果。不知道您的控制器是什么样子,我假设如果您想在get上通过marketId,它会是什么样子 ?市场D=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);
}
包你好;
导入java.util.List;
导入org.springframework.data.repository.paging和sortingrepository;
导入org.springframework.data.repository.query.Param;
导入org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel=“people”,path=“people”)
公共接口PersonRepository扩展了分页和排序存储库{
列出findByLastName(@Param(“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);
@Query(“从计划s中选择s,其中s.market.marketId=:marketId和s.locale.localeId=:localeId和s.offline>=:按s.placement、s.slot和s.online下单”)
集合findByMarket(@Param(“marketId”)整数marketId、@Param(“localeId”)整数localeId、@Param(“offline”)日期offline);
为什么不使用普通的旧GET参数<代码>?marketId=60它无法将其转换为市场实例,如果我将查找程序命名为“findByMarketId(int-marketId)”,编译器会抱怨。使用原语或任何其他“简单”对象(如字符串)不是问题。但是,由于参数是一个市场对象,它抱怨它无法将字符串转换为该类型。并且您不能使用@Query注释修改findBy,因此您可以通过简单id进行搜索?我还没有尝试过,但我更喜欢在不使用任何注释的情况下解决此问题。主要问题是如何/使用GET从浏览器传递JSON。Http的定义不允许这样做。GET是一个请求,它可以通过头、url中的参数(称为查询参数)接收参数,但从来没有主体(json所在的位置)。我不认为Spring准备将json链接到它的repo。如果是这样的话,编译器应该足够聪明,能够给出一个错误。理论上,我在考虑一个查询字符串,比如?market={marketId:60}或者类似的东西。虽然其他人以前一定遇到过这个问题,希望知道这是否就是问题的解决方式,或者是否有一个优雅的解决方案。我使用@RepositoryRestResource,因此不必编写控制器代码。我最终使用了@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);