如何解决将Elastic Search JDBC驱动程序与Hibernate / Spring Boot集成
我正在尝试从中集成Elastic Driver“ org.elasticsearch.xpack.sql.jdbc.EsDriver”
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
<version>7.10.0</version>
</dependency>
使用Hibernate进入我的spring boot应用程序。
在我的spring配置bean中,我具有以下内容:
@Bean
@ConfigurationProperties(prefix = "db.elastic")
@Qualifier("elasticDataSource")
@Primary
public DataSource elasticDataSource() {
return DataSourceBuilder.create()
.build();
}
public LocalContainerEntityManagerfactorybean elasticEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
Map<String,Object> properties = new HashMap<>();
properties.put(AvailableSettings.HBM2DDL_AUTO,"none");
properties.put(AvailableSettings.HBM2DLL_CREATE_SCHEMAS,"false");
properties.put(AvailableSettings.DIALECT,org.elasticsearch.xpack.sql.jdbc.EsDriver.class.getName());
return builder
.dataSource(elasticDataSource())
.packages(Issuer.class)
.persistenceUnit("elastic")
.properties(properties)
.build();
}
但是,当我运行此代码时,出现以下异常:
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.Metadata(EntityManagerFactoryBuilderImpl.java:904)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
at org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean.createNativeEntityManagerFactory(LocalContainerEntityManagerfactorybean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerfactorybean.buildNativeEntityManagerFactory(AbstractEntityManagerfactorybean.java:390)
at org.springframework.orm.jpa.AbstractEntityManagerfactorybean.afterPropertiesSet(AbstractEntityManagerfactorybean.java:377)
at org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean.afterPropertiesSet(LocalContainerEntityManagerfactorybean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.invokeInitMethods(AbstractAutowireCapablebeanfactory.java:1837)
at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.initializeBean(AbstractAutowireCapablebeanfactory.java:1774)
... 16 common frames omitted
Caused by: org.hibernate.HibernateException: Unable to construct requested dialect [org.elasticsearch.xpack.sql.jdbc.EsDriver]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:84)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentinitiator.initiateService(JdbcEnvironmentinitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentinitiator.initiateService(JdbcEnvironmentinitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
... 33 common frames omitted
Caused by: java.lang.classCastException: org.elasticsearch.xpack.sql.jdbc.EsDriver cannot be cast to org.hibernate.dialect.Dialect
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74)
... 38 common frames omitted
我认为这是因为驱动程序与休眠不兼容。我是正确的还是必须执行其他配置来解决此问题?
这里还有一个商用的JDBC驱动程序:https://www.cdata.com/drivers/elasticsearch/jdbc/
有人有没有使用过该驱动程序的经验,并且与Hibernate兼容?
解决方法
我不了解ElasticSearch驱动程序,但是我会告诉您为什么会出现错误。
您的错误是:由于:java.lang.ClassCastException:org.elasticsearch.xpack.sql.jdbc.EsDriver无法转换为org.hibernate.dialect.Dialect
这是因为您的属性有问题:
properties.put(AvailableSettings.DIALECT,org.elasticsearch.xpack.sql.jdbc.EsDriver.class.getName());
EsDrver.class.getName()不是休眠的方言。
示例休眠方言是:org.hibernate.dialect.MySQL5Dialect
请仔细阅读https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-jdbc.html
看到您正在使用Springboot,您甚至不必手动配置数据源/实体管理器。只需添加依赖项即可对其进行自动配置。
, ElasticSearch SQL方言没有方言,但是您可以尝试使用https://www.cdata.com/kb/tech/elasticsearch-jdbc-hibernate.rst中建议的org.hibernate.dialect.SQLServerDialect
最后,您可能必须覆盖方言中的一些配置以适合ES支持的SQL方言。
,因此,我发现org.hibernate.dialect.H2Dialect或org.hibernate.dialect.PostgreSQL9Dialect可以工作,但仅限于此。
首先,您还必须在实体名称中加上双引号:
@Data
@Table(name = "\"mytable\"")
@Entity
public class MyTable {
}
此外,我发现设置maxRows不起作用。由于某种原因,此值在生成的Sql中不受限制:
18:14:29.320 [http-nio-8081-exec-1] DEBUG org.hibernate.SQL -
select
mytabl_.NAME as col_0_0_
from
"mytable" mytabl_
order by
mytabl_.NAME asc limit ?
18:14:29.651 [http-nio-8081-exec-1] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0,SQLState: null
18:14:29.651 [http-nio-8081-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - line 1:108: mismatched input '?' expecting {'ALL',INTEGER_VALUE}
18:14:29.658 [http-nio-8081-exec-1] WARN g.e.SimpleDataFetcherExceptionHandler - Exception while fetching data (/Issuers) : line 1:108: mismatched input '?' expecting {'ALL',INTEGER_VALUE}
org.hibernate.JDBCException: line 1:108: mismatched input '?' expecting {'ALL',INTEGER_VALUE}
有人对为什么会这样有任何想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。