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

时间:2016-02-01 22:17:11

标签: java eclipse hibernate sqlite rcp

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

我尝试使用代码建立简单连接Hibernate -> SQLite数据库:

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 configs

编辑: 它必须是Hibernate加载类的方式,就像我在源代码中看到的那样[第113行],没有简单的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)还有其他错误,但是StrategySelectorImpl.class正确找到了MySQLDialect。

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

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

lClass不为空

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

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

eDocument.class存在,但在主项目中(不在Eclipse RCP Project with JARs [包括hibernate])。 SFLib_1.0.0 - 使用JAR项目

编辑: 所以问题是,如何从带有库的Eclipse RCP Project中为JAR中的类显示主RCP项目中的类。

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

3 个答案:

答案 0 :(得分:1)

尝试替换此行:

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

使用:

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

答案 1 :(得分:0)

问题已解决。问题在于捆绑可见性。解决方案是添加到MANIFEST.MF(在Runtime选项卡下),hibernate映射类使它们可以从添加了hibernate的bundle Eclipse插件中获取

答案 2 :(得分:0)

似乎Hibernate启动的ClassLoader的默认集合无法访问“app.sqlite.SQLiteDialect”类。由于您使用手动配置启动它,而不仅仅是persistence.xml,因此解决方案是传递类引用,而不是名称。在这种情况下,如果您使用Persistence.createEntityManagerFactory(String unitName, Map props)通过Persistence类使用JPA启动可能会更好。这样,它不仅限于使用字符串。

如果不转向JPA-boot,这应该适用于5.2:

Properties props = new Properties();
Configuration lConf = new Configuration();
lConf.addAnnotatedClass (eDocument.class);
props.put("hibernate.dialect", app.sqlite.SQLiteDialect.class);
props.put("hibernate.connection.driver_class", "org.sqlite.JDBC");
props.put("hibernate.connection.url", "jdbc:sqlite:sfOrders.db");
props.put("hibernate.connection.username", "");
props.put("hibernate.connection.password", "");
lConf.setProperties(props);
lConf.buildSessionFactory();

使用setProperties绕过类型转换为String