java - Spring Boot jpa 查询参数
问题描述
我在 JPA Query My OrderEntity 中添加参数时遇到问题:
@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 我如何知道我的参数已添加到查询中?我也无法运行调试。起初,它说我缺席了行号信息但是在我将jre1.8更改为jdk1.8之后。它不显示该消息,但也跳过了我的断点。请帮我
解决方案
要记录 SQL 语句,您可以使用:
log4j.logger.org.hibernate.SQL=debug
要记录参数,您可以使用:
log4j.logger.org.hibernate.type=trace
如果您在调试时遇到问题,您可以添加一个日志,以查看“电子邮件”是什么(除了添加上面提到的跟踪日志记录配置。)我建议查找有关使用特定 IDE 调试 Spring Boot 的文档利用。
推荐阅读
- sql - 输出仅检查 PL/SQL 中的第一个孩子
- python - 告诉重定向以模式打开表单
- r - as.Date 以 R 中的四位数字返回日期
- java - 我没有成功在另一个 servlet 中调用 servlet
- php - Symfony 4:重新安装 VichUploaderBundle?
- node.js - Node.js 的内部结构——首先执行请求,然后执行其他所有请求
- javascript - 如何在样式组件中以 px 为单位获取元素的宽度?
- c# - C# 在术语中拆分数学表达式
- equation - 找出两个对数方程之间的关系
- typescript - 如何在 ionic 5 中使用本机存储