Discussion:
[jboss-user] [JBoss Tools] - New Hibernate Tools features in JBDS 6
Jiri Peterka
2013-03-05 10:33:50 UTC
Permalink
Jiri Peterka [https://community.jboss.org/people/jpeterka] modified the document:

"New Hibernate Tools features in JBDS 6"

To view the document, visit: https://community.jboss.org/docs/DOC-48444

--------------------------------------------------------------
h1. New Hibernate tool features in JBDS 6

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
h2. 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 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 .


https://lh6.googleusercontent.com/9y16idOz_-IPfChs59bIzoA8SrOy_yh55SwBWua0TLLhx68CxRE5t1kDfBETt1g1wcsMFtDqUX-WYTbThROCFk1-jXjecDLzWZ0wPN8dlUawjyeakdwOsXXpUQ https://lh6.googleusercontent.com/9y16idOz_-IPfChs59bIzoA8SrOy_yh55SwBWua0TLLhx68CxRE5t1kDfBETt1g1wcsMFtDqUX-WYTbThROCFk1-jXjecDLzWZ0wPN8dlUawjyeakdwOsXXpUQ

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

h3. 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


https://lh6.googleusercontent.com/Hw3EV7J7nHaciFPdfn7b5oudUpACIyARBAWbKG-fdSwK0mgnYMrDDfY6QUbAw4SvCsTlu9bWki2x-Z7VqZB4ke7uSiHNlNCcfngxd79oL35nFvsvYkz92zBybw https://lh6.googleusercontent.com/Hw3EV7J7nHaciFPdfn7b5oudUpACIyARBAWbKG-fdSwK0mgnYMrDDfY6QUbAw4SvCsTlu9bWki2x-Z7VqZB4ke7uSiHNlNCcfngxd79oL35nFvsvYkz92zBybw

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)


https://lh6.googleusercontent.com/y0iWJEdGmdLO1aH2mNer-Ck9v7lJhlun2F5ppLo38Kdad_Bz7QOu7c064zfUlSkDBTlc-ql7LnVuoyvSdSNikEXJeIBrnwXK-S1Ulf3HKAHZkSBEo3KDOH1YnA https://lh6.googleusercontent.com/y0iWJEdGmdLO1aH2mNer-Ck9v7lJhlun2F5ppLo38Kdad_Bz7QOu7c064zfUlSkDBTlc-ql7LnVuoyvSdSNikEXJeIBrnwXK-S1Ulf3HKAHZkSBEo3KDOH1YnA


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).


https://lh3.googleusercontent.com/p_jkbXQmCQpEu0sAHAhulV8IhTb91yxwx4syl0C6nTvAL7gpXM7PMNO6pnMbQd5gXDcjsbAHCjLOlB32VO5c1LeHY2pd0Zs5FBJy8BPQNNMZ7Es4l0dChnMrxw https://lh3.googleusercontent.com/p_jkbXQmCQpEu0sAHAhulV8IhTb91yxwx4syl0C6nTvAL7gpXM7PMNO6pnMbQd5gXDcjsbAHCjLOlB32VO5c1LeHY2pd0Zs5FBJy8BPQNNMZ7Es4l0dChnMrxw

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


https://lh5.googleusercontent.com/j9pr-eVRTqrhDCfbgSuYK0PX99iPGtbiI6QJABum5HWA_0Qt0Yy-pZGPqNicMP1f0k2Os8GGi34hnL8DCSP3ePcDqldT9uRHGlulp30kOsjQG-awSgs7VmDYVg https://lh5.googleusercontent.com/j9pr-eVRTqrhDCfbgSuYK0PX99iPGtbiI6QJABum5HWA_0Qt0Yy-pZGPqNicMP1f0k2Os8GGi34hnL8DCSP3ePcDqldT9uRHGlulp30kOsjQG-awSgs7VmDYVg
h2. 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.


https://lh4.googleusercontent.com/nt4e84GBv2OO4XuSUgOEsvHz3NWuFxX-JwxMe0nGWYmAjP5_-fNgSO4mNOPgEj_4urDTmu9cQADA1yzSeHjiPO_JC6dF25a1IH8EVI098NiVC4YiDvT_ocsPHQ https://lh4.googleusercontent.com/nt4e84GBv2OO4XuSUgOEsvHz3NWuFxX-JwxMe0nGWYmAjP5_-fNgSO4mNOPgEj_4urDTmu9cQADA1yzSeHjiPO_JC6dF25a1IH8EVI098NiVC4YiDvT_ocsPHQ

Well, prerequisites are over so let’s take a look what hibernate tools provides.
h2. 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


