简体   繁体   English

Spring 数据 JPA 示例查询返回 Null

[英]Spring Data JPA Query by Example Returns Null

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.我查看了许多示例并尝试了各种 w/&w/o 示例匹配器约束,但无论我通过示例进行查询都只返回 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.我能够毫无问题地使 CRUD 存储库数据调用正常工作,但是我将其扩展到 JPA 存储库并添加了 QueryByExampleExecutor 接口。 The findOne() does not seem to be working. findOne() 似乎不起作用。

Using Spring Boot 2.2.6.RELEASE, spring-boot-starter-data-jpa and mysql 5.7.使用 Spring 启动 2.2.6.RELEASE、spring-boot-starter-data-jpa 和 mysql 5.7。

When using query by example does the entire object need to be completed for matching?使用示例查询时,是否需要完成整个 object 进行匹配? In other words, do all setters need the correct information?换句话说,所有的二传手都需要正确的信息吗? Or just the object data item that is to be matched?还是只是要匹配的 object 数据项?

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 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 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.先感谢您。

Change id type from long to Long.将 id 类型从 long 更改为 Long。 long is setting value as 0 automatically and making it part of query. long 自动将值设置为 0 并使其成为查询的一部分。

private Long id;私人长ID;

Change getter and Setter accordingly相应地更改 getter 和 Setter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM