1

I am aware that there are plenty of information around explaining how to use Swing + Hibernate and Maven in a project, but I think that all this info is way out-dated. I just tried to follow this and i just ended up having lots of import problems, once apparently the new versions of Hibernate-core dont have some of the classes that it had at the time of this tutorial (I laugh a bit when I saw a code using Vector (-: )

Question is, is it possible to create a project using the latest versions of Hibernate with Swing? Does any one have information on how to do it that is not from 3 years ago?

Issues I had :

1 - Couldnt get HQL Query to work on Netbeans... it complains about

<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>

Cant find it.. tried other options but no luck (I dont really care about this)

2 - When running code, it complains about

Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
Which is a package from early days of hibernate.

Would appreciate all the help, even pointing me towards a good documentation...

Cheers

UPDATE : Stack Trace

--- exec-maven-plugin:1.2.1:exec (default-cli) @ SwingProject ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Apr 01, 2014 4:48:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
Apr 01, 2014 4:48:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 01, 2014 4:48:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at padaria.util.NewHibernateUtil.<clinit>(NewHibernateUtil.java:30)
    at br.com.vaast.main.AdminMain.executeHQLQuery(AdminMain.java:43)
    at br.com.vaast.main.AdminMain.runQueryBasedOnFirstName(AdminMain.java:34)
    at br.com.vaast.main.AdminMain.jButton1ActionPerformed(AdminMain.java:206)
    at br.com.vaast.main.AdminMain.access$200(AdminMain.java:21)
    at br.com.vaast.main.AdminMain$3.actionPerformed(AdminMain.java:120)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.hibernate.cfg.Configuration.reset(Configuration.java:353)
    at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:289)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
    at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:168)
    at padaria.util.NewHibernateUtil.<clinit>(NewHibernateUtil.java:26)
    ... 41 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 59 more
TW.
  • 27
  • 1
  • 12

1 Answers1

2

Things I’ve tried/checked:

If you download the project that is described in the tutorial and then checking the pom.xml you will see that the libraries used are quite old. I believe that the project is built with an older version of Netbeans. If you check this tutorial: https://netbeans.org/kb/docs/java/hibernate-java-se.html , which is actually the same one but without using maven, it is mentioned that “NetBeans IDE 7.4 and earlier bundled Hibernate 3”.

If you try to follow the tutorial that builds the project without the use of Maven: https://netbeans.org/kb/docs/java/hibernate-java-se.html and you work with NetBeans 8.0 you will get a picture of the Hibernate 4.x libraries needed. That is because the IDE by default comes bundled with Hibernate 4.x libraries. Going back and building the project with Maven this time I got the following in my pom.xml

<repositories>
    <repository>
        <id>unknown-jars-temp-repo</id>
        <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
        <url>file:${project.basedir}/lib</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.8.Final</version>
    </dependency>
    <dependency>
        <groupId>unknown.binary</groupId>
        <artifactId>hibernate-jpa-2.0-api-1.0.1.Final</artifactId>
        <version>SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>unknown.binary</groupId>
        <artifactId>mysql-connector-java-5.1.23-bin</artifactId>
        <version>SNAPSHOT</version>
    </dependency>
</dependencies>

Solution that worked for me.

The above pom.xml provided some insight but there are some parts that made me unhappy: <groupId>unknown.binary</groupId> and temporary repository.

So I started searching and finally here: http://hibernate.org/orm/documentation/getting-started/ and here I got what I needed: http://hibernate.org/orm/documentation/getting-started/.

I changed my pom.xml to :

<dependencies>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.5.Final</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>

Everything is working fine with latest version of Hibernate.

NOTE:

as you can see from the links above you have two options. You can either use <artifactId>hibernate-entitymanager</artifactId> or <artifactId>hibernate-core</artifactId>.

I believe that these references can provide some answers:

NOTE 2:
I had some trouble with org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory. In order to build the project I had to use the version without internal. If I did use the internal version then the step Creating Hibernate Mapping Files and POJOs From a Database could not be completed. I had to revert back to classic to complete the step and change it back to internal at the end.

Hope that all the above are useful to you.

Community
  • 1
  • 1
zlinks
  • 1,037
  • 2
  • 16
  • 26
  • Hi zlinks ! I was very happy with your anwser, looks like you have put lots of effort on it and im thankfull already for that. I tried to download and build the project from the link that doesnt use maven but i have no idea how to transform this project in a maven project, so i can declare the hibernate libs. I am using NB 8.0, but when i imported the project, it complained that it couldnt find some of the hibernate libraries (HibernateException, Query and Session for example). Will have a look around and see how can i transform this project in a maven project. – TW. Apr 07 '14 at 02:15
  • Thanks man, got it working... had to work some stuff out but overall your post was a saviour. I do have one more question though, is it possible to do all that without having to do all the mapping manually? the project that I want to migrate to Swing was a JSF project that had all the necessary annotations to avoid all the mapping. Thanks for all your help! – TW. Apr 07 '14 at 02:47
  • Hi TW. I am glad that it helped you. I believe you can take all the annotated classes, that describe your model structure, from your JSF project and put them as is in your Swing application. You might need to alter the xml files of your project then. Try to do the reverse engineering step again but this time check `JDK 5 language features` and `EJB3 Annotations` to see how the project works with annotations. – zlinks Apr 07 '14 at 14:15
  • also check: http://stackoverflow.com/questions/1263425/hibernate-with-annotations-how-to-get-started – zlinks Apr 07 '14 at 14:38