java - Spring 数据 JPA 存储库匹配所有列或整个 pojo

标签 java spring spring-mvc jpa spring-data-jpa

我尝试搜索但没有找到准确的解决方案。我有 Address 实体。对于每个新的地址请求,首先我想检查数据库中是否存在相同的地址。我的申请是针对仓库的,同一地址请求有可能会出现多次。

地址实体

@Entity
@NamedQuery(name="Address.findAll", query="SELECT a FROM Address a")
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    private String firstname;

    private String lastname;

    private String address1;

    private String address2;

    private String address3;

    private String city;

    private String postcode;

    @JsonProperty(value="county")
    private String state;

    private String country;

    private String telephoneno;

    private String mobileno;    

    private String email;

    //bi-directional many-to-one association to Collection
    @OneToMany(mappedBy="address")
    @JsonIgnore
    private List<Collection> collections;

    //bi-directional many-to-one association to Delivery
    @OneToMany(mappedBy="address")
    @JsonIgnore
    private List<Delivery> deliveries;


    public Address() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAddress1() {
        return this.address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return this.address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getAddress3() {
        return this.address3;
    }

    public void setAddress3(String address3) {
        this.address3 = address3;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPostcode() {
        return this.postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getTelephoneno() {
        return telephoneno;
    }

    public void setTelephoneno(String telephoneno) {
        this.telephoneno = telephoneno;
    }

    public String getMobileno() {
        return mobileno;
    }

    public void setMobileno(String mobileno) {
        this.mobileno = mobileno;
    }

    public List<Collection> getCollections() {
        return this.collections;
    }

    public void setCollections(List<Collection> collections) {
        this.collections = collections;
    }

    public Collection addCollection(Collection collection) {
        getCollections().add(collection);
        collection.setAddress(this);

        return collection;
    }

    public Collection removeCollection(Collection collection) {
        getCollections().remove(collection);
        collection.setAddress(null);

        return collection;
    }

    public List<Delivery> getDeliveries() {
        return this.deliveries;
    }

    public void setDeliveries(List<Delivery> deliveries) {
        this.deliveries = deliveries;
    }

    public Delivery addDelivery(Delivery delivery) {
        getDeliveries().add(delivery);
        delivery.setAddress(this);

        return delivery;
    }

    public Delivery removeDelivery(Delivery delivery) {
        getDeliveries().remove(delivery);
        delivery.setAddress(null);

        return delivery;
    }


}

我知道一个解决方案是在存储库中声明一个方法,其中包含所有字段的And。例如

public Address findByFirstnameAndLastnameAndAddress1AndAddress2AndAddress3AndCityAndPostcode....();

但我想知道是否有更好的方法来做到这一点。有没有什么东西可以让我传递新的 Address 对象来检查数据库中是否存在相同的 Address

编辑

根据Manish的回答,我的理解如下:

1> 创建接口(interface) ExtendedJpaRepository,如答案中所述。

2> 如下所示为此接口(interface)创建实现类(引用:Spring Data Jpa Doc)

public class MyRepositoryImpl<T, ID extends Serializable>
  extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID> {
        List<T> findByExample(T example){
            //EclipseLink implementation for QueryByExample
        }
  }

3> 然后对于每个存储库接口(interface),扩展ExtendedJpaRepository。这应该使 findByExample 在每个存储库中都可用。

4> 创建自定义存储库工厂以替换默认的 RepositoryFactoryBean,如 Spring data JPA doc 的步骤 4 中所述.

5> 声明自定义工厂的 beans。(Spring Data JPA Doc 的第 5 步)

最佳答案

您正在寻找的是Query-by-Example。如 this post 中所述,此功能已考虑用于 JPA 2.0,但未包含在最终版本中。该帖子还解释说,大多数 JPA 提供程序都具有实现此功能所必需的功能。

您可以创建自定义 JPA 存储库实现,以提供开箱即用的功能。 Spring Data JPA documentation 中提供了详细信息.

起点是创建一个新界面,例如:

public interface ExtendedJpaRepository<T, ID extends Serializable>
    extends JpaRepository<T, ID> {
  List<T> findByExample(T example);
}

然后,插入使用底层 JPA 提供程序的此接口(interface)的实现。最后,配置您的自定义实现以用于您的所有存储库接口(interface)。

之后,您应该可以调用 addressRepository.findByExample(address),前提是 AddressRepository extends ExtendedJpaRepository

关于java - Spring 数据 JPA 存储库匹配所有列或整个 pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359655/

相关文章:

java - 使用Java代码将.sql文件导入MySQL

java - 如何将 spring bean 注入(inject)到 hibernate envers RevisionListener 中

spring - 登录 Controller 并在grails 3.0.0M1中查看

java - Spring MVC : best way to set global debug flag

java - 解析配置时出错java.io.FileNotFoundException :

java - 用poi写入excel文件

javascript - 如何在 Spring Boot 中的提交表单中将变量传递给 Controller ​​?

java - Spring SAML 握手失败 - 无法根据可信 key 验证不受信任的凭证

java - jsp中不显示值

java - 在 HandlerMapping 中注销映射