5

I have Eclipse RCP Application. JARs with hibernate are in another plugin which is included in the main project in MANIFEST.MF.

I try to establish simple connection Hibernate -> SQLite DB using code:

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);
}

I use custom SQLiteDialect and i get error:

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

I tried to compile it with the same hibernate configuration Java code in empty normal Java project and it works

  • db: sfOrders.db exists
  • without lConf.addAnnotatedClass (eDocument.class) i get the same error
  • i cant use xml/hbm configs

EDIT: it must be something with the way Hibernate load classes as from what I see in sources [Line 113], there is no simple Class.forName but classLoaderService: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java

I have tried replace hibernate.dialect with this:

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

Of course, there are other errors as I use SQLite (not MySQL) but MySQLDialect is found properly by StrategySelectorImpl.class.

To be 100 % sure that my app.sqlite.SQLiteDialect exists i created code:

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

lClass is not null


EDIT: I see that hibernate caches some kind of loader and it tries to find class inside my Eclipse RCP project with JARs instead of main project (in which the project with JARs is included). I changed dialect to MySQLDialect.class and next error i got is:

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

eDocument.class exists but in main project (not in Eclipse RCP Project with JARs [including hibernate]). SFLib_1.0.0 - project with JARs


EDIT: So the question is, how to make classes from main RCP project visible for classes inside JAR from Eclipse RCP Project with libraries.

so in my case, the BundleLoader.class from SF1.0.0 can't find a class from main project eDocument.class.

Arghavan
  • 1,125
  • 1
  • 11
  • 17
Bartek Szczypien
  • 333
  • 4
  • 17

3 Answers3

1

Try to replace this line:

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

With:

lConf.setProperty("dialect", "app.sqlite.SQLiteDialect");
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • I tried but i got the same error: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.SQLiteDialect] as strategy [org.hibernate.dialect.Dialect] – Bartek Szczypien Feb 02 '16 at 06:43
  • @BartekSzczypien try to replace hibernate.dialect with dialect : befor take a look at ex: above – Abdelhak Feb 02 '16 at 07:10
  • i see that i dont have org.hibernate.dialect.SQLiteDialect class, from what i checked org.hibernate.dialect package is inside hibernate-core-5.0.6.Final.jar but SQLiteDialect is missing Where can i find default SQLiteDialect that comes from hibernate? (if it does exist in hibernate JARs)? – Bartek Szczypien Feb 02 '16 at 07:10
  • @BartekSzczypien ok use app.sqlite.SQLiteDialect – Abdelhak Feb 02 '16 at 07:12
  • it doesnt work, i have tried and this time i got: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set It must work as i have checked in empty project, there is some problem with the way hibernate load classes. The source of StrategySelectorImpl is here [Line 113]: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorImpl.java – Bartek Szczypien Feb 02 '16 at 07:15
0

The issue is resolved. The problem was in bundle visibility. The solution was to add into MANIFEST.MF (under Runtime tab), hibernate mapped classes to make them available to reach from bundle Eclipse plugin where hibernate was added

Bartek Szczypien
  • 333
  • 4
  • 17
  • Can you be more explicit about this answer, perhaps with a code snip? I'm having a similar problem and I don't understand your solution. – BadZen May 31 '17 at 17:36
  • At first I strongly NOT recommend using SQLite with Hibernate as there is no support for SQLite. I added hibernate mapped to database entities / classes to MANIFEST.MF/runtime/Exported Packages and It starts working. So generally if You want to have hibernate JAR in another Eclipse Plugin / package, all the mapped entities should be visible for this package. – Bartek Szczypien Jun 02 '17 at 05:07
0

It seems like the default collection of ClassLoaders that Hibernate starts with does not have access to the "app.sqlite.SQLiteDialect" class. Since you are booting it up with a manual configuration, rather than just a persistence.xml, then a solution would be to pass the class reference, rather than the name. In that case it might be better if you use the JPA boot-up through the Persistence class, using Persistence.createEntityManagerFactory(String unitName, Map props). That way it isn't limited to only taking strings.

Without moving to JPA-boot, this should work for 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();

Using setProperties bypasses the typecast to String.

coladict
  • 4,799
  • 1
  • 16
  • 27