[英]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.