Hibernate 如何基于Spring数据JPA中另一个属性的值返回映射?
我们的模型课是这样的:Hibernate 如何基于Spring数据JPA中另一个属性的值返回映射?,hibernate,jpa,spring-data-jpa,spring-data,Hibernate,Jpa,Spring Data Jpa,Spring Data,我们的模型课是这样的: @Data @Entity public class Address { private Long idDL; private String streetName; private Long pincode; private String State; private String Country; private Long addressCategory; privat
@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> {
}
存储库类类似于:
@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> {
}
@存储库
公共接口地址存储库扩展了JpaRepository{
}
当我们的服务层中出现status='Y'
时,我们希望将包含idDL
的Map
作为键和pincode
作为值返回。
我们不想使用
HQL
或NativeQuery
等编写任何显式查询,而是想使用findByStatus
之类的东西。在spring数据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));
}
}
@存储库
公共接口地址存储库扩展了JpaRepository{
公共列表findByStatus(字符串状态);
默认公共映射getMap(){
返回findByStatus(“Y”).stream().collect(Collectors.toMap(Address::getIdDL,Address::getPincode));
}
}
否,无法使用方法查询创建。即使使用JPQL,我也不认为你很难得到List