英文:
Is there something wrong with my method-naming in 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
并使用了正确的参数,但我得到了相同的错误日志。我的方法有什么问题?
英文:
I have a simple question about JpaRepository
.
First, this is my Entity class.
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;
}
}
And below is my repository interface to do CRUD operations.
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);
}
After starting my spring-boot project, I get this error :
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!
I tried findByEmailAndS_codeAndPh_type
with proper arguments, but I got the same error log. What's the problem with my method there?
答案1
得分: 1
问题在于在spring-data-jpa方法名称中,下划线(_
)只限于类层次结构。它基于Java中使用驼峰命名法的简单约定,而您正在违反这一约定。
将字段ph_code
重命名为实体和方法名称中的phCode
,将s_code
重命名为实体和方法名称中的sCode
。
英文:
The problem is that underscore (_
) is restricted to class hierarchies in spring-data-jpa mathod names. It's based on the simple convention of using camelCase in Java, which you're breaking.
Rename the field ph_code
to phCode
and s_code
to sCode
both in the entity and in the method name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论