Hello EclipseLink on the NetBeans Platform
Let's use EclipseLink to set up some very basic database interaction in a NetBeans Platform application.
Though it will be the ultimate 'Hello World' scenario, it should show how to get started with database interactivity on the NetBeans Platform, while also yet again showing the benefit of the NetBeans Platform's modular architecture. Of course, feel free to adapt these instructions to your needs, for example, instead of EclipseLink, use TopLink, or Hibernate, or whatever.
You should also be surprised by how easy it is, once you know how.
We'll simply access a database and display what we find there:
Our application will look as follows:
Notice that we will have 4 separate modules, which will enable us to easily provide alternative database providers, as well as alternative persistence providers, because the UI module uses generic code that could make use of any alternative backing modules.
Let's get started.
- Create a Java Library and Generate Entity Classes from the Database. Firstly, create a Java Library project. Then use the Entity Classes from Database wizard to generate entity classes from your database. In the wizard, select EclipseLink in the step where you use the wizard to generate a persistence unit. Look at the generated code and notice that, among other things, you have a persistence.xml file in a folder called META-INF, thanks to the wizard.
In my case, I chose a Sample database that comes with the IDE, and then I specified I want an entity class for the Customer table, which resulted in the IDE also creating an entity class for the related DiscountCode table:
Build the Java Library and you will have a JAR file in the above application's "dist" folder. As you will read in the next step, that JAR file needs to be added as a library wrapper module to the application you will start creating in the next step.
- Create a NetBeans Platform Application. In the New Project dialog, specify that you want to create a new NetBeans Platform Application. Once you've created it, right-click the Modules node in the Projects window and choose Add New Library. Then select the JAR you created in the previous step. You now have your first custom module in the new application.
- Create Supporting Library Wrappers. Do the same as you did when creating the library wrapper for the entity class JAR, but this time for the EclipseLink JARs (which are in your GlassFish distro, make sure to include the persistence JAR that you find there too and, if you don't know which ones to include, go back to the Java Library shown in the previous screenshot and then expand the Libraries folder, which will show you which libraries you need). Next, create yet another library wrapper module... for the DerbyClient JAR.
- Create the UI Module. The final module you will need will provide the UI. So, create a new module (not a Library Wrapper Module, but just a plain NetBeans Module) and add a Window Component via the New Window Component wizard.
- Set Dependencies. You now have lots of classes all neatly separated into distinct modules. In order to be able to use code from one module in another module, you'll need to set dependencies, i.e., very explicit contracts (as opposed to accidental reuse of code in one place from another place, resulting in unmaintainable chaos). First, the entity classes module needs to have dependencies on the DerbyClient module, as well as on the EclipseLink module. Then, the UI module needs a dependency on the EclipseLink module as well as the entity classes module. (You could split things further so that the EclipseLink module is not a dependency of the UI module, by putting the persistence JAR in one module, with the other EclipseLink JARs separated in a different module.)
- Now, finally, let's do some coding. Not much needed, though. Add a JTextArea to the TopComponent in the UI module. Then add this to the end of the TopComponent constructor:
EntityManager entityManager = Persistence.createEntityManagerFactory("EntityLibPU").createEntityManager();
Query query = entityManager.createQuery("SELECT c FROM Customer c");
List<Customer> resultList = query.getResultList();
for (Customer c : resultList) {
jTextArea1.append(c.getName() + " (" +c.getCity() + ")" + "\n");
}Above, you can see I am referring to a persistence unit named "EntityLibPU", which is the name set in the persistence.xml file. In addition, I am referring to one of the entity classes, called Customer, which is in the entity classes module. Adapt these bits to your needs.
- Deploy the Application. Start your database server and then run the application. You should see this:
Congrats, you've just managed to set up JPA via EclipseLink in a modular NetBeans Platform application... and you only typed 6 lines of code.
| Attachment | Size |
|---|---|
| fig-1-eclipselink.png | 23.33 KB |
| fig-2-eclipselink.png | 10.53 KB |
| fig-3-eclipselink.png | 12.33 KB |





Comments
Miguel Garcia-lopez replied on Sat, 2009/01/17 - 11:58am
Hi Geertjan, firtst of all congrats for all your great contributions. I recently began developing a NB Platform based app and have found your articles and blog entries of great interest (and readability, you have a nice sense of humour :)
The steps on this entry are as expected also quite useful. I only find a drawback: Whenever I need to make changes to entity library (which is a regular NB IDE Java Library Project), I am then also required to manually copy the new generated .jar file into the NB platform library wrapper module project (step nr. 2 in the post). In fact, I need to copy it directly to the project directory, having to have a knowledge on its filesystem layout.
This makes it not really agile if the above happens often (which, BTW, is my case). An alternative could be not to develop a Java Library Project with the entity classes, but a NB Platform module directly. The problem here would then be that the NB IDE does not include the option "Generate entity classes from database" under File -> New... when the proyect is a NBP module (but it does when a regular Java Library Project!)
I'm also considering filling in a bug report on this, as I do not really happen to see any justification for this fact. Having solved that, one could keep developing a Suite (or NBP-based app.) which does include a "JPA-enabled" module all within the great NB IDE and also let it keep track of all dependencies.
As said before, congratulations again and thanks for your contributions (many which I already added/implemented in our NBP app.)
Geertjan Wielenga replied on Sat, 2009/01/17 - 12:03pm
in response to:
Miguel Garcia-lopez
I'm also considering filling in a bug report on this, as I do not really happen to see any justification for this fact. Having solved that, one could keep developing a Suite (or NBP-based app.) which does include a "JPA-enabled" module all within the great NB IDE and also let it keep track of all dependencies.
[/quote]
+1 on that! Here's my issue on this, please go in there and agree with me:
http://www.netbeans.org/issues/show_bug.cgi?id=155214
Thanks for liking my humor, nice to hear. :-)
Geertjan Wielenga replied on Sat, 2009/01/17 - 12:06pm
in response to:
Miguel Garcia-lopez
I only find a drawback: Whenever I need to make changes to entity library (which is a regular NB IDE Java Library Project), I am then also required to manually copy the new generated .jar file into the NB platform library wrapper module project (step nr. 2 in the post). In fact, I need to copy it directly to the project directory, having to have a knowledge on its filesystem layout.
This makes it not really agile if the above happens often (which, BTW, is my case).
[/quote]
Workaround: create a new Ant target and include it in your build process. During the build, the JAR will be created afresh in the NetBeans module, automatically, without you needing to do anything special (once you've created that Ant target). (See "Updating the Sources" here.)
Miguel Garcia-lopez replied on Sat, 2009/01/17 - 7:18pm
in response to:
Geertjan Wielenga
Milos Silhanek replied on Mon, 2009/02/23 - 5:10am
I is better to have domain classes in regular java jar by me. It will be used in many other occasions.
What was worse: I spent some time to look up necessary jars: Couldn't it be difficult to enumerate them?
javax.persistence.jar, org.eclipse.persistence.antrl.jar, org.eclipse.persistence.asm.jar, org.eclipse.persistence.core.jar, org.eclipse.persistence.jpa.jar, org.eclipse.persistence.oracle.jar.
I do not have such a time to try if some of them is not needed.
P.S.: Great tutorial. I looked up it for my job.
mateusz markowski replied on Mon, 2009/02/23 - 3:58pm
Hi,
Maybe it's stupid problem. I'm trying to do first part of Your tut, but with no success. I did everything in the samee way. I did a wrapper modul for javax.persistence and added for Viewer-UI. Also with topllink provider I get errors. Do you have any idea?Stack trace:
java.lang.ClassNotFoundException: javax.persistence.spi.PersistenceProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:252)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Caused: java.lang.NoClassDefFoundError: javax/persistence/spi/PersistenceProvider
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at org.netbeans.JarClassLoader.doLoadClass(JarClassLoader.java:219)
at org.netbeans.ProxyClassLoader.selfLoadClass(ProxyClassLoader.java:263)
Caused: java.lang.NoClassDefFoundError: javax/persistence/spi/PersistenceProvider while loading org.eclipse.persistence.jpaorg.eclipse.persistence.jpa.PersistenceProvider; see http://wiki.netbeans.org/DevFaqTroubleshootClassNotFound
at org.netbeans.ProxyClassLoader.selfLoadClass(ProxyClassLoader.java:267)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:226)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at javax.persistence.Persistence.findAllProviders(Persistence.java:186)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
mateusz markowski replied on Thu, 2009/02/26 - 2:43am
in response to:
mateusz markowski
Saira Ali replied on Thu, 2011/01/06 - 2:04am
Thanks for this suggestion and reporting the bug so quickly. I'll try to apply the workaround,it having such as good ant-based build system. thanks
Martin Vondráček replied on Thu, 2012/02/02 - 8:27am
The PU is not same as the DB entity, so the tutorials might confuse you when you try to do a bit different example (different tables etc.). I was searching for solution to this mistake for days...
Carla Brian replied on Sat, 2012/04/28 - 10:54pm
Mateo Gomez replied on Tue, 2012/07/17 - 2:01am
in response to:
Martin Vondráček
so thats the difference,..thanks for this i was confused at first
mexican appetizers
Matt Coleman replied on Wed, 2012/07/18 - 1:55am
in response to:
Carla Brian
awesome idea Carla..this feature is what i like most graphic designer buffalo
Lyza Carmen replied on Mon, 2012/11/19 - 11:10pm
This is very useful. Job well done.
miami
Muhammad Danish replied on Fri, 2012/11/23 - 8:46am
Muhammad Danish replied on Fri, 2012/12/14 - 9:44am