spring引导jpa查询参数

s4n0splo  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(355)

我对jpa查询my orderentity中的add参数有问题:

@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之后。它不显示该消息,但也跳过我的断点。请帮帮我

uinbv5nw

uinbv5nw1#

要记录sql语句,可以使用:

log4j.logger.org.hibernate.SQL=debug

要记录参数,可以使用:

log4j.logger.org.hibernate.type=trace

如果您在调试时遇到问题,可以添加一个日志,以查看“电子邮件”是什么(除了添加上面提到的跟踪日志配置之外)。我建议您查找有关使用特定ide调试spring boot的文档。

相关问题