New Hibernate Tools features in JBDS 6

Version 3

    This article will show you some of new features and enhancements in Hibernate Tools 3.6.0 which is part of JBoss Tools 4.0.0 and also JBoss Developer Studio 6.0.0 (further referred as JBDS).

     

     

    We shall partially meet this areas of Hibernate tools in this tutorial:

    • JBoss Hibernate tools libraries for code generation with ANT tasks support
    • New database profile connection features for Hibernate
    • Some new Hibernate features in JPA projects

    Prerequisites

     

    JBoss Developer Studio


    It’s expected you have JDK 1.6 or higher and JBDS 6.0.0.GA that you can download from https://devstudio.jboss.com/earlyaccess/6.0.0.GA.html

     

    and to install is simply using following command:

    java -jar jbdevstudio-product-universal-6.0.0.GA-v20121206-1855-B186.jar

     

    Go through installer wizard and after it’s finish start JBoss Developer Studio, select new workspace location and you should see a screen below .


     

    Click on “Get started with JBoss Central” and your IDE is ready for you.

     

    Database

    Outside IDE download and start database needed for this tutorial. We will use sakila database running on h2 database. Follow these steps:

     

    mkdir ~/db

    cd db

    git://github.com/maxandersen/sakila-h2.git

    ./sakila-h2/runh2.sh

     

    You should see a database started


     

    Go back to IDE and check the database. Switch to “Database Development” perspective. And define database connection. In “Data Source Explorer” view define new connection by context menu. Select Generic JDBC Driver (because there is not specific profile for h2)


     

     

    Switch  to “JAR List” tab and add h2-<version>.jar from h2 where you have installed it. Then go to properties and add jdbc connection as written by started sakila db server. Update drive class (you can use auto lookup feature if you want).


     

    Now you can test connection by clicking “Test Connection button”


    You should see “Ping succeeded!” message box. After it’s closing, just just click Finish.

     

    Now you can browse database structure and can see database content if you want.


     

    Well, prerequisites are over so let’s take a look what hibernate tools provides.

    NEW FEATURE: Hibernate tools libraries update

    One thing that was updated in JBDS 6 is Hibernate Tools for Ant. It was updated to these versions.

     

    • 3.4.0.CR2 for Hibernate 3.5
    • 3.6.0.CR1 for Hibernate 3.6
    • 4.0.0.CR1 for Hibernate 4

     

     

    So let’s refresh our memory and try small tutorial how to use this feature in JBDS. We will create simple plug-in project and let UI to generate java db entities and also build.xml using hibernate-tools.jar ant tasks that can be modified and used further. Then we shall import ant file into our project, slightly update it and and execute it.

     

     

    Now create a testing project. In menu navigate: New -> Other … -> Plug-in project


     

     

     

    In wizard navigate: Next


     

     

    In wizard navigate: Next, Finish.

     

     

    Nice, your project is created. You can also create Java Project or any other type either  mavenized or Eclipse type project.

     

    Additional steps would be only slightly different. We created eclipse plug-in project so we can easily add dependencies via manifest file.

     

     

    Now open MANIFEST.MF in Package Explorer to add hibernate-tools into our project.


     

     

     

    Switch to “Dependencies” tab click “Add...” button and start type “org.jboss.tools.hibernate” and select “org.jboss.tools.hibernate4_0” which adds hibernate libraries to our project (and mainly desired hibernate-tools.jar for Hibernate 4.0.


     

     

    When hibernate plugin is added into your MANIFEST.MF, you can save and close the editor.

     

     

    Let’s create hibernate configuration.

     

     

    In menu navigate to: New -> Other … -> Hibernate Configuration File (cfg.xml). Click Next, select src folder and click Next


     

     

     

    NEW FEATURE: Connection profile can be used for hibernate configuration settings

     

    One of new features is “Get values from connection”. Since we’ve already created database connection profile we can use it.


     

     

     

    You can see the values were filled. We just need to add some additional properties like database dialect, etc. Check “Create a console configuration” and “Next”. Here just switch “Hibernate Version” to “4.0”. Click Finish.

     

     

    NEW FEATURE: Connection profile can be used for datasource connection settings

     

    It’s not part of this tutorial but similarly we can use this feature also while creating datasource file for application server like JBoss AS os EAP. To use it navigate to New -> Other... -> JBoss Datasource (-ds.xml).


     

     

    Ok, let’s continue with our example. Let’s try to generate some code from our database. Switch to “Hibernate” perspective if you already didn’t do that. Now navigate to Run -> Hibernate Code Generation -> Hibernate Code Generation Configurations... 

     

     

    You should see dialog similar to one below. Your configuration is already there so we can configure it a little bit and generate some code. Select your “Console configuration”, check “Reverse engineer from JDBC Connection” and add some package for exported entities.  Select also your project src as “Output directory:”.


     

     

    On “Exporters”  tab you can select Domain code or any other artefacts you want to generate (note that some of them requires additional configuration).


     

     

    When you click “Run” your code will be generated. There are many other possibilities how to tweak your exports (like reveng files, etc.) but it’s beyond this tutorial.  Anyway you have exported entities.


     

     

     

    During code generation there was some “magic” done. Let’s see what happened. This export we’ve just made is done by hibernate-tools.jar ant task which is used in generated build.xml. Let’s see if we can take a look at this ant file.

     

     

    So now try get the ant file. Click On project, invoke context menu and select “Export” -> “Hibernate” -> “Ant Code Generation”


     

     

     

    Select Hibernate configuration, select project, fill in the “File name:” to export and click Finish.


     

    We we successful, build.xml is available in our plug-in project. We can see HibernateToolTask target which is executes <hbm2java> task.


     

     

    You can now use and customize ant xml file to achieve your specific goals by adding and modifying ant tasks and of course you can use code completion. Not that you can use it for generating different types of hibernate configuration, for various export types and definition and more.  And of course this script works also outside an IDE. For more details see a documentation. Except of purpose of this tutorial you should use libraries provided by supported runtime if possible or community versions located on maven repository.

     

     

    Not let’s take a shortly look on some other features that are available.

     

     

    NEW FEATURE: Persistence.xml is update with connection profile information automatically

     

     

    As the title refers you don’t have to add connection informations into persistence.xml manually.  You can see it for yourself. Just Create “New JPA Project” (again New -> Others... -> JPA Project)


     

     

    Fill in basic properties like “Project name”.


     

     

    Select “Hibernate (JPA 2.x)” platform and set connection to a connection you’ve created before.


     

    And click Finish. When you open persistence.xml, you can see your connection is properly configured.


     

     

    Let’s see another features. We will use a project created before.


     

    NEW FEATURE: Persistence.xml is update with connection profile information automatically


     

    Before continuing add Hibernate libraries to JPA Project (you can use libraries available in JBDS in from plugins/org.jboss.tools.hibernate4_.... /lib/required). Without this feature will not work.

     

     

    Select JPA project and from context menu use “JPA Tools -> Generate Entities from tables”. Set basic parameters like what package should be used for entities generation and correct hibernate configuration. Click Finish.


     

     

    When you check persistence.xml, JBDS updated your persistence.xml accordingly with new entities. If your persistence.xml is opened during code generation you can see it live.


     

     

     

    There are many others Hibernate features available in latest stable Hibernate Tools that is available within JBoss Tools or JBoss Developer Studio. You can see all Hibernate tools features for JBDS 6.0.0 here.


     

    That’s all, enjoy latest Hibernate Tools! If you have any comment please share it below.