https://lh3.googleusercontent.com/snmZAUub5K6UFo5sKKBldEbTNCZOvqUtNeVH6po135g6-D0irnP0Cb4hEXcqzGnOolFYBOvf6IxsfGx2FcUFO581sMwMCRYG0TDr9NkIa54Lmo8XxxvbD9vHpw https://lh3.googleusercontent.com/snmZAUub5K6UFo5sKKBldEbTNCZOvqUtNeVH6po135g6-D0irnP0Cb4hEXcqzGnOolFYBOvf6IxsfGx2FcUFO581sMwMCRYG0TDr9NkIa54Lmo8XxxvbD9vHpw



In wizard navigate: Next


https://lh4.googleusercontent.com/m5EPpl2Bydw7QPb7XO6qJbGzREDt8ZSquDTu4zy97sUEX07eZmq4GClAPPovdnxvNeHwXhd-LLQLdT8yqXqHVCWuPT501BQm590DNB_0_PaeJGs0fNXIbWAZNQ https://lh4.googleusercontent.com/m5EPpl2Bydw7QPb7XO6qJbGzREDt8ZSquDTu4zy97sUEX07eZmq4GClAPPovdnxvNeHwXhd-LLQLdT8yqXqHVCWuPT501BQm590DNB_0_PaeJGs0fNXIbWAZNQ


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.


https://lh5.googleusercontent.com/DedhkVJeb-F7sScMpctDwVx_i-Re3bSnWyX2Sw-7gTd_MjBDqE-JZjyGULSiEPn8gaRvAqr4GJkPjUEK2cUIc6am0BD8TmSNs8qieiixx-AtWhGYbME0ePq6Hg https://lh5.googleusercontent.com/DedhkVJeb-F7sScMpctDwVx_i-Re3bSnWyX2Sw-7gTd_MjBDqE-JZjyGULSiEPn8gaRvAqr4GJkPjUEK2cUIc6am0BD8TmSNs8qieiixx-AtWhGYbME0ePq6Hg



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.


https://lh6.googleusercontent.com/s6HcHQIgo5NxeO2dBNPk-qyARc1jBpCB28m83Zwpq03RA0bAzNwgfQuO7FLCc-8rNOrbA7s15u6uQV9PovRwnMP8ZqbsLyde39kSUB7IdCqAdE9Ek6NTDy_5Vg https://lh6.googleusercontent.com/s6HcHQIgo5NxeO2dBNPk-qyARc1jBpCB28m83Zwpq03RA0bAzNwgfQuO7FLCc-8rNOrbA7s15u6uQV9PovRwnMP8ZqbsLyde39kSUB7IdCqAdE9Ek6NTDy_5Vg


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


https://lh4.googleusercontent.com/WxC_TtmF3jLxsPAt2MxzF5JL6vxTCdohCS_GgSW7uAEidlkBAXvbbMRZMaP-8YSzaw24osKaWGVBssnW6a6n3AqmiwpJg6o7xIUz0fyGO_GYChFYm6fHDf3clA https://lh4.googleusercontent.com/WxC_TtmF3jLxsPAt2MxzF5JL6vxTCdohCS_GgSW7uAEidlkBAXvbbMRZMaP-8YSzaw24osKaWGVBssnW6a6n3AqmiwpJg6o7xIUz0fyGO_GYChFYm6fHDf3clA



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.


https://lh6.googleusercontent.com/RNn0jiRNmWcDgDug1aJmNbMfR03MROV0Gzqe4tM9b-cUk0cOIQJ-NWcWi4U_n69JZ0KNCPtlk7DX_LbbYN4faQ0nVjbGvvXcGhxc82GLP5H3ljBFd17ozLsNRQ https://lh6.googleusercontent.com/RNn0jiRNmWcDgDug1aJmNbMfR03MROV0Gzqe4tM9b-cUk0cOIQJ-NWcWi4U_n69JZ0KNCPtlk7DX_LbbYN4faQ0nVjbGvvXcGhxc82GLP5H3ljBFd17ozLsNRQ

https://lh3.googleusercontent.com/ksHlLJumN5CF5oDxoRiwrjndxkG-vRqMwktc-do_8tV45TPnAB-yvX2U_DQXgaYZXD_IPPl5lOKyOQ4nXb7hoK6emw18yIzw2kl6i-6jBrOD9GlfylMGG9n-ew https://lh3.googleusercontent.com/ksHlLJumN5CF5oDxoRiwrjndxkG-vRqMwktc-do_8tV45TPnAB-yvX2U_DQXgaYZXD_IPPl5lOKyOQ4nXb7hoK6emw18yIzw2kl6i-6jBrOD9GlfylMGG9n-ew


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).


