使用实体管理器时,未为该名称定义查询
2022-09-03 05:22:21
我有以下实体:
package com.server.models;
@Entity
@Table(name="users")
@NamedQueries({
@NamedQuery(name=User.QUERY_FIND_USER,query="SELECT c FROM user c WHERE c.username = :username")
})
public class User {
public static final String QUERY_FIND_USER = "LoginFindUser";
// ...
}
然后使用实体管理器(em)我正在执行以下操作:
package com.server.controllers;
@Service
@Transactional
@RestController
@RequestMapping("/user")
public class LoginController {
@PersistenceContext
private EntityManager em;
// my code
TypedQuery<User> queries = em.createNamedQuery(User.QUERY_FIND_USER,User.class).setParameter("username", username);
List<User> users = queries.getResultList();
}
但是,我收到以下错误:
java.lang.IllegalArgumentException: No query defined for that name [LoginFindUser]
这是我的弹簧启动配置。这应该包括扫描实体。
package com.server.boot;
@Configuration
@ComponentScan({"com.server"})
@EnableAutoConfiguration
public class Starter {
public static void main(String[] args){
SpringApplication.run(Starter.class,args);
System.out.println("started application");
}
@Bean
public LoginController loginController(){
return new LoginController();
}
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}