Spring rest Jpa查询返回Null值

问题描述 投票:-1回答:2

我正在Jhipster上构建一个rest api,该api必须使用类别ID作为搜索参数来返回开票人详细信息。对categorys端点的调用返回的是类别列表,但是使用category id之一的billers端点的调用返回的是空结果。

public interface ApplicationUrl {

String BILLERS = "/category/{categoryid}";

}

这是控制器:

@RequestMapping(ApplicationUrl.BASE_CONTEXT_URL)
public class BillingGatewayController {


    @Autowired
    private BillingGatewayService billingGatewayService;

@GetMapping(ApplicationUrl.BILLERS)
    public BillersServiceResponse getAllBillersByCatId(@PathVariable Long categoryId) {
        BillersServiceResponse defaultServiceResponse = new BillersServiceResponse();
        defaultServiceResponse.setMessage(Message.fail.name());
        ResponseCode responseCode = ResponseCode.BILLER_NOT_AVAILABLE;
        log.debug("REST request to get all billers");
        List<BillerDto> billers = billingGatewayService.findBillers(categoryId);
       if (CollectionUtils.size(billers) > 0) {
            responseCode = ResponseCode.SUCCESSFUL;
            defaultServiceResponse.setStatus(responseCode.getCode());
            defaultServiceResponse.setMessage(Message.SUCCESSFUL.name());
            defaultServiceResponse.setData(billers);
        }
        defaultServiceResponse.setStatus(responseCode.getCode());
        return defaultServiceResponse;
    }
}

这是服务类别:

public interface BillingGatewayService {
List<BillerDto> findBillers(Long id);

}
public interface BillersRepository extends JpaRepository<Billers, Long>, JpaSpecificationExecutor<Billers> {

}

@Service("billingGatewayService")
public class BillingGatewayServiceImpl implements BillingGatewayService {

@Autowired
    private ExtBillersRepository billersRepository;

@Override
    public List<BillerDto> findBillers(Long categoryId) {
        BillerResponseDto billerResponseDto = new BillerResponseDto();
        List<BillerDto> billers = billersRepository.findAllActiveBillers(categoryId);
        billerResponseDto.setBillers(billers);
        billerResponseDto.setCategoryId(String.valueOf(categoryId));
        return billers;
      }
}
import com.fms.payfuze.dto.BillerDto;
import com.fms.payfuze.repository.BillersRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;


public interface ExtBillersRepository extends BillersRepository {

    String ACTIVE_BILLERS = "select new com.fms.payfuze.dto.BillerDto(b.id, b.name) from Billers b inner join b.categories c where c.id=b.id order by b.name";

    @Query(ACTIVE_BILLERS)
    List<BillerDto> findAllActiveBillers(@Param("id") Long id);
}

这是billerDTO:

public class BillerDto {

    private String billerId;

    private String nameOfBiller;

    public BillerDto(Long id, String name) {

        this.billerId = String.valueOf(id);
        this.nameOfBiller = name;
    }

    public String getBillerId() {
        return billerId;
    }

    public void setBillerId(String billerId) {
        this.billerId = billerId;
    }

    public String getNameOfBiller() {
        return nameOfBiller;
    }

    public void setNameOfBiller(String nameOfBiller) {
        this.nameOfBiller = nameOfBiller;
    }


}

这是Billers类别:

@Entity
@Table(name = "billers")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Billers implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "active")
    private Boolean active;

    @Column(name = "date_created")
    private Instant dateCreated;

    @OneToOne
    @JoinColumn(unique = true)
    private BillersRouterConfig billersRouterConfig;

    @OneToMany(mappedBy = "billers")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<TransactionDetails> billers = new HashSet<>();

    @ManyToMany(mappedBy = "billers")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @JsonIgnore
    private Set<Categories> categories = new HashSet<>();


Getters and setters 


我已经参加了好几天,并且头脑风暴,除了建设性和重建性的工作,我将不胜感激。谢谢

java postgresql spring-boot spring-data-jpa jhipster
2个回答
2
投票

您正在存储库中传递@Param("id"),但实际上并未在SQL查询中使用该ID。您的where条件只有一个join语句。加入表后,需要添加AND c.id = :id,以便您可以通过类别ID提及要获取的类别。

还应该在您的JOIN语句中为WHERE c.biller_id = b.id吗?


0
投票

尝试这样的事情

String ACTIVE_BILLERS = 
"select new com.fms.payfuze.dto.BillerDto(b.id, b.name)
 from Billers b inner join 
 b.categories c 
 where c.billers_id = b.id and c.billers_id = :id
 order by b.name";
© www.soinside.com 2019 - 2024. All rights reserved.