【问题标题】:Spring rest Jpa query returning Null valueSpring rest Jpa 查询返回 Null 值
【发布时间】:2020-06-20 13:18:22
【问题描述】:

我正在 Jhipster 上构建一个 rest api,它必须使用类别 ID 作为搜索参数返回账单详细信息。对类别端点的调用返回类别列表,但使用类别 id 之一对帐单端点的调用返回空结果。

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


    【解决方案1】:

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

    另外,您的 JOIN 语句中应该是 WHERE c.biller_id = b.id

    【讨论】:

    • 另外,我不知道你为什么用b.categories c而不是categories c
    • 谢谢,您能帮忙查询一下吗?已经有一个 WHERE A c.id = :id 了。
    • 另外,在你的 JOIN 语句中不应该是 WHERE c.biller_id = b.id 吗?
    • 我正在搜索 Category Id,它是 categories 表中的主键,应该是 billers 表中的外键。我注意到 category 表中 Id 列的值与 billers 表中 Id 的值不同
    • 发生这种情况是因为类别表中的 ID 是类别 ID,而 billers 表的 ID 是 billers id。您应该在类别上有 biller_id 以通过任何主键和外键关系加入两个表
    【解决方案2】:

    试试这样的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2018-02-05
      • 1970-01-01
      • 2020-09-22
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多