java - 无法加载实体元数据

标签 java jpa metadata specifications

我正在使用 jpa 和规范来查询,但出现了 nullpointException,我无法知道原因。

这是我的项目结构 enter image description here 我有一个如下所示的实体:

@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 + "%");
                }
            }
        };
    }

当我调用规范查询方法时,它将抛出空点异常。 于是我调试了这个方法,发现有问题。 enter image description here

调试消息显示 University_ 元数据未加载并且 运行 Path<String> name = root.get(University_.nameZH); 时会抛出空点异常 enter image description here

这是异常的整个堆栈跟踪

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>)

所以我检查了构建文件夹,确保 University_ 元数据已经构建。我在build文件夹中找到了它,见下图 enter image description here 因为matadata太多,所以我没有在图片上包含university_元数据,但它已经存在了。 有人可以帮助我吗?

最佳答案

根据你对类University_的定义,静态属性nameZH没有初始化,所以为null,这就是空指针异常的原因。所以University_确实存在并且编译成功。否则不可能抛出空指针异常,而是抛出某个类无法加载的异常。

关于java - 无法加载实体元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005600/

相关文章:

java - Netbeans 7.4 对 Web 应用程序的支持

java - JPQL 带条件的左外连接

php - Symfony2/Doctrine 找不到自定义存储库类的映射文件

php - 在 PHP 中上传时删除元数据的图像

c# - NavigationProperty.GetDependentProperties() 返回一个空集合

java - 静态初始化 block 跳过

java - 未打印非 ASCII 值符号

java - JPA POJO 作为数据对象

java - 使用java根据elasticsearch数组中的条件获取特定值

java - JPA在多个事务中插入行,限制单个事务中的最大行数