java - 我在 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
了正确的参数,但我得到了相同的错误日志。我的方法有什么问题?
解决方案
问题是下划线 ( _
) 仅限于 spring-data-jpa 方法名称中的类层次结构。它基于在 Java 中使用 camelCase 的简单约定,您正在打破它。
在实体和方法名称中将字段重命名ph_code
为phCode
和。s_code
sCode
推荐阅读
- php - 取回正确数量的匹配项
- vuejs2 - Bootstrap-vue b-table:如何为非活动行设置 css-Class?
- azure-active-directory - 如何使用服务用户和令牌而不是证书来管理 Azure AD?
- r - 要求在 R Studio 中设置工作目录 - 多个用户使用同一个 R 脚本
- javascript - Express js登录系统
- node.js - 这个 c# 加密函数在 node.js 中的外观如何
- php - 如何将冰岛月份名称转换为英文飞蛾名称?
- java - 错误 1053:我的 java 包装应用程序没有响应,但它实际上正在运行
- amazon-web-services - AWS EC2 实例终止保护在自动缩放 IN 下不起作用
- java - 如何修复在 Android 中提供 layout_width 属性