12 Replies Latest reply on Oct 21, 2008 9:14 AM by Olivier Thierry

    How to configure Hibernate Console in a Maven project ?

    Olivier Thierry Apprentice

      Hi,

      I work on a Maven project with JPA Hibernate implementation for persistence. I would like to make Hibernate console work with this project, but I encounter problems.

      My project has EJB3 entity classes (with JPA annotations) and this persistence.xml file (all in build path and so in classpath) :

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence
       xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
       version="1.0">
       <persistence-unit name="t4Seam">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/jdbc/t4Seam</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.show_sql" value="false"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
       </properties>
       </persistence-unit>
      </persistence>


      At runtime, database connection is configured with a datasource, so I added a hibernate.properties file to configure database connection for Hibernate console.

      hibernate.connection.datasource=java:/jdbc/t4Seam
      hibernate.connection.driver_class=com.mysql.jdbc.Driver
      hibernate.connection.url=jdbc:mysql://localhost:3306/t4Seam
      hibernate.connection.username=T4
      hibernate.connection.password=T4
      hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect


      I then created my Hibernate Console configuration with "JPA" type, selected my hibernate.properties file, configured the persistence unit as "t4Seam" and added MySQL drivers in the classpath.

      For what I understood, this is all I need to do since it looks for required files (EJB3 entities, persistence.xml) in the classpath. But when I jump to the Hibernate configuration view and double click on my configuration, I have the following stack trace :

      Can't find mapping file
      java.io.FileNotFoundException: Mapping for org.hibernate.cfg.AnnotationConfiguration@a9ae3e not found.
      
      java.io.FileNotFoundException: Mapping for org.hibernate.cfg.AnnotationConfiguration@a9ae3e not found.
       at org.hibernate.eclipse.console.actions.OpenMappingAction.run(OpenMappingAction.java:142)
       at org.hibernate.eclipse.console.actions.OpenMappingAction.run(OpenMappingAction.java:92)
       at org.hibernate.eclipse.console.views.KnownConfigurationsView$3.run(KnownConfigurationsView.java:165)
       at org.hibernate.eclipse.console.views.KnownConfigurationsView$1.doubleClick(KnownConfigurationsView.java:100)
       at org.eclipse.jface.viewers.StructuredViewer$1.run(StructuredViewer.java:799)
       at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
       at org.eclipse.core.runtime.Platform.run(Platform.java:857)
       at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:46)
       at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:199)
       at org.eclipse.jface.viewers.StructuredViewer.fireDoubleClick(StructuredViewer.java:797)
       at org.eclipse.jface.viewers.AbstractTreeViewer.handleDoubleSelect(AbstractTreeViewer.java:1387)
       at org.eclipse.jface.viewers.StructuredViewer$4.widgetDefaultSelected(StructuredViewer.java:1173)
       at org.eclipse.jface.util.OpenStrategy.fireDefaultSelectionEvent(OpenStrategy.java:237)
       at org.eclipse.jface.util.OpenStrategy.access$0(OpenStrategy.java:234)
       at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:295)
       at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
       at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
       at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3682)
       at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3293)
       at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2389)
       at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
       at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2219)
       at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
       at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:289)
       at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:461)
       at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
       at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:106)
       at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:169)
       at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
       at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
       at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)
       at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
       at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
       at org.eclipse.equinox.launcher.Main.run(Main.java:1173)


      Which mapping file is it talking about ? It is JPA, so it doesn't need mapping files, doesn't it ? Is there something special to do to make it work with JPA ?

      Thanks in advance for your help ;)

      Olivier

        • 1. Re: How to configure Hibernate Console in a Maven project ?
          Max Rydahl Andersen Master

          Looks like a bug in the openmapping code.

          Please report it in jira - thanks.

          • 2. Re: How to configure Hibernate Console in a Maven project ?
            Olivier Thierry Apprentice

            Thanks Max. I finally could make the configuration node work and now I can see all my EJB3 entity classes when expanding the configuration node, so no more problem with mapping file. The problem is that I don't exactly now how I solved this problem ! I Just made a maven clean then a maven install and it was OK. I will report it if I reproduce the problem, but I think it was my fault, not a problem with JBoss Tools ;)

            Anyway I have another problem when opening Session Factory and Database nodes in Hibernate Configurations view. I think it deals with the way I configured the datasource since it can't find it.

            The stack trace is following :

            org.hibernate.HibernateException: Could not find datasource
             at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
             at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
             at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
             at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
             at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2005)
             at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:99)
             at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:65)
             at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:96)
             at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:58)
             at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:103)
             at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:196)
             at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
            Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
             at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
             at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
             at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
             at javax.naming.InitialContext.lookup(InitialContext.java:351)
             at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
             ... 11 more


            My persistence.xml files refers to a JNDI datasource, so there might be something special to do in hibernate.properties to make it work. Is there an example for this ?

            • 3. Re: How to configure Hibernate Console in a Maven project ?
              Max Rydahl Andersen Master

              look at the hibernate-console.properties seam wizard creates. you can use similar properties to override what the persistence.xml says.

              • 4. Re: How to configure Hibernate Console in a Maven project ?
                Olivier Thierry Apprentice

                Thanks, it helped me a lot. I wrote the following hibernate.properties file :

                hibernate.connection.password=T4
                hibernate.connection.username=T4
                hibernate.connection.driver_class=com.mysql.jdbc.Driver
                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                hibernate.connection.url=jdbc://mysql/localhost/t4Seam
                
                hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
                hibernate.datasource=
                hibernate.transaction.manager_lookup_class=


                Then the Session Factory node is OK. But the Database one is still not :(
                I have a message about not suitable driver. Don't understand why since this is the one I use on the same database on production mode.

                The stack trace :
                org.hibernate.exception.JDBCConnectionException: Getting database metadata
                
                org.hibernate.exception.JDBCConnectionException: Getting database metadata
                 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
                 at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:64)
                 at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.caseForSearch(AbstractMetaDataDialect.java:163)
                 at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getTables(JDBCMetaDataDialect.java:22)
                 at org.hibernate.cfg.reveng.JDBCReader.processTables(JDBCReader.java:476)
                 at org.hibernate.cfg.reveng.JDBCReader.readDatabaseSchema(JDBCReader.java:74)
                 at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:105)
                 at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:65)
                 at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:96)
                 at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:58)
                 at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:103)
                 at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:196)
                 at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
                Caused by: java.sql.SQLException: No suitable driver
                 at java.sql.DriverManager.getConnection(DriverManager.java:545)
                 at java.sql.DriverManager.getConnection(DriverManager.java:140)
                 at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
                 at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getConnection(AbstractMetaDataDialect.java:122)
                 at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:61)
                 ... 11 more


                Do you have any idea why it says my driver is not suitable ?

                P.S. : about my initial problem ("can't find mapping file"), it occurs when you double click on nodes. But when you expand them there is no problem ;)

                • 5. Re: How to configure Hibernate Console in a Maven project ?
                  Max Rydahl Andersen Master

                  look for possible spaces in the property file that confuses the jdbc driver lookup.

                  make sure your jdbc driver is on the classpath of the console configuration (or through the associated project)

                  • 6. Re: How to configure Hibernate Console in a Maven project ?
                    Olivier Thierry Apprentice

                    I tried with HSLQDB instead of MySQL and it works perfect. So it is a problem with MySQL driver. I use mysql-connector-java-5.0.3.jar archive. Are there known problems with this one ?

                    • 7. Re: How to configure Hibernate Console in a Maven project ?
                      Max Rydahl Andersen Master

                      does the driver classname actually exist in the jar you are pointing to ?

                      • 8. Re: How to configure Hibernate Console in a Maven project ?
                        Olivier Thierry Apprentice

                        Yes, it is the one I use when running my app on JBoss Application Server. So it looks like there's something that Hibernate Console uses and that it not supported by this MySQL driver. I find it strange ...

                        • 9. Re: How to configure Hibernate Console in a Maven project ?
                          Max Rydahl Andersen Master

                          could you show me the *full* stacktrace ?

                          Just need to check, but I do believe this is just a pure driver lookup nothing hibernate tools specific about it.

                          • 10. Re: How to configure Hibernate Console in a Maven project ?
                            Olivier Thierry Apprentice

                            Changing back my project to MySQL, it works now ... But I can't find what made it work ! I use the same driver, hibernate.properties file is the same, persistence.xml file too ! I will report again if I find what caused the problem in case other people encounter the same problems. Thanks a lot anyway for your help ;)

                            • 11. Re: How to configure Hibernate Console in a Maven project ?
                              Olivier Thierry Apprentice

                              Hi,

                              I had not used Hibernate console for a long time, but my problem with MySQL is back with MySQL ... I have the following stack trace :

                              org.hibernate.exception.JDBCConnectionException: Getting database metadata
                               at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
                               at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:64)
                               at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.caseForSearch(AbstractMetaDataDialect.java:163)
                               at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getTables(JDBCMetaDataDialect.java:22)
                               at org.hibernate.cfg.reveng.JDBCReader.processTables(JDBCReader.java:476)
                               at org.hibernate.cfg.reveng.JDBCReader.readDatabaseSchema(JDBCReader.java:74)
                               at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:105)
                               at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:65)
                               at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:96)
                               at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:58)
                               at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:103)
                               at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:196)
                               at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
                              Caused by: java.sql.SQLException: No suitable driver
                               at java.sql.DriverManager.getConnection(DriverManager.java:545)
                               at java.sql.DriverManager.getConnection(DriverManager.java:140)
                               at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
                               at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getConnection(AbstractMetaDataDialect.java:122)
                               at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:61)
                               ... 11 more


                              Upgraded from 5.0.3 to 5.1.6, same problem. Tried with HSQLDB or Oracle, no problem, so this is a problem with MySQL.

                              My connection is configured this way in hibernate.properties :

                              hibernate.connection.password=et4
                              hibernate.connection.username=et4
                              hibernate.connection.driver_class=com.mysql.jdbc.Driver
                              hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect
                              hibernate.connection.url=jdbc:mysql/srv-r1-be01:3307/et4
                              


                              And in my classpath I added the jar with all my entities and the jar containing the MySQL driver.

                              Any idea where it comes from ?

                              • 12. Re: How to configure Hibernate Console in a Maven project ?
                                Olivier Thierry Apprentice

                                Shame on me ... My connection url was wrong.

                                It works with this one :

                                jdbc:mysql://srv-r1-be01:3307/et4


                                instead of this one :

                                jdbc:mysql/srv-r1-be01:3307/et4