Robert Niestroj
Robert Niestroj

Reputation: 16131

Spring, Spring Data JPA: org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped

im getting a org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped when i create a method in a Spring Data JPA Reposiotry that uses the @Query annontation and Spring Data JPA validates it.

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped [SELECT t from Test t]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) [rt.jar:1.8.0_40]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
    at com.sun.proxy.$Proxy125.createQuery(Unknown Source)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
    ... 47 more

The repository looks like this:

public interface TestRepository extends JpaRepository<Test, Long>{

   Test findByDescriptionContaining(String text); //works

   @Query("SELECT t from Test t") //fails
   Test getOr();

}

What is interesing is that i can use Spring Data JPA Method name resolving and this queries work. Also when i add new fields to the Entity and have hbm2ddl.auto set to update changes are persisted to DB. But queries with the @Query annotation are not working.

My persistence configuration looks like this:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.company", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean")
@ComponentScan("com.company")
public class PersistenceJPAConfig {

   private static final Logger LOGGER = LoggerFactory.getLogger(PersistenceJPAConfig.class);

   @Bean
   @DependsOn("dataSource")
   public JdbcTemplate jdbcTemplate() {
      return new JdbcTemplate(dataSource());
   }

   @Bean
   @DependsOn("dataSource")
   public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
      return new NamedParameterJdbcTemplate(dataSource());
   }

   @Bean
   public MailSender mailSender(){
      final MailSenderImpl mailSenderImpl = new MailSenderImpl();
      mailSenderImpl.setDataSource(dataSource());
      return mailSenderImpl;
   }

   @Bean
   public DataSource dataSource() {
      HikariConfig config = new HikariConfig();
      config.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      config.setJdbcUrl("jdbc:sqlserver://localhost;DatabaseName=mydb");
      config.setUsername("user");
      config.setPassword("pass");
      config.setPoolName("HikariCpConnectionPool");
      config.setMaximumPoolSize(50);
      config.setMinimumIdle(2);
      return new HikariDataSource(config);
   }

   @Bean
   @DependsOn({"dataSource"})
   public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[]{
         "com.company.**.*"
      });
      em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      em.setJpaProperties(additionalProperties());
      return em;
   }

   @Bean
   public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
      return new PersistenceExceptionTranslationPostProcessor();
   }

   @Bean
   public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
      return new JpaTransactionManager(entityManagerFactory);
   }

   private Properties additionalProperties() {
      Properties properties = new Properties();
      properties.setProperty("hibernate.hbm2ddl.auto", "update");
      properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
      properties.setProperty("hibernate.show_sql", "false");
      properties.setProperty("hibernate.format_sql", "false");
      properties.setProperty("hibernate.use_sql_comments", "false");
      properties.setProperty("hibernate.id.new_generator_mappings", "false");
      properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
      properties.setProperty("hibernate.generate_statistics", "false");
      return properties;
   }

}

UPDATE : the Test entity. Base entity has @Id Long id and is a @MappedSuperClass

@Entity(name = "test")
public class Test extends BaseEntity{

   public Test() {
   }

   @Column(name = "description")
   private String description;

Upvotes: 4

Views: 12427

Answers (4)

BJ5
BJ5

Reputation: 512

Use like below if the name of the table is test in database

@Entity
@Table(name="test")

Upvotes: 1

M. Deinum
M. Deinum

Reputation: 124441

@Entity(name = "test")
public class Test extends BaseEntity { ... }

In your entity you specify a name attribute overriding the default naming (the use of the name of the class). Hence your query is wrong, your query expects an entity named Test but that isn't available.

You have 2 possible solutions

  1. Remove the name attribute and leave the query as is.
  2. Change the query to select t from test t (notice the t instead of T).

Subsequently your return type is also wrong as it will return a collection of elements and not a single element. So change that to List<Test>.

Upvotes: 21

Omkar Puttagunta
Omkar Puttagunta

Reputation: 4156

Remove the * in the setPackagesToScan property and simply use - em.setPackagesToScan(new String[]{"com.company"});

Upvotes: 1

Bhuwan Prasad Upadhyay
Bhuwan Prasad Upadhyay

Reputation: 3056

Use List to get all object

@Query("SELECT t from Test t") //fails
List<Test> getOr();

Because SELECT t from Test t fetch all rows from database .

Upvotes: 0

Related Questions