Tag Archives: IDEA JPA Custome Query Error

[Solved] IDEA JPA Custome Query Error: Can‘t resolve symbol ‘Type‘

I encountered a problem when doing project design today.

Problem found:insert image description here
As shown in the figure above, use the JPA annotation @Query to customize the query, and report the error Can’t resolve symbol ‘Type’.

Solve problem 1 – not Working:
I checked a lot of information on the Internet and said that you can follow the following
File -> Project Structure -> Facets -> JPA -> select the ” + ” sign in the lower right corner -> persistence.xml

As shown in the figure below:
insert image description here
After adding, it may still report an error. Continue the following operations: File -> Invalidate Caches/Restart, which is actually to clear the cache and restart.
But after restarting it still reported the same error.

Solve problem 2 – it works

  • The writing method of the JPA statement under the @Query annotation is different from the native SQL statement. select t from Type tIt means to query all data from the table maintained by the Type entity class (using JPA to maintain the database table through the entity class, so you can query the data in the corresponding table through the Type entity class).
  • select entity class name from entity class entity class name.
  • But the reason for the error here is actually a problem with the Hibernate mapping relationship. The from Xxx used in the hql query, Xxx is not the name of the entity class, but the EntityName (Hibernate annotation). If the EntityName is not explicitly specified after @Entity, the name of the entity class is used by default; if the EntityName is explicitly specified, when using hql query, the from EntityName should be used instead of the from entity class name.
  • In my code, there are entity classes Tag, Type, Blog, etc., but the annotations all specify @Entity(name=”t_type”), etc., so the following methods should be used in the Dao layer:
@Query("select t from t_type t")
List<Type> findTop(Pageable pageable);

The following is a screenshot of some code annotations in the entity class
insert image description here

insert image description here
insert image description here