I tried to search but didn't find accurate solution. I have Address
entity. With every new Address request, first I want to check whether same exist in database or not. My application is for warehouse & there are chances that same address request will come multiple time.
Address Entity
@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;
}
}
I know one solution could be declaring a method in repository with And
including all fields. for e.g.
public Address findByFirstnameAndLastnameAndAddress1AndAddress2AndAddress3AndCityAndPostcode....();
But I want to know if better way to do this. Is there anything using which I just pass new Address
object to check same Address
exist in database or not.
EDIT
Based on Manish's answer, following is what I understand:
1> Create interface ExtendedJpaRepository
as mentioned in answer.
2> Create implementation class for this interface as below (Ref : 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> Then for every repository interface, extend ExtendedJpaRepository
. This should make findByExample
readily available in every repository.
4> Create a custom repository factory to replace the default RepositoryFactoryBean as stated in step 4 of Spring data JPA doc.
5> Declare beans of the custom factory.(Step-5 of Spring Data JPA Doc )