微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Data JPA CRUD Repository Interface JPQL @Query 生成自定义查询失败 命名返回值

如何解决Spring Data JPA CRUD Repository Interface JPQL @Query 生成自定义查询失败 命名返回值

我正在开发基于休息(非休息)的 api,但遇到以下问题。尝试使用 JPQL 创建自定义查询以更新客户信息。实体客户中的成员电子邮件是唯一的,因此我必须编写自己的查询,否则会导致唯一的约束冲突异常。到目前为止,我发现了这一点,现在我正在尝试通过在 Spring CRUD Repository Interface 中使用 JPQL 编写自定义查询来修复。

客户控制器

@RestController public class CustomerController {
    
    @Autowired
    private CustomerService customerService;
    
    @GetMapping("/customers")
    public List<CustomerDTO> allCustomer(){
        return customerService.findAll();
    }
    
    @GetMapping("/customers/{id}")
    public CustomerDTO oneCustomer(@PathVariable("id") long id) {
        return customerService.findOneById(id);
    }
    
    @PostMapping("/customers")
    public CustomerDTO addCustomer(@RequestBody CustomerDTO customerDTO) {
        return customerService.saveCustomer(customerDTO);
    }
    
    @PutMapping("/customers/{id}")
    public CustomerDTO updateCustomer(@RequestBody CustomerDTO customerDTO) {
        return customerService.updateCustomer(customerDTO);
    }
    
    @DeleteMapping("/customers/{id}")
    public void deleteCustomer(@PathVariable("id") long id) {
         customerService.deleteCustomer(id);
    }

}

客户服务

@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private CustomerDTO customerDTO;

    @Autowired
    private modelmapper modelmapper;

    private final Logger log = LoggerFactory.getLogger(CustomerService.class);

    // Gebe Liste von Kunden zurück
    public List<CustomerDTO> findAll() {
        var it = customerRepository.findAll();
        var customerList = new ArrayList<CustomerDTO>();
        for (Customer customer : it) {
            customerDTO = convertToDto(customer);
            customerList.add(customerDTO);
        }
        return customerList;
    }

    // Gebe einen bestimmten Kunden zurück
    public CustomerDTO findOneById(long id) {
        Customer customer = customerRepository.findById(id);
        CustomerDTO customerDTO = convertToDto(customer);
        return customerDTO;
    }

    // Speicher einen Kunden in der Datenbank und gebe diesen zurück
    public CustomerDTO saveCustomer(CustomerDTO customerDTO) {
        if (customerDTO != null) {
            Customer savedobject = customerRepository.save(convertToEntity(customerDTO));
            // Abrufen der gespeicherten Entity und Umwandlung in DTO,weil DTO nun weitere Werte enthält als zuvor (Id & timestamp)
            CustomerDTO responseCustomer = convertToDto(customerRepository.findById(savedobject.getId()).get());
            return responseCustomer;
        } else {
            log.info("Kunden speichern in die Datenbank fehlgeschlagen");
            return null;
        }
    }

    // Kundendaten bearbeiten
    public CustomerDTO updateCustomer(CustomerDTO customerDTO) {
        if (customerDTO != null) {
            Customer updatedobject = customerRepository.updateCustomerByDTO(convertToEntity(customerDTO));
            // Abrufen der gespeicherten Entity und Umwandlung in DTO
            Customer getCustomer = customerRepository.findById(updatedobject.getId()).get();
            CustomerDTO responseCustomer = convertToDto(getCustomer);
            return responseCustomer;
        } else {
            log.info("Bearbeiten des Kunden in der Datenbank fehlgeschlagen!");
            return null;
        }

    }

    // Lösche Kunden aus der Datenbank
    public void deleteCustomer(Long id) {
        customerRepository.deleteById(id);
    }

    // Umwandlung von Entity zu DTO Objekt
    public CustomerDTO convertToDto(Customer customer) {
        CustomerDTO customerDTO = modelmapper.map(customer,CustomerDTO.class);
        return customerDTO;
    }

    // Umwandlung von DTO zu Entity Objekt
    private Customer convertToEntity(CustomerDTO customerDTO) {
        Customer customer = modelmapper.map(customerDTO,Customer.class);
        return customer;
    }

}

客户资料库

public interface CustomerRepository extends CrudRepository<Customer,Long> {

    /*
     * Here we can create our custom search queries on CustomerRepository
     */

    List<Customer> findBySurname(String surname);

    Customer findById(long id);

    Customer findByEmail(String email);
    
    //Update Customer workaround email field ConstraintViolationException
    @Transactional
    @Modifying
    @Query("UPDATE Customer c SET c.given_name = :#{#customer.given_name},c.surname = :#{#customer.surname},c.birthday= :#{#customer.birthday},"
            + " c.street_address = :#{#customer.street_address},c.city = :#{#customer.city},c.postal_code = :#{#customer.postal_code},"
            + " c.phone_number = :#{#customer.phone_number},c.balance= :#{#customer.balance},c.bonuspoints= :#{#customer.bonuspoints}"
            + " WHERE c.id = :#{#customer.id} ")
    Customer updateCustomerByDTO(@Param("customer") Customer customer);
    
}

这会导致遵循 Stacktrace,到目前为止我还没有找到任何解决方案。 堆栈跟踪

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerService': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository' defined in com.yildiz.Tradilianz.customer.CustomerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method Failed; nested exception is java.lang.IllegalArgumentException: Validation Failed for query for method public abstract com.yildiz.Tradilianz.customer.Customer com.yildiz.Tradilianz.customer.CustomerRepository.updateCustomerByDTO(com.yildiz.Tradilianz.customer.Customer)! at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessproperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.populateBean(AbstractAutowireCapablebeanfactory.java:1415) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:608) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:531) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.Abstractbeanfactory.lambda$doGetBean$0(Abstractbeanfactory.java:335) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.2.jar:5.3.2] at .......

Stacktrace 告诉我,验证是错误的,但我认为我的 JPQL 查询是正确的。我很困惑你知道我做错了什么吗?

解决方法

命名

有可能与命名的问题。您不包括您的客户的实体。为您的JPQL查询表明它是否真的使用snake_case命名约定?

在 JPQL 中,您应该使用Java 类中的确切字段名称。通常是camelCase命名约定,而在数据库中使用snake_case。

返回值

肯定存在与方法签名的问题。修改的查询只能使用空隙或INT /整数作为返回类型。

假设你真的使用客户一个snake_case和改变的返回类型后,updateCustomerByDTO void的,查询工作正常。

void updateCustomerByDTO(@Param("customer") Customer customer);

关于如何处理不可更新的电子邮件问题的另一个提示。你可以只用一个@Column注释与updatable属性设置为false。如果您真的不想更新电子邮件,那会容易得多。

@Entity
class Customer {
   ...
   @Column(updatable = false)
   String email;
   ...
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。