简体   繁体   English

Spring JPA H2 数据库获取 org.h2.jdbc.JdbcSQLSyntaxErrorException 未找到表

[英]Spring JPA H2 database get org.h2.jdbc.JdbcSQLSyntaxErrorException Table not found

I have a Spring boot Entity defined as:我有一个 Spring 引导实体定义为:

@Data
@Entity
@Table(name = "TaxOffice")
public class TaxOffice {

    public TaxOffice(){}

    public TaxOffice(int id, String name, int voivodeship_id){
        this.id = id;
        this.name = name;
        this.voivodeship_id = voivodeship_id;
      
    }

    @Id
    private int id;

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

    @Column(name="voivodeship_id")
    private int voivodeship_id;

    @ManyToOne
    @JoinColumn(name = "city_id")
    private City city;

    @OneToOne
    @JoinColumn(name = "details_id")
    private TaxOffice_Detail taxOffice_details;
}

In application-test.properties, I have following settings:在 application-test.properties 中,我有以下设置:

spring.datasource.url=jdbc:h2:mem:TestDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.defer-datasource-initialization=true

When I run this Test当我运行这个测试时

    @Test
    void  findAllByCity_idTest(){
        assertEquals(1, taxOfficeService.findAllByCity_id(48).size());
    }

i recieve this error:我收到此错误:

org.h2.jdbc.JdbcSQLSyntaxErrorException: 
Table "TAX_OFFICE" not found; SQL statement:
/* select t from TaxOffice t where t.city.id = :id */ select taxoffice0_.id as id1_1_, taxoffice0_.city_id as city_id4_1_, taxoffice0_.name as name2_1_, taxoffice0_.details_id as details_5_1_, taxoffice0_.voivodeship_id as voivodes3_1_ from tax_office taxoffice0_ where taxoffice0_.city_id=? [42102-210]

There is no Table "TAX_OFFICE", but there is "TaxOffice", so why is it looking for "TAX_OFFICE"?没有“TAX_OFFICE”表,但有“TaxOffice”,为什么要找“TAX_OFFICE”? Why is this happening and how can i fix this?为什么会发生这种情况,我该如何解决?

Edit: TaxService.java编辑:TaxService.java

@Transactional
@Service
public class TaxOfficeService {

    @Autowired
    TaxOfficeRepository taxOfficeRepository;

    public List<TaxOffice> findAllByCity_id(int id){
        return taxOfficeRepository.findAllByCity_id(id);
    }

}

TaxOfficeRepository税务局资料库

@Repository("taxOfficeRepository")
public interface TaxOfficeRepository extends JpaRepository<TaxOffice,Integer> {

    @Query("select t from TaxOffice t where t.city.id = :id")
    List<TaxOffice> findAllByCity_id(int id);
}

Hibernate and Spring by default having naming strategies, which decide how the entity class must be compiled and the table and column names be generated. Hibernate 和 Spring 默认具有命名策略,这决定了实体 class 必须如何编译以及如何生成表名和列名。 This can be customized as per use through application properties or hibernate configuration file.这可以通过应用程序属性或 hibernate 配置文件根据用途进行自定义。

eg例如

spring:
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.h2.jdbc.JdbcSQLSyntaxErrorException h2 数据库 java - org.h2.jdbc.JdbcSQLSyntaxErrorException h2 database java H2版本升级后org.h2.jdbc.JdbcSQLSyntaxErrorException - org.h2.jdbc.JdbcSQLSyntaxErrorException after H2 version upgrade 插入时间戳记时,H2 org.h2.jdbc.JdbcSQLSyntaxErrorException - H2 org.h2.jdbc.JdbcSQLSyntaxErrorException when inserting a Timestamp org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列; SQL 声明 [SPRINGBOOT] - org.h2.jdbc.JdbcSQLSyntaxErrorException: Column not found; SQL statement [SPRINGBOOT] 引起:org.h2.jdbc.JdbcSQLSyntaxErrorException:Function “SYSUTCDATETIME”未找到; SQL 声明:[90022-200] - Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Function "SYSUTCDATETIME" not found; SQL statement:[90022-200] org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误 - org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列“USER0_.PROFILE_ID”; SQL 声明: - org.h2.jdbc.JdbcSQLSyntaxErrorException : Column "USER0_.PROFILE_ID" not found; SQL statement: Spring SQL:org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误“;预期为“标识符”,使用 INSERT INTO 时 - Spring SQL: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "; expected "identifier", when using INSERT INTO 如何修复 org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误需要“标识符” - How to fix org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement expected "identifier" H2 内存数据库。 JdbcSQLSyntaxErrorException: 未找到表(此数据库为空) - H2 In-memory database. JdbcSQLSyntaxErrorException: Table not found (this database is empty)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM