Getting Hibernate3 to work in JDeveloper 10.1.3

Our company focuses mainly on Oracle related products. Our Java developers use JDeveloper in combination with JHeadstart to build web applications. But that doesn’t mean we’re not interested and involved in other technologies as well. We are investing lots of time into frameworks like Spring, Java Server Faces and Hibernate. In this article I will describe how I got Hibernate3 to work in Jdeveloper 10.1.3 and an Oracle 10g database. In order to get Hibernate to work in JDeveloper, I followed these steps:

Create a DataSource to use within my application

Use the DataSource from within a JSP page

Enable Hibernate on my project

Configure Hibernate to query a table in my database using a servlet

1. Create a DataSource to use within my application Creating a DataSource is quite simple. In JDeveloper, first open the Connections Navigator tab (select “View -> Connection Navigator” or press [CTRL-SHIFT-O]) and create a new database connection. In my case I used the OE schema on our local play-around database. Then, select “Tools -> Embedded OC4J Server Preferences” and make sure the database connection is listed there. I chose to only use this connection in the current workspace. Since I named my database connection “oe” I have an entry called “jdev-connection-oe” in my Data Sources list.

Inspecting the properties for this DataSource I notice the JNDI Name is “jdbc/oeDS”. If your connection doesn’t show up, click the [Refresh Now] button in the “Data Sources” section.

2. Use the DataSource from within a JSP page For testing the DataSource I created a simple JSP page connecting to the DataSource and querying a table. Since I am using the OE schema I decided to query the CUSTOMERS table. First I created a new workspace in JDeveloper and created an empty project in this workspace. I then added a JSP page by right clicking the project name in the “Applications Navigator” and selecting “New…”. From the “New Gallery” window I opened “Web Tier” in the Category list and selected JSP. From the Items list I selected JSP and named it “index.jsp”. In the Tag Libraries section I selected “JSTL Core 1.1″ and “JSTL SQL 1.1″ and I then clicked [Finish]. This prepared my project for being a web application and made sure the needed SQL taglib is enabled on this project. Next, I added code to query my DataSource. To do so, I selected “JSTL 1.1 SQL” in the “Components Palette” and clicked “Query”. In the “Enter Common Properties” section I entered the value “queryresults” for my variable and in the “Enter Advanced Properties” section I entered the value “jdbc/oeDS” (which is the JNDI name for my DataSource, remember?) for the DataSource. I also enetered “SELECT * FROM CUSTOMERS ORDER BY CUSTOMER_ID” as value for the “Sql” field. In order to display the query results, I entered this code below the query statement:

3. Enable Hibernate on my project Next I’d like to do the same thing using Hibernate. In order to be able to do that, I first have to prepare both my project and JDeveloper for Hibernate3. Hibernate3 can be downloaded from this link. For preparing this article, I used Hibernate version 3.0.5. Simply download the .zip or .tar.gz file to your hard disk, unpack it and remember where you put it. One small pitfall of the JDeveloper 10.1.3 and Hibernate 3 combination, is the fact that Hibernate 3 uses slightly newer ASM and ANT jar files, which are not compatible with JDeveloper 10.1.3. Fortunately, for both there is a workaround. In order to make the ASM jars that come with Hibernate3 work with JDeveloper 10.1.3, I went to the directory where JDeveloper was installed into. There is an asm.jar file hidden in the j2ee/home/lib directory. I replaced this jar file with the asm.jar file shipped with Hibernate3 to prevent the “java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit” error as described in this post from happening. After replacing this jar file, I restarted JDeveloper to be sure the new jar file was picked up. Next I imported the hibernate3.jar and required third party jar files into my project. In order to do so, I right clicked my project again and selected “Project Properties…” again. In the Libraries section I clicked “Add Library…” and then “New…”. I chose a name for the new library (“Hibernate3″) and then clicked the “Add Entry…” button. Next I browsed to the directory containing the hibernate3.jar file and selected it. After this I clicked the “Add Entry…” button again and went to the “lib” directory below the one containing the hibernate3.jar file. Here I selected “antlr-2.7.5H3.jar”, “asm.jar”, “cglib-2.1.jar”, “commons-collections-2.1.1.jar”, “commons-logging-1.0.4.jar”, “dom4j-1.6.jar” and “echache-1.1.jar”. Please note that the Hibernate documentation specifies Log4j to be an optional jar to be loaded. Doing so with JDeveloper 10.1.3 will generate an error when the OC4J server is started up, so don’t include this one. After having added the jar files, click [OK] and make sure the “Export” checkbox is checked. This will ensure the libraries will be available in the OC4J server.

4. Configure Hibernate to query a table in my database using a servlet So, now I was able to create my Hibernate table mapping and use this in a servlet. Since I decided to query the CUSTOMERS table in the OE schema, I created a POJO called Customer.java which has private members for id, custFirstName and custLastName as well as getters and setters for these members. Next I created a configuration file called Customer.hbm.xml like this. I right clicked the project and selected “New…”. I then expanded the “General” category and selected “XML”. In the “Items” section, I selected “XML Document”. I made sure it’s called “Customer.hbm.xml” and was put it the public_html/WEB-INF/classes directory. Since this directory didn’t exist yet, I created it. Please note this config file can be put anywhere, as long as the reference to it in the hibernate.cfg.xml file, which we will create in a moment, references it correctly. I just chose to put it there so I can keep all my Hibernate config files together. Next I made sure the contents of the “Customer.hbm.xml” file looks like this:

Remember I told you there was a problem related to the versions of ANT being different between JDeveloper 10.1.3 and Hibernate3? This link tells you what is the cause of the problem. It also says to either make sure the Hibernate3 jar files are loaded before the BEA Weblogic (or in our case, the OC4J) jar files, or to use the Hibernate 2.1 query parser. Here you can find that setting the “hibernate.query.factory_class” to “org.hibernate.hql.classic.ClassicQueryTranslatorFactory” will tell Hibernate3 to use the Hibernate2.1 query parser, which solved the problem of the “ClassNotFoundException: org.hibernate.hql.ast.HqlToken” error I got. By the way, I found the link to this workaround here.

So, the last thing I had do now was to create a servlet that uses Hibernate to query my database and disply the results. Again I right clicked the project and selected “New…”. This time I expanded the “Web Tier” category and selected “Servlets”. In the “Items” section I selected “HTTP Servlet”. I named the servlet “HibernateServlet” and put it in the “nl.amis.wouter.servlets” package. I also selected the methods “doGet()” and “doPost()” to be created. I then specified no parameters to be taken into account but I did specify a name and a mapping for the servlet, which were “HibernateServlet” and “/hibernateservlet” respectively. I then clicked [Finish]. When I was done modifying the code, my servlet looked like this:

Upon starting the servlet by selecting it in the Code Editor and hitting the [F11] key, I saw this output appear in my browser window:

Yes, it looks pretty much the same. As a matter of fact, the output IS the same. So, it works!

Please note this article only goes to prove it is possible to get Hibernate3 and JDeveloper 10.1.3 work together. I know my SQL code isn’t the most effective in the world (which is the first reason for me to want to use a mapping framework like Hibernate). Besides that, I am not sure what the effect of replacing the asm.jar file in JDeveloper will do in the long run. Finally, using the Hibernate2.1 query parser rather than the Hibernate3 version may very well mean some of the new Hibernate features may not work as expected. But it’s a start and I am happy about it

Hi, I am trying to develop an application using hibernate, but it seems I am not able to create a build.xml file that works I have been looking at some manuals but it doesnÂ´t seem to work. Everyone gives a solution, but they donÂ´t work. Can you help me out? please… Regards

I don’t know what’s changed in jdev, but I basically followed the main steps and _it_just_worked_. I didn’t futz with the asm.jar or the 2.1 query parser or anything. Also, I did my example by moving the query code into a separate facade-like class that returns a List, added a jsp:useBean tag to create an instance of the facade-like class, and then just added another c:foreach that sets the list as the items and then loops over them (ie. no raw servlet code required). And the hibernate generated table displays right under the sql tag generated table.

Thank you very much for your comments. I am aware of making OC4J load local classes and jars before classes and jars shipped with OC4J. Please be aware that on JDeveloper 10.1.2 setting this option in oc4j-config.xml is overwritten everytime JDeveloper is started. In one of our projects we ended up making this file readonly. This problem does not occur with JDeveloper 10.1.3. I will definately try loading local ASM jars out on JDeveloper 10.1.3 in an Hibernate3 context, cause I haven’t tried that. It certainly sounds like a better option than to completely replace the OC4J jars.

As this code runs normal in the jvm, it didnâ€™t work any more ones I had deployed to oc4j and ran it from a servlet.

