Java org.h2.jdbc.JdbcSQLException:Table";所有“U序列”;找不到
有人能告诉我以下错误的原因吗 我在我的项目中使用Hibernate,在服务器启动过程中遇到以下错误Java org.h2.jdbc.JdbcSQLException:Table";所有“U序列”;找不到,java,hibernate,jdbc,Java,Hibernate,Jdbc,有人能告诉我以下错误的原因吗 我在我的项目中使用Hibernate,在服务器启动过程中遇到以下错误 15:04:27.909 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaValidator - HHH000319: Could not get database metadata org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
15:04:27.909 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaValidator - HHH000319: Could not get database metadata
org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.168.jar:1.3.168]
at org.h2.message.DbException.get(DbException.java:169) ~[h2-1.3.168.jar:1.3.168]
at org.h2.message.DbException.get(DbException.java:146) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.readTableOrView(Parser.java:4770) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.readTableFilter(Parser.java:1084) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690) ~[h2-1.3.168.jar:1.3.168]
您必须确保创建了表和序列。如果它被创建,那么它将工作 未创建表
“所有序列”
。请检查您的数据库是否存在
您的问题是没有创建序列和表
检查您的hibernate.cfg.xml。它的配置不好。
为便于澄清,我给出了以下建议:
org.h2.Driver
jdbc:h2:数据库/测试
sa
公开的
1.
org.hibernate.dial.h2方言
org.hibernate.cache.internal.NoCacheProvider
真的
创造
如果在persistence.xml
中的持久化单元中使用了错误的方言,或者根据错误的数据库进行验证,就会发生这种情况。例如,在本地H2数据库上运行应用程序时,最好的选择是删除方言,因为Hibernate可以识别没有此属性的数据库(如果Hibernate版本足够新,可以识别较新的数据库)。另一种解决方案是删除validate属性,但我不建议这样做,因为启动时没有数据库检查:
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
我使用了多个数据源
oracledb1配置
@Primary
@Bean(name = "oracleDb1EntityManager")
public LocalContainerEntityManagerFactoryBean oracleDb1EntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(oracleDb1DataSource)
.properties(hibernateProperties())
.packages("com.fengxin58.user.ddd.domain.model.oracle.db1")//设置实体类所在位置
.persistenceUnit("oracleDb1PersistenceUnit")
.build();
}
private Map<String, Object> hibernateProperties() {
String env = monitorService.env();
if(log.isDebugEnabled()) {
log.debug("current profile: {}", env);
}
Resource resource = null;
if(EnvEnum.TEST.key().equals(env)) {
resource = new ClassPathResource("hibernate-oracle-db1-test.properties");
}else {
resource = new ClassPathResource("hibernate-oracle-db1.properties");
}
try {
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
return properties.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey().toString(),
e -> e.getValue())
);
} catch (IOException e) {
return new HashMap<String, Object>();
}
}
@Primary
@Bean(name=“oracleDb1EntityManager”)
公共LocalContainerEntityManagerFactoryBean或EntityManagerFactory(EntityManagerFactoryBuilder){
返回生成器
.dataSource(oracleDb1DataSource)
.properties(hibernateProperties())
.packages(“com.fengxin58.user.ddd.domain.model.oracle.db1”)//设置实体类所在位置
.persistenceUnit(“oracleDb1PersistenceUnit”)
.build();
}
私有映射hibernateProperties(){
字符串env=monitorService.env();
if(log.isDebugEnabled()){
调试(“当前配置文件:{}”,env);
}
Resource=null;
if(EnvEnum.TEST.key().equals(env)){
resource=新类路径资源(“hibernate-oracle-db1-test.properties”);
}否则{
resource=新类路径资源(“hibernate-oracle-db1.properties”);
}
试一试{
Properties Properties=PropertiesLoaderUtils.loadProperties(资源);
返回properties.entrySet().stream()
.collect(collector.toMap)(
e->e.getKey().toString(),
e->e.getValue()
);
}捕获(IOE异常){
返回新的HashMap();
}
}
hibernate-oracle-db1-test.properties
hibernate.hbm2ddl.auto=更新
应用程序测试.yml
神谕:
db1:
数据源:
url:jdbc:h2:mem:test
驱动程序类名:org.h2.driver
用户名:root
密码:
db2:
数据源:
url:jdbc:h2:mem:test
驱动程序类名:org.h2.driver
用户名:root
密码:
在我的例子中,我在应用程序中使用了多个数据源,并使用H2进行测试。
在这种情况下,您必须在数据源的JPA属性中手动设置方言,而不是通过application.properties文件
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return builder
.dataSource(ds)
.packages("com.application.demo")
.properties(properties)
.persistenceUnit("persitenceUnit1")
.build();
Map properties=newhashmap();
properties.put(“hibernate.dial”、“org.hibernate.dial.h2dial”);
返回生成器
.数据源(ds)
.packages(“com.application.demo”)
.物业(物业)
.持久性单位(“持久性单位1”)
.build();
在使用的每个数据源中
以防它能帮助别人
关于所有数据库模式默认都有“所有序列”表吗?所有序列
描述了当前用户可访问的所有序列<代码>DBA_序列
-描述数据库中的所有序列<代码>用户\u序列-描述当前用户拥有的所有序列。此视图不显示SEQUENCE_OWNER列。此答案帮助我找到了问题。它是org.hibernate.dialent.oracle10galent
值,但在我的代码中,它被隐藏在@Bean
方法中的一个Spring@Configuration
类中,该方法创建了一个hibernatejbaindoradapter
。删除Oracle方言属性后,Spring能够自动配置H2,错误也就消失了。
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return builder
.dataSource(ds)
.packages("com.application.demo")
.properties(properties)
.persistenceUnit("persitenceUnit1")
.build();