title: Spring boot + hibernate - entity is not mapped + best way to configuration
body: <p>I'm trying to learn spring-boot basic annotation configuration with hibernate, to make myself a template that will always work. </p>
  
  <p>I'm using spring-boot latest release version 1.51 on STS (spring tool suite) 3.8.3.</p>
  
  <p>Here is my main:</p>
  
  <pre><code>@SpringBootApplication
  @EnableAutoConfiguration
  public class DemoApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Now, I know that <code>@SpringBootApplication</code> automatically comes with <code>@componetScan</code>, so I didn't add it.</p>
  
  <p>My configuration class:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(entityManagerFactoryRef = "someEntityManagerFactory", transactionManagerRef = "someTransactionManager", basePackages = {
          "com.example.*" })
  @EntityScan(basePackages = "com.demo.models")
  @ConfigurationProperties(prefix = "mysql.datasource")
  public class DataBaseConfig {
  
      @Autowired
      private Environment env;
  
      @Bean
      public DataSource someDataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(env.getProperty("mysql.datasource.driver-class-name"));
          dataSource.setUrl(env.getProperty("mysql.datasource.url"));
          dataSource.setUsername(env.getProperty("mysql.datasource.username"));
          dataSource.setPassword(env.getProperty("mysql.datasource.password"));
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean someEntityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(someDataSource());
          em.setPackagesToScan(new String[] { "org.openlegacy.analytics.models" });
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());
  
          return em;
      }
  
      @Bean
      public PlatformTransactionManager someTransactionManager() {
          JpaTransactionManager tm = new JpaTransactionManager();
          tm.setEntityManagerFactory(someEntityManagerFactory().getObject());
          tm.setDataSource(someDataSource());
          return tm;
      }
  
      Properties additionalProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
          properties.setProperty("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
          properties.setProperty("spring.jpa.show-sql", env.getProperty("spring.jpa.show-sql"));
          properties.setProperty("spring.jpa.hibernate.naming.physical-strategy",
                  env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
          return properties;
      }
  
  }
  </code></pre>
  
  <p>My controller class:</p>
  
  <pre><code>@RestController
  @RequestMapping("/users")
  public class UserController {
  
      @Autowired
      private UserRepository userRipository;
  
      @RequestMapping(value = "", method = RequestMethod.GET)
      public List&lt;User&gt; getItems() {
          return userRipository.getUsers();
      }
  
      @RequestMapping(value = "/message", method = RequestMethod.GET)
      public String getMessage() {
          return userRipository.getMessage();
      }
  
  }
  </code></pre>
  
  <p>My repository class:</p>
  
  <pre><code>@Transactional
  @Repository
  public class UserRepository {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @SuppressWarnings("unchecked")
      public List&lt;User&gt; getUsers() {
          return entityManager.createQuery("select u from User u").getResultList();
      }
  
      public String getMessage() {
          return "hello";
      }
  }
  </code></pre>
  
  <p>My entity class:</p>
  
  <pre><code>@Entity(name = "user")
  public class User implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "user_name")
      private String userName;
  
      @Column(name = "password")
      private String password;
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getUserName() {
          return userName;
      }
  
      public void setUserName(String userName) {
          this.userName = userName;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  }
  </code></pre>
  
  <p>and my properties file:</p>
  
  <pre><code># DataSource settings: set here your own configurations for the database connection.
  mysql.datasource.username=openlegacy
  mysql.datasource.password=openlegacy
  mysql.datasource.driver-class-name=com.mysql.jdbc.Driver
  mysql.datasource.url=jdbc:mysql://localhost:3306/olbank
  spring.jpa.database= MYSQL
  
  spring.data.jpa.repositories.enabled=true
  #spring.jpa.database-platform=org.hibernate.dialect.MYSQL5Dialect
  
  # Show or not log for each sql query
  spring.jpa.show-sql = true
  
  # Hibernate ddl auto (create, create-drop, update)
  spring.jpa.hibernate.ddl-auto = update
  
  # Naming strategy
  #spring.jpa.hibernate.naming.strategy= org.hibernate.cfg.ImprovedNamingStrategy
  spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  
  # The SQL dialect makes Hibernate generate better SQL for the chosen database
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  </code></pre>
  
  <p>and when I'm tying to retrieve data from the user table, I get this error:</p>
  
  <blockquote>
    <p>org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped...</p>
  </blockquote>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>why am I getting this error? I know that user is mapped by the class name, and that's what I'm doing</p></li>
  <li><p>is this the best way to configure hibernate with spring-boot? It's important for me to write code by the best practices. </p></li>
  </ol>
  
  <p>Please give a detailed answer so I can learn from it. </p>
  
  <p>Any other information that can be useful is welcome :)</p>
  
  <p>Thanks.</p>