When I started the debugger, I noticed that the â€˜readerâ€™ variable in the code example above, was of type oracle.j2ee.ws.wsdl.xml.WSDLReaderImpl instead of type javax.wsdl.xml.WSDLReader.

Apparently oc4jâ€™s class-loader canâ€™t resolve the wsdl4j.jar and uses the standard webservice jar files that are contained in the Oracle JAX-RPC CLIENT library that come with the jdeveloper installation. If I am not mistaken these are located at â€¦\jdev1013\webservices\lib\ .

I also notices that in this lib directory oracle contains jar files for saaj, jax-rpc ect.

Does this mean you are tied down to using the oracle implementationâ€™s of these components, or is there a way to use libraries of your chosing ?

If not I have a huge problem, either I refactor my code to work with the oracle libraries or I abandon jdeveloper completely.

Without being able to deploy my own jar files to oc4j, jdeveloper is of no use to me.. unfortunately because until now I found it a great tool.

I see that you are managing the connection pooling through the oc4j container. We are using C3P0 for this. However, as we deploy to production, we probably should let the container manage the connection pool, so thanks for the help. Do you have any thoughts on C3P0 vs. oc4j managed data sources?

Another question…Is it possible to let the oc4j container manage the SessionFactory through JNDI? Not just the connection pool, but the entire factory. I’m concerned about running out of of Hibernate sessions in a high traffic website.

Would you know why the project can’t file the servlet? When I look at the project directory, there is a classes folder with the package hierarchy and compiled servlet. However, there are no .jar files created, and I believe that the application is not finding the compiled class files on its classpath.

Can you please check and verify that your web.xml really does contain the correct settings? I noticed that the settings are WRONG in the article. Every tag, e.g. the tag with name “res-ref-name” and value “jdbc/oeDS” should have a closing tag as well. If they are missing, please add them and try again.

In our email conversation you told me to replace the antlr jar file shipped with JDeveloper/OC4J with the Hibernate one and try again. I ran into this same issue while writing this article and I’m sorry I didn’tr mention it here. Thanks for pointing it out to me though!

Pleased to read your interesting article, helped me to workaround the ast.HqlToken error. Instead of it, i get now a funny error when i’m playing with Hibernate. if I ever try a Hql query such as “from Cats where kitten.color = ‘black'”, kitten as a set of cat, i get an illegal query exception !! Seems that the deep-in query skill is not included with the classic hql parser. I’m trying to fix it putting the antlr library in the oc4j classpath, if it’s possible. Any suggestions ? Am I going by the narrow path ? Any easier solution ?

The tutorial at the link you provide also tells you what config files to create and what Java code to write. Just follow the example and you should get the hang of it. Perhaps you might also want to follow the first chapter of the Hibernate reference documentation at

I am glad my article was of help to you and anybody else who appreciated it. Unfortunately, I am not aware of any Hibernate mapping tools for JDeveloper. If you, or anyone else, would ever find out, please let me know!

This is a very nice article and answers many Hibernate/JDeveloper integration questions…

I have one question though. Is there any tool to help Hibernate mapping in JDeveloper. I know Hibernate Tools can be used with Eclipse and TopLink has the nicest tool in JDeveloper. I wonder if there’s a tool like this to help Hibernate/JDeveloper…

I have tried using Hibernate for the first time and I was successful and thanks a lot for the same. I had a small problem while adding the Hibernate Library. I had selected the directory where the Hibernate.jar was present and then selected the specified jar files from the lib folder. But still the Hibernate.jar was not available and it was throwing compilation errors and I had to add the Hibernate.jar once again ( instead of the directory ) and overcame the problem.

Thanks a lot for pointing me to this link. As a matter of fact, I already have seen it and followed the instructions in it to get Hibernate to work with JDeveloper. Unfortunately, the tutorial leads to enabling Hibernate2.1 and I wanted to have Hibernate3.

meta

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 206 other subscribers

Email Address

About

AMIS is internationally recognized for its deep technological insight in Oracle technology. This knowledge is reflected in the presentations we deliver at international conferences such as Oracle OpenWorld, Hotsos and many user conferences around the world. Our AMIS Technology Blog, the most referred Oracle technology knowledge base outside the oracle.com domain. However you arrived here, we appreciate your interest in AMIS. Link to our Google+ Profile AMIS