https://lh5.googleusercontent.com/o-3fjbCN1Ah-aArR9uS8tJ60p3Cvj-SvNgHNWboSektTT8UY3Kj2OL2DYKJQ9IpXJpGcZ96SeFSoq1tuxD3WJGdJ9h1GpK18yKBQyuVyxrpNSnl5SvnKZYYZ4w https://lh5.googleusercontent.com/o-3fjbCN1Ah-aArR9uS8tJ60p3Cvj-SvNgHNWboSektTT8UY3Kj2OL2DYKJQ9IpXJpGcZ96SeFSoq1tuxD3WJGdJ9h1GpK18yKBQyuVyxrpNSnl5SvnKZYYZ4w


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:”.


https://lh3.googleusercontent.com/GCJcd_4n74WmPl4Z4dPEGtn5MlpQpKspmM4tjwELeFF8WQcygrw9A1pVcnrbvJ2Q9oWs9L_skyoPxInrYogPTeqsk1uEm3gHhOvbDWJ3rKrLDlSgcoh3pVzmHA https://lh3.googleusercontent.com/GCJcd_4n74WmPl4Z4dPEGtn5MlpQpKspmM4tjwELeFF8WQcygrw9A1pVcnrbvJ2Q9oWs9L_skyoPxInrYogPTeqsk1uEm3gHhOvbDWJ3rKrLDlSgcoh3pVzmHA


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


https://lh4.googleusercontent.com/jMT2x8GNex6g5wrF_y2SWAdLN-hY2lBBxNLvN71e5CmHnLxz-Q5bwB8B3XDdZVriyKP9yC6MODvuuePZCMjmd4oa3qj0XreMTtETt0LB0LSzM7IGkxDONuuYcQ https://lh4.googleusercontent.com/jMT2x8GNex6g5wrF_y2SWAdLN-hY2lBBxNLvN71e5CmHnLxz-Q5bwB8B3XDdZVriyKP9yC6MODvuuePZCMjmd4oa3qj0XreMTtETt0LB0LSzM7IGkxDONuuYcQ


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.


https://lh6.googleusercontent.com/Y95LxhoU9diaA9XE-sKBU1bKbk90hEW6YvNcxYoPxChd1XVY3Rt81rq3s7dOdA6MCl8YKlm_RZ-FfRMXZm_ly7Ssfx6wzL3E8de51z1xfTsWuXH5BXjH3IJ_ZA https://lh6.googleusercontent.com/Y95LxhoU9diaA9XE-sKBU1bKbk90hEW6YvNcxYoPxChd1XVY3Rt81rq3s7dOdA6MCl8YKlm_RZ-FfRMXZm_ly7Ssfx6wzL3E8de51z1xfTsWuXH5BXjH3IJ_ZA



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”



https://lh5.googleusercontent.com/qQqPmOZuV3621uS-d7MwlRZ8LIeg3wbr0dJswlWlkpmPT2NKW4bC-KoaXOzDqS7M2Ky_bnTF_CMis3C3fBWZx_-3_UaD80HgWL6dfD2MjhAfew76iHHMeq3suA https://lh5.googleusercontent.com/qQqPmOZuV3621uS-d7MwlRZ8LIeg3wbr0dJswlWlkpmPT2NKW4bC-KoaXOzDqS7M2Ky_bnTF_CMis3C3fBWZx_-3_UaD80HgWL6dfD2MjhAfew76iHHMeq3suA


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


https://lh6.googleusercontent.com/9fL3xfB_ty3SYcjXvEce6A_fMT98k1QM1vqO6EN6Ye5uZBkPBeRyZmju3mJLPWl7hf9aJHx33obEYr-_XVz3kWdjeURKzbGHjMKT4O1Iuw4prtZ_lfBGlbZ3sw https://lh6.googleusercontent.com/9fL3xfB_ty3SYcjXvEce6A_fMT98k1QM1vqO6EN6Ye5uZBkPBeRyZmju3mJLPWl7hf9aJHx33obEYr-_XVz3kWdjeURKzbGHjMKT4O1Iuw4prtZ_lfBGlbZ3sw

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


https://lh6.googleusercontent.com/V7uIDxTPmX2zDNRmMZttwn3rU9IvglZOXh1R1czy7cwkmBIUG_sS66-EyiNRSRFaGk5mxVXfDiOT64-PzoYCCbOzJ8KFSBkzfF_DPFF_o20oktcLKxR2XZRetA https://lh6.googleusercontent.com/V7uIDxTPmX2zDNRmMZttwn3rU9IvglZOXh1R1czy7cwkmBIUG_sS66-EyiNRSRFaGk5mxVXfDiOT64-PzoYCCbOzJ8KFSBkzfF_DPFF_o20oktcLKxR2XZRetA


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 https://access.redhat.com/knowledge/docs/en-US/JBoss_Developer_Studio/6.0/html/Hibernate_Tools_Reference_Guide/ant.html documentation.


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)


