我在H2数据库中使用spring数据,并且没有为我的实体对象创建数据库表。
application.properties
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:file:~/app_db
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create
DomainApplication.java
@SpringBootApplication
@Import(DefaultPersistenceConfig.class)
@EntityScan(basePackages = {"com.sample.Domain.modal"})
public class DomainApplication {
public static void main(String[] args) {
SpringApplication.run(DomainApplication.class, args);
}
}
DefaultPersistanceConfig:
@Configuration
@EnableJpaRepositories(basePackages = {"com.sample.Domain.model" }, entityManagerFactoryRef = "defaultEntityManagerFactory", transactionManagerRef = "defaultTransactionManager")
@EnableTransactionManagement
public class DefaultPersistenceConfig {
@Autowired
public DataSource defaultDataSource;
@Bean
@Primary
@Qualifier("defaultEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean defaultEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(defaultDataSource).persistenceUnit("hib-unit")
.packages("com.sample.Domain.model").build();
}
@Bean
@Primary
@Qualifier("defaultTransactionManager")
public PlatformTransactionManager defaultTransactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
实体类:
@Entity
@Table(name = "GENERAL_LEDGER_TRANSACTION")
public class GeneralLedgerTransaction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id = 0L;
....
JPA Repository界面:
@Repository
public interface GeneralLedgerTransactionRepository extends JpaRepository<GeneralLedgerTransaction, Long> {
}
当我运行应用程序并对控制器进行休息调用以从存储库中获取数据时,它应返回一个空数组,但我不断收到错误。
org.h2.jdbc.JdbcSQLException:表&#34; GENERAL_LEDGER_TRANSACTION&#34;未找到; SQL语句: 从GENERAL_LEDGER_TRANSACTION中选择... generalled0_ [42102-197] at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)〜[h2-1.4.197.jar:1.4.197] ...
答案 0 :(得分:0)
在您的application.properties
中添加spring.datasource.initialize=true