gpt4 book ai didi

java - Hibernate 5 + Eclipse RCP + SQLite 3 - 无法将名称解析为策略 org.hibernate.dialect.Dialect

转载 作者:IT王子 更新时间:2023-10-29 06:29:44 25 4
gpt4 key购买 nike

我有 Eclipse RCP 应用程序。具有 hibernate 功能的 JAR 位于另一个插件中,该插件包含在 MANIFEST.MF 的主项目中。

我尝试建立简单的连接 Hibernate -> SQLite DB 使用代码:

try {
Configuration lConf = new Configuration();
lConf.addAnnotatedClass (eDocument.class);
lConf.setProperty("hibernate.dialect", "app.sqlite.SQLiteDialect");
lConf.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
lConf.setProperty("hibernate.connection.url", "jdbc:sqlite:sfOrders.db");
lConf.setProperty("hibernate.connection.username", "");
lConf.setProperty("hibernate.connection.password", "");

StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(lConf.getProperties());
_sessionFactory = lConf.buildSessionFactory(builder.build());
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}

我使用自定义 SQLiteDialect,但出现错误:

java.lang.ExceptionInInitializerError
at app.sqlite.DatabaseConnector.connect(DatabaseConnector.java:57)
at views.TransactionManagerView$6.widgetSelected(TransactionManagerView.java:144)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at smoothflow.Application.start(Application.java:25)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
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:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at app.sqlite.DatabaseConnector.connect(DatabaseConnector.java:55)
... 29 more
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [app.sqlite.SQLiteDialect] as strategy [org.hibernate.dialect.Dialect]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:162)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:126)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:120)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74)
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:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 42 more

我尝试在空的普通 Java 项目中使用相同的 hibernate 配置 Java 代码编译它并且它有效

  • db: sfOrders.db 存在
  • 没有 lConf.addAnnotatedClass (eDocument.class) 我得到同样的错误
  • 我不能使用 xml/hbm 配置

编辑:从我在源代码 [第 113 行] 中看到的情况来看,它一定与 Hibernate 加载类的方式有关,没有简单的 Class.forName,而是 classLoaderService: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java

我试过用这个替换 hibernate.dialect:

lConf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

当然,还有其他错误,因为我使用的是 SQLite(不是 MySQL),但 MySQLDialect 已被 StrategySelectorImpl.class 正确找到。

为了 100% 确定我的 app.sqlite.SQLiteDialect 存在,我创建了代码:

Class lClass = Class.forName("app.sqlite.SQLiteDialect");

lClass 不为空


编辑:我看到 hibernate 缓存了某种加载程序,它试图在我的 Eclipse RCP 项目中使用 JAR 而不是主项目(其中包含带有 JAR 的项目)来查找类。我将方言更改为 MySQLDialect.class,我得到的下一个错误是:

ClassNotFoundException: app.eCommerceCalls.eDocument cannot be found by SFLib_1.0.0

eDocument.class 存在但在主项目中(不在带有 JAR [包括 hibernate] 的 Eclipse RCP 项目中)。 SFLib_1.0.0 - 带有 JAR 的项目


编辑:所以问题是,如何使主 RCP 项目中的类对于 Eclipse RCP 项目中带有库的 JAR 中的类可见。

所以在我的例子中,来自 SF1.0.0 的 BundleLoader.class 无法从主项目 eDocument.class 中找到一个类。

最佳答案

尝试替换这一行:

    lConf.setProperty("hibernate.dialect", "app.sqlite.SQLiteDialect");

与:

lConf.setProperty("dialect", "app.sqlite.SQLiteDialect");

关于java - Hibernate 5 + Eclipse RCP + SQLite 3 - 无法将名称解析为策略 org.hibernate.dialect.Dialect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com