https://lh5.googleusercontent.com/gKgJ8AmiGcaAv3yX-ZQ_Zyufr8DvhabPdbRNzckbXNZapuUausXIK6MTcBFzN7S62v8uig9b9LvtUgkAGtH3fMLKpECfNdMEO9ks6qA0IY9QM5AkrRNW6rKf0A https://lh5.googleusercontent.com/gKgJ8AmiGcaAv3yX-ZQ_Zyufr8DvhabPdbRNzckbXNZapuUausXIK6MTcBFzN7S62v8uig9b9LvtUgkAGtH3fMLKpECfNdMEO9ks6qA0IY9QM5AkrRNW6rKf0A


Fill in basic properties like “Project name”.


https://lh5.googleusercontent.com/xK5tluIA9T-VPOpEpyHGnkZDA82C6i1mArLOddDdbHqZ3njrSPt65qhmtuDzznOZArrkYvqyFeaSaAT5ntRiNOu8X7WMVSEgb0OPnwcRT9zW7KmDpR1e_JK0Pw https://lh5.googleusercontent.com/xK5tluIA9T-VPOpEpyHGnkZDA82C6i1mArLOddDdbHqZ3njrSPt65qhmtuDzznOZArrkYvqyFeaSaAT5ntRiNOu8X7WMVSEgb0OPnwcRT9zW7KmDpR1e_JK0Pw


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


https://lh4.googleusercontent.com/9eCBDt0Fw-2AFLS3Bbs_Beg12QA9iSpKzGWsGOqWYXPBIxeQB5xMa_acL0QxR2PzlnA6iBHz7wyDmjSXlaifjuwjGqbOsHixXGfIdVPGV4rRso9YNWxhfthR7A https://lh4.googleusercontent.com/9eCBDt0Fw-2AFLS3Bbs_Beg12QA9iSpKzGWsGOqWYXPBIxeQB5xMa_acL0QxR2PzlnA6iBHz7wyDmjSXlaifjuwjGqbOsHixXGfIdVPGV4rRso9YNWxhfthR7A

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


https://lh3.googleusercontent.com/ta--ERrdFssKUolw7aCOOVw7AbIi9Pe6MoBKK6CyGwIgEWKPsQgeyND8jSsqxdDl4ilp_p068qDSsAWvCWLx4DuhMUjQiF_kFn5w1xyiMWQ3blZprS6-LZJ3cQ https://lh3.googleusercontent.com/ta--ERrdFssKUolw7aCOOVw7AbIi9Pe6MoBKK6CyGwIgEWKPsQgeyND8jSsqxdDl4ilp_p068qDSsAWvCWLx4DuhMUjQiF_kFn5w1xyiMWQ3blZprS6-LZJ3cQ


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.


https://lh5.googleusercontent.com/WHmxN1LGfxnpO92hUrvW91jINMemiCEG-nGTFUnbOzXKf6PsjclS_heW94PDPk6S5e3DzzuNVYBZoF4zcjmPYltNqevVGah8BVmkR-VfHfSXAm_GMyxB9KXLOw https://lh5.googleusercontent.com/WHmxN1LGfxnpO92hUrvW91jINMemiCEG-nGTFUnbOzXKf6PsjclS_heW94PDPk6S5e3DzzuNVYBZoF4zcjmPYltNqevVGah8BVmkR-VfHfSXAm_GMyxB9KXLOw


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.


https://lh3.googleusercontent.com/7wBWkVsEXVEeaWabGjIe7bLCNu88X2XDTm2avehwk3NCcQZpe5Fu9rGSIXK2ZyzdYdOZtXjVXQRuyXsGJUpan33Q99bky3iUyPEbiVXuetuqaCVceY2Tuvovqw https://lh3.googleusercontent.com/7wBWkVsEXVEeaWabGjIe7bLCNu88X2XDTm2avehwk3NCcQZpe5Fu9rGSIXK2ZyzdYdOZtXjVXQRuyXsGJUpan33Q99bky3iUyPEbiVXuetuqaCVceY2Tuvovqw



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 https://issues.jboss.org/issues/?filter=12318813 here.



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

Comment by going to Community
[https://community.jboss.org/docs/DOC-48444]

Create a new document in JBoss Tools at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2128]
Loading...