首页 > 解决方案 > 我在 JPA 中的方法命名有问题吗?

问题描述

我有一个关于JpaRepository.

首先,这是我的实体类。

package com.surveypedia.domain.pointhistory;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor
@Getter
@Entity
@Table(name = "pointhistory")
public class PointHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer ph_code;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false, name = "s_code")
    private Integer s_code;

    @Column(nullable = false)
    private Integer pointchange;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private PointHistoryType ph_type;

    public PointHistory(String email, Integer s_code, Integer pointchange, PointHistoryType ph_type) {
        this.email = email;
        this.s_code = s_code;
        this.pointchange = pointchange;
        this.ph_type = ph_type;
    }
}

下面是我进行 CRUD 操作的存储库接口。

package com.surveypedia.domain.pointhistory;

import com.surveypedia.tools.SQL;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PointHistoryRepository extends JpaRepository<PointHistory, Integer> {

    List<PointHistory> findByEmail(String email);

    PointHistory findByS_codeAndEmailAndPh_type(Integer s_code, String email, PointHistoryType ph_type);
}

启动我的 spring-boot 项目后,我收到此错误:

java.lang.IllegalArgumentException: Failed to create query for method public abstract com.surveypedia.domain.pointhistory.PointHistory com.surveypedia.domain.pointhistory.PointHistoryRepository.findByS_codeAndEmailAndPh_type(java.lang.Integer,java.lang.String,com.surveypedia.domain.pointhistory.PointHistoryType)! No property s found for type PointHistory!

我尝试findByEmailAndS_codeAndPh_type了正确的参数,但我得到了相同的错误日志。我的方法有什么问题?

标签: javamysqlspringjpaspring-data-jpa

解决方案


问题是下划线 ( _) 仅限于 spring-data-jpa 方法名称中的类层次结构。它基于在 Java 中使用 camelCase 的简单约定,您正在打破它。

在实体和方法名称中将字段重命名ph_codephCode和。s_codesCode


推荐阅读