Spring JPA H2 数据库获取 org.h2.jdbc.JdbcSQLSyntaxErrorException Table 未找到
Spring JPA H2 database get org.h2.jdbc.JdbcSQLSyntaxErrorException Table not found
我有一个 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;
}
在应用程序-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
当我运行这个测试
@Test
void findAllByCity_idTest(){
assertEquals(1, taxOfficeService.findAllByCity_id(48).size());
}
我收到此错误:
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]
没有Table“TAX_OFFICE”,但是有“TaxOffice”,为什么要找“TAX_OFFICE”呢?
为什么会发生这种情况,我该如何解决?
编辑:TaxService.java
@Transactional
@Service
public class TaxOfficeService {
@Autowired
TaxOfficeRepository taxOfficeRepository;
public List<TaxOffice> findAllByCity_id(int id){
return taxOfficeRepository.findAllByCity_id(id);
}
}
税务局资料库
@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 和 Spring 默认具有命名策略,这决定了必须如何编译实体 class 以及如何生成 table 和列名。这可以根据使用情况通过应用程序属性或休眠配置文件进行自定义。
例如
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
我有一个 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;
}
在应用程序-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
当我运行这个测试
@Test
void findAllByCity_idTest(){
assertEquals(1, taxOfficeService.findAllByCity_id(48).size());
}
我收到此错误:
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]
没有Table“TAX_OFFICE”,但是有“TaxOffice”,为什么要找“TAX_OFFICE”呢? 为什么会发生这种情况,我该如何解决?
编辑:TaxService.java
@Transactional
@Service
public class TaxOfficeService {
@Autowired
TaxOfficeRepository taxOfficeRepository;
public List<TaxOffice> findAllByCity_id(int id){
return taxOfficeRepository.findAllByCity_id(id);
}
}
税务局资料库
@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 和 Spring 默认具有命名策略,这决定了必须如何编译实体 class 以及如何生成 table 和列名。这可以根据使用情况通过应用程序属性或休眠配置文件进行自定义。
例如
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl