Java 使用实体管理器时,没有为该名称定义查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27124427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:51:03  来源:igfitidea点击:

No Query defined for that name, when using Entity Manager

javasqlspringhibernatespring-boot

提问by mangusbrother

I have the following entity:

我有以下实体:

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";
    //  ...
}

And then using the Entity Manager (em) i'm doing the following:

然后使用实体管理器(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();
}

However I am getting the following error:

但是我收到以下错误:

java.lang.IllegalArgumentException: No query defined for that name [LoginFindUser]

Here's my spring-boot Configuration. This should include the scanning of the entity.

这是我的 spring-boot 配置。这应该包括对实体的扫描。

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

}

采纳答案by mangusbrother

Found it. Thanks to the guys in the comments

找到了。感谢评论区的小伙伴们

Add

添加

@EntityScan("com.server.models")

to your configuration class

到您的配置类

and I had an error in the query cause userneeded to be User

我在查询原因中有一个错误user需要User

回答by Rasheed

Okay, this might be an issue with you persistence configuration. If your models definitions are all in a separate ejb, make sure to add them in the persistence.xml in the persistence-unit

好的,这可能是您的持久性配置的问题。如果您的模型定义都在单独的 ejb 中,请确保将它们添加到persistence-unit 的persistence.xml 中

回答by Kevin Tomas

You can use this code snippet shown below.

您可以使用下面显示的代码片段。

<property name="packagesToScan" value="com.servers.model" />

回答by Shubhashish Mishra

Add

添加

entitymanager.packagesToScan:com.server.models

entitymanager.packagesToScan:com.server.models

to your application.properties file.

到您的 application.properties 文件。