【发布时间】:2015-04-06 05:37:28
【问题描述】:
我尝试搜索但没有找到准确的解决方案。我有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> 如答案中所述创建接口ExtendedJpaRepository。
2> 为这个接口创建实现类如下(参考: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> 然后对于每个存储库接口,扩展ExtendedJpaRepository。这应该使 findByExample 在每个存储库中随时可用。
4> 创建自定义存储库工厂以替换 Spring data JPA doc 的步骤 4 中所述的默认 RepositoryFactoryBean。
5> 声明自定义工厂的bean。(Spring Data JPA Doc 的Step-5)
【问题讨论】:
-
你有没有试过
findByAddress(Address address)在我的项目中工作 -
是的,我试过了。但是在启动tomcat时给了我一个错误,指出“在地址实体中找不到地址”。 Tomcat 尝试为
AddressDataRepository& 创建对象,此时它会抛出错误。你的代码是如何工作的?您是否在 `Address pojo 中创建了Address变量? -
好的,我再看看。对不起,我的记忆是危险的。我有点搞混了。在地址上和你一样,findBy..Attributes...
标签: java spring spring-mvc jpa spring-data-jpa