I have looked at numerous examples and tried various w/ & w/o example matcher constraints but whatever I do my query by example only returns null.
I am able to get the CRUD Repository data calls to work without any issues, however I extended that to the JPA Repository and added the QueryByExampleExecutor interface. The findOne() does not seem to be working.
Using Spring Boot 2.2.6.RELEASE, spring-boot-starter-data-jpa and mysql 5.7.
When using query by example does the entire object need to be completed for matching? In other words, do all setters need the correct information? Or just the object data item that is to be matched?
And Yes, the database does contain only one correct string on which I am attempting to match.
Customer
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="social_security_number")
private String socialSecurityNumber;
//Other data fields are included
}
CustomerRepository
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
QueryByExampleExecutor
import java.util.Optional;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
public interface QueryByExampleExecutor<T> {
<S extends T> Optional<S> findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example, Sort sort);
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
<S extends T> long count(Example<S> example);
<S extends T> boolean exists(Example<S> example);
}
CustomerService
public interface CustomerService {
public Customer findBySSN(String ssn);
}
CustomerServiceImpl
@Service
public class CustomerServiceImpl implements CustomerService {
private CustomerRepository customerRepository;
@Autowired
public CustomerServiceImpl(CustomerRepository theCustomerRepository) {
customerRepository = theCustomerRepository;
}
@Override
public Customer findBySSN(String ssn) {
//Create a new object to use as a basis for querying against
Customer customer = new Customer();
//Set the parameters for the query
customer.setSocialSecurityNumber(ssn);
//Create an example query
Example<Customer> customerExample = Example.of(customer);
Optional<Customer> result = customerRepository.findOne(customerExample);
if (result.isPresent()) {
return result.get();
} else {
// Didn't find the customer
throw new ObjectNotFoundException("Customer", "Social Security Number ("+ssn+") not found...");
}
}
}
Calling Method
@Autowired
private CustomerService customerService;
Customer ssnCustomer = customerService.findBySSN("123456789");
if(ssnCustomer == null) {
System.out.println("SSN NOT FOUND WAS NULL");
} else {
System.out.println("SSN FOUND "+ssnCustomer.getId());
}
What am I missing? Thank you in advance.