Spring boot 使用枚举筛选状态
您好,我有spring服务,在存储库中,我尝试按枚举进行筛选 列出findAllByStatusStatusType类型 由于某些原因,此枚举参数未传递给sql查询 我看到Organization0_u0.status=?在SQL中,但未传递任何参数 知道原因是什么吗 枚举: 服务:Spring boot 使用枚举筛选状态,spring-boot,enums,spring-data-jpa,spring-data,Spring Boot,Enums,Spring Data Jpa,Spring Data,您好,我有spring服务,在存储库中,我尝试按枚举进行筛选 列出findAllByStatusStatusType类型 由于某些原因,此枚举参数未传递给sql查询 我看到Organization0_u0.status=?在SQL中,但未传递任何参数 知道原因是什么吗 枚举: 服务: public List<Organization> getAllOrganizations() { return Lists.newArrayList(organizationRepos
public List<Organization> getAllOrganizations() {
return Lists.newArrayList(organizationRepository.findAllByStatus(StatusType.TO_BE_DELETED));
}
和创建方法拦截器,以将枚举参数转换为特定类型
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import com.example.demo.dto.ParamWrapper;
import com.example.demo.dto.StatusType;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
public class CustomQueryParamMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (invocation instanceof ReflectiveMethodInvocation) {
ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation)invocation;
Object[] args = invocation.getArguments();
if (args != null && args.length > 0) {
Object[] newArgs = new Object[args.length];
for (int i=0 ;i<args.length; i++) {
if (args[i] instanceof ParamWrapper) {
ParamWrapper p = (ParamWrapper)args[i];
newArgs[i] = p.getValue();
} else {
newArgs[i] = args[i];
}
}
rmi.setArguments(newArgs);
}
}
return invocation.proceed();
}
}
ParamWrapper是枚举应该实现的泛型类型
public interface ParamWrapper<T> {
T getValue();
}
那么您的枚举应该这样更改
public enum StatusType implements ParamWrapper<Integer> {
ACTIVE,
TO_BE_DELETED,
@Deprecated IN_ACTIVE;
@Override
public Integer getValue() {
//you should determine your value
return 1;
}
}
和创建方法拦截器,以将枚举参数转换为特定类型
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import com.example.demo.dto.ParamWrapper;
import com.example.demo.dto.StatusType;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
public class CustomQueryParamMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (invocation instanceof ReflectiveMethodInvocation) {
ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation)invocation;
Object[] args = invocation.getArguments();
if (args != null && args.length > 0) {
Object[] newArgs = new Object[args.length];
for (int i=0 ;i<args.length; i++) {
if (args[i] instanceof ParamWrapper) {
ParamWrapper p = (ParamWrapper)args[i];
newArgs[i] = p.getValue();
} else {
newArgs[i] = args[i];
}
}
rmi.setArguments(newArgs);
}
}
return invocation.proceed();
}
}
ParamWrapper是枚举应该实现的泛型类型
public interface ParamWrapper<T> {
T getValue();
}
那么您的枚举应该这样更改
public enum StatusType implements ParamWrapper<Integer> {
ACTIVE,
TO_BE_DELETED,
@Deprecated IN_ACTIVE;
@Override
public Integer getValue() {
//you should determine your value
return 1;
}
}
你确定他们没有通过吗?也许参数只是隐藏在问号后面。是的,当我添加第二个参数时,我只看到第二个参数:Hibernate:select。。。来自组织Organization0?,其中组织状态=?和Organization0_u0.name=?2019-10-23 16:34:31.003跟踪19884-[0.1-9090-exec-6]o.h.type.descriptor.sql.BasicBinder:绑定参数[2]为[VARCHAR]-[test]您可以发布您的模型吗。我对为该枚举定义列的部分感兴趣。我猜,在实体类的列定义中使用@EnumeratedEnumType.STRING。公共类组织扩展AbstractBaseEntity实现BaseEntity是否确实没有传递它们?也许参数只是隐藏在问号后面。是的,当我添加第二个参数时,我只看到第二个参数:Hibernate:select。。。来自组织Organization0?,其中组织状态=?和Organization0_u0.name=?2019-10-23 16:34:31.003跟踪19884-[0.1-9090-exec-6]o.h.type.descriptor.sql.BasicBinder:绑定参数[2]为[VARCHAR]-[test]您可以发布您的模型吗。我对您为该枚举定义列的部分感兴趣。我猜,在实体类的列定义中使用@EnumeratedEnumType.STRING。公共类组织扩展AbstractBaseEntity实现BaseEntity基于它应该使用简单的枚举设置。我真的需要包装我的枚举吗?我认为我的解决方案更灵活,您可以将任何枚举或甚至任何自定义类型作为参数传递给jpa存储库方法,但是\@Enumerated注释应该在\@Entity对象中使用,因为它应该使用简单的枚举设置。我真的需要包装我的枚举吗?我认为我的解决方案更灵活,您可以将任何枚举或甚至任何自定义类型作为参数传递给jpa存储库方法,但是\@Enumerated注释应该在\@Entity对象中使用