19

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:

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.

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

}
7
  • Does it work when you put a simple String as name? If yes, it's probably some evaluation time difference Commented Nov 25, 2014 at 11:00
  • 1
    Then check if User entity is defined in your persistence unit. Commented Nov 25, 2014 at 11:11
  • I'm using spring-boot so the persistence is autodefined. The Context Scan should be including it Commented Nov 25, 2014 at 11:12
  • added the configuration to the question Commented Nov 25, 2014 at 11:16
  • 2
    And BTW, if the entity was mapped, you would get an exception because the query is invalid. user != User. Commented Nov 25, 2014 at 11:20

5 Answers 5

21

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 user needed to be User

Sign up to request clarification or add additional context in comments.

2 Comments

what is the version of persistence has this annotation ?
or to <property name="packagesToScan" value="com.servers.model" /> if you are using old dispatcher-servlet.xml
8

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

Comments

1

You can do by below configuration.

private static final String[] ENTITYMANAGER_PACKAGES_TO_SCAN = { "a.b.c.entity" };

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {

    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setJpaVendorAdapter(vendorAdaptor());
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    entityManagerFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
    entityManagerFactoryBean.setJpaProperties(jpaHibernateProperties());

    return entityManagerFactoryBean;
}

Comments

0

You can use this code snippet shown below.

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

Comments

0

Add

entitymanager.packagesToScan:com.server.models

to your application.properties file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.