【发布时间】:2021-09-15 08:30:50
【问题描述】:
我在 JPA Query 中添加参数时遇到问题 我的订单实体:
@Entity
@Table(name = "orderbill")
public class OrderEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "quantity")
private int quantity;
@Column(name = "timebought")
private Date timebought;
@Column(name = "totalcost")
private Float totalCost;
@Column(name = "address")
private String address;
@Column(name = "ratenum")
private Integer rateNum;
@Column(name = "ratetext")
private String rateText;
@Column(name = "status")
private boolean status;
@ManyToOne
@JoinColumn(name = "productid")
private ProductEntity products;
@ManyToOne
@JoinColumn(name = "customeremail")
// @JsonBackReference
private PersonEntity customers;
public OrderEntity() {
super();
}
public OrderEntity(OrderDTO order) {
super();
this.id = order.getId();
this.quantity = order.getQuantity();
this.timebought = order.getTimebought();
this.totalCost = order.getTotalCost();
this.address = order.getAddress();
this.rateNum = order.getRateNum();
this.rateText = order.getRateText();
this.status = order.isStatus();
this.products = new ProductEntity(order.getProducts());
this.customers = new PersonEntity(order.getCustomers());
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ProductEntity getProducts() {
return products;
}
public void setProducts(ProductEntity products) {
this.products = products;
}
public PersonEntity getCustomers() {
return customers;
}
public void setCustomers(PersonEntity customers) {
this.customers = customers;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
if (quantity < 0) {
throw new IllegalArgumentException("Quantity must not below zero");
}
this.quantity = quantity;
}
public Date getTimebought() {
return timebought;
}
public void setTimebought(Date timebought) {
this.timebought = timebought;
}
public float getTotalCost() {
return totalCost;
}
public void setTotalCost(float totalCost) {
if (totalCost < 0) {
throw new IllegalArgumentException("Total must not below zero");
}
this.totalCost = totalCost;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getRateNum() {
return rateNum;
}
public void setRateNum(int rateNum) {
if (rateNum < 0) {
throw new IllegalArgumentException("Rate number must not below zero");
}
this.rateNum = rateNum;
}
public String getRateText() {
return rateText;
}
public void setRateText(String rateText) {
this.rateText = rateText;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
我有一个这样的存储库:
@Repository
public interface OrderRepository extends JpaRepository<OrderEntity, Integer> {
@Query(nativeQuery = true, value="select o from OrderEntity o where o.customers.getEmail() = :email")
List<OrderEntity> findByCustomers(@Param("email")String email);
}
我用它
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
OrderRepository orderRepository;
public List<OrderEntity> findOrderByCustomer(String email) {
PersonEntity person = personService.getPerson(email);
List<OrderEntity> orderList = orderRepository.findByCustomers(person.getEmail());
if (orderList.isEmpty())
throw new ObjectNotFoundException("Could not find any order bought by email: " + email);
return orderList;
}
但我得到的是
Hibernate: select userentity0_.email as email1_1_, userentity0_.fullname as fullname4_1_, userentity0_.password as password6_1_, userentity0_.role as role8_1_ from persons userentity0_ where userentity0_.email=?
Hibernate: select userentity0_.email as email1_1_, userentity0_.fullname as fullname4_1_, userentity0_.password as password6_1_, userentity0_.role as role8_1_ from persons userentity0_ where userentity0_.email=?
当我用 Postman 测试时,它返回 404 我怎么知道我的参数已添加到查询中?我也无法运行调试。起初,它说我 absent 缺行号信息 但是在我将 jre1.8 更改为 jdk1.8 之后。它不显示该消息,但也跳过了我的断点。请帮帮我
【问题讨论】:
-
nativeQuery是最后的手段,仅当您无法以其他方式表达查询时。你可以去掉整个@Query,直接说findByCustomersEmail。 -
@chrylis-cautiouslyoptimistic- 我这样做了,但结果是一样的
标签: java spring-data-jpa jwt spring-boot-maven-plugin