我在规范中使用jpa进行查询,但是得到了nullpointexception,我不知道原因。
@Entity
@Table(name = "universities")
@Data
@ToString(exclude = {"country", "administrativeDivision",
"departments", "projectVersions", "createdBy", "updatedBy"})
@EqualsAndHashCode(exclude = {"country", "administrativeDivision",
"departments", "projectVersions", "createdBy", "updatedBy"})
@EntityListeners(AuditingEntityListener.class)
public class University {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name_zh",nullable = false,unique = true)
private String nameZH;
@Column(name = "name_en",unique = true)
private String nameEN;
@ManyToOne
private Country country;
@ManyToOne
private AdministrativeDivision administrativeDivision;
@OneToMany(mappedBy = "university",cascade =
{CascadeType.PERSIST,CascadeType.REMOVE, CascadeType.MERGE
})
@JsonIgnore
private List<Department> departments = new ArrayList<>(16);
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "targetUniversities")
private Set<ProjectVersion> projectVersions = new HashSet<>();
@CreatedDate
@Column(nullable = false)
private Timestamp createdAt;
@LastModifiedDate
@Column(nullable = false)
private Timestamp updatedAt;
@ManyToOne
@JoinColumn(name = "created_by")
private User createdBy;
@ManyToOne
@JoinColumn(name = "updated_by")
private User updatedBy;
}
这是元数据
@Generated(value =
"org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(University.class)
public abstract class University_ {
public static volatile SingularAttribute<University,
AdministrativeDivision> administrativeDivision;
public static volatile SingularAttribute<University, Country>
country;
public static volatile SingularAttribute<University, Timestamp>
createdAt;
public static volatile SingularAttribute<University, User>
updatedBy;
public static volatile SingularAttribute<University, User>
createdBy;
public static volatile SingularAttribute<University, String>
nameZH;
public static volatile SetAttribute<University, ProjectVersion>
projectVersions;
public static volatile SingularAttribute<University, Long> id;
public static volatile SingularAttribute<University, String>
nameEN;
public static volatile ListAttribute<University, Department>
departments;
public static volatile SingularAttribute<University, Timestamp>
updatedAt;
}
这是带有元数据的jpa的规范查询方法
/**
* 模糊查询
* @param params
* @return
*/
public static Specification<University>
fuzzyQuery(UniversityFuzzyQueryParams params) {
return new Specification<University>() {
@Nullable
@Override
public Predicate toPredicate(Root<University> root,
CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
String nameZH = params.getUniversityName();
if (nameZH == null) {
return null;
}else {
//look here
Path<String> name = root.get(University_.nameZH);
return cb.like(name, "%" + nameZH + "%");
}
}
};
}
当我调用规范查询方法时,它将抛出nullpointexception。 所以我调试了此方法并发现了错误。
调试消息显示未加载University_元数据并且
运行Path<String> name = root.get(University_.nameZH);
时将抛出nullpointexception
这是异常的整个堆栈跟踪
java.lang.NullPointerException: null
at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:123)
at com.hikedu.backend.repository.specifications.UniversitySpecification$1.toPredicate(UniversitySpecification.java:34)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:695)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:626)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:584)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:387)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy168.findAll(Unknown Source)
at com.hikedu.backend.service.impl.UniversityServiceImpl.fuzzyQuery(UniversityServiceImpl.java:222)
at com.hikedu.backend.service.impl.UniversityServiceImpl$$FastClassBySpringCGLIB$$5f04221.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.hikedu.backend.service.impl.UniversityServiceImpl$$EnhancerBySpringCGLIB$$1a0ab94b.fuzzyQuery(<generated>)
at com.hikedu.backend.controller.UniversityController.getAllUniversityByFuzzyQuery(UniversityController.java:144)
at com.hikedu.backend.controller.UniversityController$$FastClassBySpringCGLIB$$e8dd14ad.invoke(<generated>)
因此,我检查了build文件夹,以确保university_元数据已经生成。我在构建文件夹中找到它,请参见下图 因为有太多的matadata,所以我没有在图片中包含university_元数据,但它已经存在。 有人可以帮我吗?
答案 0 :(得分:1)
根据您对University_类的定义,静态属性nameZH未初始化,因此它为null,这是null指针异常的原因。因此,University_确实存在并且已成功编译。否则,将无法引发null指针异常,但会引发无法加载某些类的异常。
答案 1 :(得分:0)
我得到了答案。 @StaticMetamodel
类名称必须与原始类名称相同,并以“ _”结尾。同样,两个类必须在同一包中。
例:
原始课程为com.a.Demo.class
。然后,StaticMetamodel为com.a.Demo_.class