如何解决如何基于Spring Data JPA中另一个属性的值返回Map?
我们的模型类如下:
@Data
@Entity
public class Address {
private Long idDL;
private String streetName;
private Long pincode;
private String State;
private String Country;
private Long addressCategory;
private String status;
}
存储库类如下:
@Repository
public interface AddressRepository extends JpaRepository<Address,Long> {
}
当我们在服务层中使用Map<Long,String>
时,我们想返回包含idDL
作为键和pincode
作为值的status='Y'
。
我们不想使用HQL
或NativeQuery
等编写任何显式查询,而想要使用findByStatus
之类的东西。是否有办法在spring data jpa中实现这一点?
解决方法
我认为没有即时的方法可以做到这一点。如果您不想编写任何查询,那么接口中的默认方法可能就足够了:
@Repository
public interface AddressRepository extends JpaRepository<Address,Long> {
public List<Address> findByStatus(String status);
default public Map<Long,Long> getMap() {
return findByStatus("Y").stream().collect(Collectors.toMap(Address::getIdDL,Address::getPincode));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。