OnTheRoad
OnTheRoad

Reputation: 613

cannot load the entity meta data

I am using jpa with the Specification to query but got a nullpointexception and i cannot to know the reason.

Here is my project struct enter image description here I have a entity like below :

@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;
}

Here is the metadata

@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;

}

Here is the specificationi query method of the jpa with metadata

/**
* 模糊查询
* @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 + "%");
                }
            }
        };
    }

when i call the specification query method it will throw a nullpointexception. So i debuged this method and find something wrong. enter image description here

The debug message is show the University_ metadata is not loaded and When running the Path<String> name = root.get(University_.nameZH); will throw a nullpointexception enter image description here

Here is the whole stacktrace of the exception

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

So i checked the build folder make sure the university_ metadata already builded. I finded it on the build folder, see below picture enter image description here Cause there are so many matadata, so i not include the university_ metadata on the picture, but it already exists. Can someone help me?

Upvotes: 0

Views: 501

Answers (2)

Yulong
Yulong

Reputation: 1

I got an answer. The @StaticMetamodel class name must be same with original class name and end with "_". Also both classes must be in the same package. Example: Original class is com.a.Demo.class. Then StaticMetamodel is com.a.Demo_.class

Upvotes: 0

vipcxj
vipcxj

Reputation: 1038

According to your definition of the class University_, the static attribute nameZH is not initialized, so it is null, which is the reason for the null pointer exception. So University_ does exist and is successfully compiled. Otherwise, it is impossible to throw a null pointer exception, but throw an exception that some class cannot be loaded.

Upvotes: 1

Related Questions