简体   繁体   中英

@Query Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: topic is not mapped error

@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{ 
    @Query(value = "SELECT u from topic u where u.name = ?1")
    List<Topic> findEverything(String name);
}

According to the spring documentation, I can do the above @query. However, it gives me Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: topic is not mapped error.

However, when i do this below, it works.

@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{
    @Query(value="SELECT * FROM topic WHERE topic.name = ?1", nativeQuery = true)
    List<Topic> findEverything(String name);
}

I am using mySQL database. I need to know why. please help me thank you.

if you're using a JPQL query, try to annotate and name your param, instead of relying on positional params, like so:

@Query(value = "SELECT u from topic u where u.name = :name")
List<Topic> findEverything(@Param("name")String name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM