![](/img/trans.png)
[英]Setting NULL value in UPDATE query of Native JPA in Spring data
[英]Spring rest Jpa query returning Null value
我正在 Jhipster 上構建一個 rest api,它必須使用類別 ID 作為搜索參數返回賬單詳細信息。 對類別端點的調用返回類別列表,但對使用類別 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
我已經研究了好幾天並進行了頭腦風暴,除了建設性和重建性之外,我會感謝所有投入。 謝謝
您在存儲庫中傳遞@Param("id")
但實際上並未在 SQL 查詢中使用該 ID。 您的where
條件只有一個 join 語句。 您需要在加入表格后添加AND c.id = :id
以便您可以通過類別 id 提及您想要獲取的類別。
另外,它應該是您的 JOIN 語句中的WHERE c.biller_id = b.id
嗎?
嘗試這樣的事情
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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.