gpt4 book ai didi

spring-boot - 有没有办法为 JPA 存储库 : findByEnumContaining(String enum) [enum is subString for possible ENUM values] 添加抽象方法

转载 作者:行者123 更新时间:2023-12-05 04:24:46 29 4
gpt4 key购买 nike

这是我的 JPA @Repository,在这里我们可以得到 list<Person>findByFullNameContaining(String query) - 通过在查询中提供 fullName 的子字符串

@Repository
public interface PersonRepository extends CrudRepository<Person,String> {
Optional<Person> findByFullName(String fullName);
List<Person> findByDepartment(Department department);
List<Person> findByFullNameContaining(String query);

}

类似地,我们可以通过提供 ENUM 的子字符串值来为 ENUM 值执行某些操作吗?怎么办?

例如。

public enum Department {
NORTH,
SOUTH,
EAST,
WEST
}

List<Person> findByDepartmentContaining(String query);

JPA @Entity 人:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "Person")
public class Person {

@Id
@NotNull(message = "Mobile number is required")
@Size(min = 10, max = 10, message = "Mobile no. must be 10 digits")
@Column(name = "person_id", unique = true, length = 10)
private String mobileNum;

@Transient
@NotNull(message = "Password is required")
@Size(min = 1, message = "Password cannot be empty")
private String password="****";

@NotNull(message = "Name cannot be empty")
@Size(min = 1, max = 255, message = "fullName must be 1-255 characters long")
@Column(name = "full_name")
private String fullName;

@Column(name = "department")
@Enumerated(EnumType.STRING)
@NotNull(message = "Department must be specified")
private Department department = Department.UNKNOWN;

public Person() {
}

public Person(String mobileNum, String fullName, String password, Department department) {
this.mobileNum = mobileNum;
this.password = password;
this.fullName = fullName;
this.department = department;
}

public String getMobileNum() {
return mobileNum;
}

public void setMobileNum(String mobileNum) {
this.mobileNum = mobileNum;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

@Override
public String toString() {
return "Person [fullName=" + fullName + ", mobileNum=" + mobileNum + ", password=" + password + ", department=" + department + "]";
}

}

最佳答案

当声明这样的方法时,这个问题没有具体说明问题是什么。

但是尝试使用 spring boot 2.7PostgreSql 数据库应用程序抛出以下运行时错误:

java.lang.IllegalArgumentException: Parameter value [%some value%] did not match expected type [com.Department (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.6.5.Final.jar:5.6.5.Final]

至少 hibernate 的问题是它期望实体中的 Department 字段始终作为参数传递给它自己的对象类型,即 Department

我认为没有办法避免这种情况,因为这是 Hibernate 的开箱即用功能。

不过,我认为正确的方法不是定义这种类型的方法,而是定义以下方法。 Department 已经存在于应用程序代码中,因此在需要调用查询时它是已知的。所以我认为以下解决方案将被视为更好的做法:

//Repository method to be used in PersonRepository
List<Person> findByDepartmentIn(List<Department> departments);

然后可以通过以下方式从服务层调用存储库。

//Service method to be used
public List<Person> findByDepartmentIn(String searchBy) {

List<Department> departments = Arrays.stream(Department.values()).filter(dep -> dep.getName().contains(searchBy)).collect(Collectors.toList());

return personRepository.findDepartmentIn(departments);
}

关于spring-boot - 有没有办法为 JPA 存储库 : findByEnumContaining(String enum) [enum is subString for possible ENUM values] 添加抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73433703/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com