gpt4 book ai didi

java - 在 JPA 存储库中使用 @MOdify 和 @Query 受哪些规则约束?

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:52 25 4
gpt4 key购买 nike

既然我的项目已经成功完成,我们正在尝试记录经验教训。仍然让我感到困惑的是:

我们有一个地址数据库,需要在用户开始输入街道名称时自动完成。使用 JPA 存储库,我们实现了一个 PString 类(只是 String 的持久包装器),然后实现了此接口(interface):

@RepositoryRestResource(collectionResourceRel = "locations", path = "locations")
public interface LocationRepository extends JpaRepository<Location, Integer>, LocationRepositoryCustom {
List<Location> findByStreetNameAndCommunity_ID(@Param("street") String streetName, @Param("commId") Integer commId);

@Modifying
@Query("select distinct x.streetName from Location x where x.streetName like :street%")
List<PString> findStreetNameStartingWith(@Param("street") String streetName);
}

尝试通过网络调用locations/search/findStreetNameStartingWith?street=N%20College会导致:

{"cause":null,"message":"PersistentEntity 不能为 null!"}

但是,我们添加了一个 Controller 来调用该方法:

@RestController
@RequestMapping("/custom/locations")
public class LocationController {

@Autowired
private LocationRepository repo;

@RequestMapping(value = "/findStreetNamesStartingWith", method=RequestMethod.GET)
public List<PString> findStreetNameStartingWith(
@Param("streetName") String streetName) {

return repo.findStreetNameStartingWith(streetName);
}
}

调用/custom/locations/findStreetNamesStartingWith?streetName=N%20Coll 返回预期的三个结果。为什么如果直接调用该方法不起作用,但当我们通过 Controller 传递它时却像灰狗一样运行?

最佳答案

确保您配置了Spring Data REST正确地,例如添加 RepositoryRestConfiguration:

@Configuration
public class CustomizedRestMvcConfiguration extends RepositoryRestMvcConfiguration {

@Override
public RepositoryRestConfiguration config() {
RepositoryRestConfiguration config = super.config();
config.setBasePath("/custom");
return config;
}
}

关于java - 在 JPA 存储库中使用 @MOdify 和 @Query 受哪些规则约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com