2015-04-14 72 views
0

计划实体与市场实体以及一些其他“简单”属性具有一对一关系。弹簧数据存储库(JPA) - 使用实体关系的findByXXX

这里是我的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传递市场JSON对象?使用POST不会是一个问题,但findXxx方法必须使用GET。我尝试通过类似的东西:

?market={marketId:60} 

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

+0

为什么不使用普通的旧GET参数? '?marketId = 60' –

+0

它无法将其转换为市场实例,如果我将取景器命名为“findByMarketId(int marketId)”,编译器将会抱怨。 – Jonathan

回答

0

不知道你的控制器是什么样的,我会假设如果你想通过marketId得到它看起来像。

?marketId = 60

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

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

我正在使用@RepositoryRestResource,所以我不必编写控制器。 – Jonathan

+0

我最终使用了@Query注释,就像jdepedro建议的那样: – Jonathan

0

在文档中,参考使用@Param注释,这样你就可以打电话给你休息的服务给查询参数。 Here你有一个例子:

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

} 
+0

使用一个原语或任何其他“简单”对象(如字符串)不是问题。但是,由于该参数是Market对象,因此它会抱怨它无法将字符串转换为该类型。 – Jonathan

+1

而且您无法使用@Query注释修改find​​By,因此您可以通过简单的ID进行搜索? – droidpl

+0

还没有尝试过,但我更喜欢在没有任何注释的情况下解决问题。主要的问题是如何使用GET从浏览器传递JSON。 – Jonathan

0

我结束了使用@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);