我和springjpa一起工作,我有一个搜索模块,它在从3个级别进行搜索时运行良好,但是当我尝试搜索3个级别时,问题就出现了。
实体1(1级)
@Entity
@Table(name="ALUMNO")
public class Alumno implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDALUMNO", nullable = false)
private Long idAlumno;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "IDGRADO", insertable = true, updatable = true)
private Grado grado;
实体2(2级)
@Entity
@Table(name = "GRADO")
public class Grado implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDGRADO", nullable = false)
private Integer idGrado;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "IDNIVEL", insertable = true, updatable = true)
private Nivel nivel;
实体3(3级)
@Entity
@Table(name="NIVEL")
public class Nivel implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDNIVEL", insertable = false, updatable = false)
private Integer idNivel;
@Column(name = "DESCNIVEL")
private String descNivel;
自定义规范
else if (criteria.getOperation().equalsIgnoreCase("=")) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.equal(root.<String>get(criteria.getKey()),
criteria.getValue() != null ? criteria.getValue().toString().toUpperCase() : null);
} else {
return builder.equal(root.get(criteria.getKey()),
criteria.getValue() != null ? criteria.getValue() : null);
}
自定义过滤器适用于grado
AlumnoSpecification grado = new AlumnoSpecification(new SearchCriteria("grado", "=", alumno.getGrado().getIdGrado()));
result = a ? result.and(grado) : Specification.where(grado);
a = true;
用于搜索筛选器grado.nivel.idnivel
AlumnoSpecification nivel = new AlumnoSpecification(new SearchCriteria("grado.nivel", "=", alumno.getGrado().getNivel().getIdNivel()));
result = a ? result.and(nivel) : Specification.where(nivel);
a = true;
org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute with the the given name [nivel.idNivel] on this ManagedType [com.pe.antares.bean.Grado]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [nivel.idNivel] on this ManagedType [com.pe.antares.bean.Grado]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:257) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) ~[spring-data-jpa-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at com.sun.proxy.$Proxy110.findAll(Unknown Source) ~[na:na]
暂无答案!
目前还没有任何答案,快来回答吧!