Using Eclipse Dali with Hibernate as Persistence Provider
Eclipse Ganymede comes with in built support for the EclipseLink JPA implementation. However, we’ll want to explore how to use Hibernate’s JPA implementation with Eclipse Dali for JPA operations
To begin with, please make sure you have the following
MySQL connector (or a jdbc connector of your choice if you are not using MySQL)
You will need to install Hibernate Tools as an Eclipse plugin. Next, you’ll have to add MySQL to your list of driver definitions. This you do by going to the “Window” → “Preferences”. On the left tree, navigate to “Data Management” → “Connectivity” → “Driver Definitions”. Click on the “Add” button. You now have a “New Driver Definition” window with three tabs – Name/Type, Jar List and Properties.
In the Name/Type tab, select MySQL. A list of choices now appears of “MySQL JDBC Driver”. Select System Version 5.0, click on the “Jar List” tab and delete the default MySQL driver file specified there. Click on “Add Jar/Zip” and add your MySQL connector. Please ensure that there are no spaces in the path to your MySQL connector, or you may have trouble later on. Click “Ok” to finish the driver definition.
Next is to add the Hibernate EntityManager libraries as your JPA implementation. First, extract the Hibernate EntityManager zip that you downloaded into a location without spaces in the path. Still in the “Preferences” window, click on “JPA” on the left tree. In the “Default JPA Implementation Library”, click the “Configure user libraries” link. Click on the “New” button and give this a good name e.g. HibernateJPA. Click on “Add JARs” and add the “hibernate-entitymanager.jar” that is available in your extracted Hibernate EntityManager folder. Next, add all the jars in the lib folder of the extracted folder. These should be
Click on “Ok” to complete adding the JPA implementation.
Now, create a Java Project called “jpaproject”. In the project explorer view, right-click the project and select “JPA Tools” → “Convert To JPA Project”. Click on “Next” on the “Project Facets” screen that shows up. On the “Configure JPA Settings” window, select “Hibernate” as your platform. You will now add your connection to the database by clicking on “Add connection” link. In the “Connection Profile” window, type MySQL in the filter. Select MySQL and enter the name “jpaprojectDB” in the “Name” field. Click “Next”, and specify your database connection details. Click “Test connection” to be sure, and then “Finish” when completed.
You should now have something like below
Remember to uncheck the “Create orm.xml” option. Its useless overhead we don’t need now. You now have a project that supports JPA, with a META-INF/persistence.xml file generated in your src folder. Funny enough, after all this information provided, Dali still refuses to populate your persistence.xml file with these details. You will have to do them yourself (talk about DRY).
Double-click on your persistence.xml. On the “General” tab, specify “org.hibernate.ejb.HibernatePersistence” as your Persistence Provider. In the “Connection” tab, choose “Resource Local” from the Transaction Type drop down list. Under “Hibernate” tab, select “MySQL” as Database dialect, “com.mysql.jdbc.Driver” as Driver class, “jdbc:mysql://localhost:3306/jaccra” as your connection URL, and then provided the username and password fields for the database connection. Click on “Source” and you should something close to the ff.
<persistence version=“1.0″ xmlns=“http://java.sun.com/xml/ns/persistence”
<persistence-unit name=“jpaproject” transaction-type=“RESOURCE_LOCAL”>
<property name=“hibernate.dialect” value=“org.hibernate.dialect.MySQLDialect”/>
<property name=“hibernate.connection.driver_class” value=“com.mysql.jdbc.Driver”/>
<property name=“hibernate.connection.url” value=“jdbc:mysql://localhost:3306/jaccra”/>
<property name=“hibernate.connection.username” value=“edem”/>
<property name=“hibernate.connection.password” value=“edem”/>
Now you are all set up for your JPA machinations. One last and very important thing to do is to add the MySQL connect jar to your build path. Now when you right-click on your project in the project explorer, you should see an option “JPA Tools”, under which there are a couple of other options. Explore them and see what you get.