无法加载实体元数据
cannot load the entity meta data
我正在使用带有规范的 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 + "%");
}
}
};
}
当我调用规范查询方法时,它会抛出一个空点异常。
所以我调试了这个方法,发现不对劲。
调试消息显示 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.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$f04221.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$a0ab94b.fuzzyQuery(<generated>)
at com.hikedu.backend.controller.UniversityController.getAllUniversityByFuzzyQuery(UniversityController.java:144)
at com.hikedu.backend.controller.UniversityController$$FastClassBySpringCGLIB$$e8dd14ad.invoke(<generated>)
所以我检查了构建文件夹,确保 university_ 元数据已经构建。我在build文件夹中找到了它,见下图
因为元数据太多,所以我没有在图片上包含 university_ 元数据,但它已经存在了。
有人可以帮助我吗?
根据你对classUniversity_的定义,静态属性nameZH没有初始化,所以为null,这就是空指针异常的原因。所以 University_ 确实存在并且编译成功。否则不可能抛出空指针异常,而是抛出某些class无法加载的异常
我得到了答案。 @StaticMetamodel
class 名称必须与原 class 名称相同,并以“_”结尾。此外,两个 classes 必须在同一个包中。
例子:
原来的class是com.a.Demo.class
。那么StaticMetamodel就是com.a.Demo_.class
我正在使用带有规范的 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 + "%");
}
}
};
}
当我调用规范查询方法时,它会抛出一个空点异常。
所以我调试了这个方法,发现不对劲。
调试消息显示 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.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$f04221.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$a0ab94b.fuzzyQuery(<generated>)
at com.hikedu.backend.controller.UniversityController.getAllUniversityByFuzzyQuery(UniversityController.java:144)
at com.hikedu.backend.controller.UniversityController$$FastClassBySpringCGLIB$$e8dd14ad.invoke(<generated>)
所以我检查了构建文件夹,确保 university_ 元数据已经构建。我在build文件夹中找到了它,见下图
根据你对classUniversity_的定义,静态属性nameZH没有初始化,所以为null,这就是空指针异常的原因。所以 University_ 确实存在并且编译成功。否则不可能抛出空指针异常,而是抛出某些class无法加载的异常
我得到了答案。 @StaticMetamodel
class 名称必须与原 class 名称相同,并以“_”结尾。此外,两个 classes 必须在同一个包中。
例子:
原来的class是com.a.Demo.class
。那么StaticMetamodel就是com.a.Demo_.class