【发布时间】:2021-12-09 16:33:59
【问题描述】:
我必须在通过 POST 请求发送的address_table 中返回与其 City 对应的 State。我在查询中使用了WHERE 子句,但它需要一个字符串。如果我希望它包含从AddressManager 中的方法返回的城市,我该怎么办?
如果作为发布请求发送的城市是我在WHERE a.city = '' 中输入的内容,则此代码有效
存储库
@Repository
public interface AddressRepository extends JpaRepository<Address, Long>
{
@Query("SELECT a.state FROM address_table a WHERE a.city = '' ")
String getState();
// the blank city should be the city from user.setCity (ua.getCity())
}
经理
@Component
public class AddressManager
{
@Autowired
private AddressRepository aRepo;
@Autowired
private UserRepository uRepo
public void save(UserAddressDto ua)
{
User user = new User();
user.setCity (ua.getCity());
user.setState(aRepo.getState());
uRepo.save(user);
}
}
【问题讨论】:
-
我认为在您的
@Query中,您正在混合 jpa 和本机 SQL。你要写什么样的声明?如果是 JPA,您还应该发布实体代码,例如Address、City等。如果它是一个 SQL 查询,你应该发布你的数据库表结构,例如address_table和城市表
标签: java spring-data-jpa jpql