简体   繁体   中英

Spring rest Jpa query returning Null value

I am building a rest api on Jhipster that must return the billers details using a Category ID as the search parameter. The call to the categories endpoint is returning list of categories but the call to the billers endpoint using one of the category id is returning a null result .

public interface ApplicationUrl {

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

}

This is the controller :

@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;
    }
}

This is the service classes :

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);
}

This is the 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;
    }


}

and this is the Billers class:

@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 


I have been on it for days and been brain storming , I'll appreciate all inputs but constructive and reconstructive . Thanks

You're passing @Param("id") in your repository but you are not actually using that id in your SQL Query. Your where condition only has a join statement. You need to add AND c.id = :id after joining the table so you can mention which category you want to get by category id.

Also, it should be WHERE c.biller_id = b.id in your JOIN statement?

Try something like this

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM