JPA 中的动态查询(按示例查询)



我有这些结构:

public class Invoice {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    @JoinColumn(name = "customer_id")
    @OneToOne(targetEntity = Customer.class)
    private Customer customer;
    @JoinColumn(name = "address_id")
    @OneToOne(targetEntity = Address.class)
    private Address address;
    private String invoiceType;
    // getters and setters
}
public class Customer {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    private String name;
    // getters and setters
}
public class Address {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    private String city;
    private String state;
    private String country;
    @ManyToOne
    @JoinColumn(name = "customer_id")
    @JsonIgnore
    private Customer customer;
    // getters and setters
}

InvoiceRepository是这样的:

public interface InvoiceRepository extends JpaRepository<Invoice, String> {
    List<Invoice> findByCustomerIdAndAddressId(long customerId, long addressId);
    List<Invoice> findByCustomerId(long customerId);
    List<Invoice> findByCustomerIdAndInvoiceType(long customerId, String invoiceType);
}

以及它在发票服务上自动连接的存储库。

我已经阅读了有关示例查询的信息,但我不确定是否可以将其与 JPA 一起使用。我可以在没有休眠的情况下使用吗?

谢谢!

我想你误解了JPA和Hibernate。

澄清一下,JPA是一个规范。JPA 代表 Java Persistence API。它有各种实现 - 所谓的提供者 - 例如OpenJPA,EclipseLink,Hibernate等。

想象一下,这就像有很多接口 - 基本上是API - 而你没有它们的实现。这是JPA。

接口的实现由提供程序完成,例如 Hibernate。

当然,你可以在没有Hibernate提供程序的情况下使用JPA,但是你必须使用另一个(EclipseLink,OpenJPA等(。

最新更新