Yes, this is another of my continuing series of JavaFX posts. I hope you are enjoying the posts, and hopefully you will take a look at JavaFX, because it's really going to be a great platform. One of the key complaints from some application development folks is “yea it does cool animations, but where's the non-trivial uses?”. True there has been a lot of “toy” applications, some touted as “Enterprise”, but I want to give you a key usage for your business application development.
First off a brief primer on JPA, or Java Persistent API. The technology grew out of the complexity and (loathing) of the EJB 2.0 spec. Hibernate came up with way to use POJOs to persist data. The brains behind JPA were the driving force behind JPA. Now is the time to learn JPA, because it's a big part of the EE 6 spec.
Some notices here first; I am using NetBeans as my IDE. You should be able to use the general theme with Eclipse though. JavaFX can use Java classes in the script. The key part for me was how to integrate them. The easiest thing to is create a new Desktop application project (New Project → Java → Desktop Application). Pick the database application, so the JPA wizard displays. Select your table, and the wizard creates the JPA controller, and all of the Entity classes for you. Run the Build project, and now you have a JAR file. If you don't have NetBeans, or want to do this by hand, create the Controller and Entity classes and put them in a JAR file. Add the JAR file to your JavaFX project path, and your ready to go! The following Code can be put in your script to access:
var db = new YourTableJPAController(); //Creates a new instance of your controller
var list = db.findYouTableEntities(); // find all of the entities of your table. To display all of the values, you can run a for loop like this:
Vbox { for(y in new YourTableJpaController().findWineEntities())
Text { content: y.getField() }
} The code above iterates through the List object created by findYourTableEntites, and puts it in a Vertical Box (Vbox). Each item in the Box is a Text object whose display value is one of your fields from your table.
You can now dynamically populate your fields from a database, but more importantly, you are using JPA to handle all of the heavy lifting. Notice I didn't mention anything JDBC. I let JPA do all of the communicating with the database. This is a layer abstraction that let's be an application development guy, not a DBA or Network Admin (not that there's anything wrong with that). If you are using an application server that handles JPA like Glassfish, then you can make your JavaFX application available on the browser and desktop. Now you can have that awesome looking application that actually does something. You are happy to start doing something cool, and your boss is happy because it does something he wants.
Comments for JavaFX + JPA = Awesome