当前位置:  开发笔记 > 数据库 > 正文

Spring,Spring Data JPA:org.hibernate.hql.internal.ast.QuerySyntaxException:Test未映射

如何解决《Spring,SpringDataJPA:org.hibernate.hql.internal.ast.QuerySyntaxException:Test未映射》经验,为你挑选了1个好方法。

我得到一个org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped当我在Spring Data JPA Reposiotry中创建一个使用@Queryannontation和Spring Data JPA验证它的方法.

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

存储库看起来像这样:

public interface TestRepository extends JpaRepository{

   Test findByDescriptionContaining(String text); //works

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

}

令人感兴趣的是我可以使用Spring Data JPA Method名称解析并且这个查询可以正常工作.此外,当我向实体添加新字段并将hbm2ddl.auto设置为更新时,更改将持久保存到DB.但是带@Query注释的查询不起作用.

我的持久性配置如下所示:

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

}

更新:测试实体.基础实体拥有@Id Long id并且是@MappedSuperClass

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

   public Test() {
   }

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

M. Deinum.. 13

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

在您的实体中,您指定一个name覆盖默认命名的属性(使用类的名称).因此,您的查询是错误的,您的查询需要一个名为Test但不可用的实体.

您有2种可能的解决方案

    删除该name属性并保持查询不变.

    将查询更改为select t from test t(注意t而不是T).

随后返回类型也是错误的,因为它将返回元素集合而不是单个元素.所以改变它List.



1> M. Deinum..:
@Entity(name = "test")
public class Test extends BaseEntity { ... }

在您的实体中,您指定一个name覆盖默认命名的属性(使用类的名称).因此,您的查询是错误的,您的查询需要一个名为Test但不可用的实体.

您有2种可能的解决方案

    删除该name属性并保持查询不变.

    将查询更改为select t from test t(注意t而不是T).

随后返回类型也是错误的,因为它将返回元素集合而不是单个元素.所以改变它List.

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有