e hënë nëntor 14, 2005 I gave you a short primer on Java EE 5 persistence in my last blog entry. Now it's time for a demo. I'll show you how to setup the persistence framework in web application and create a POJO class that represents data from a database table. Then I will execute an EJBQL query to get object from database. In the last step I will use the new JSF component palette to generate a page that displays object in table.
I'll be using a NetBeans Java EE 5 daily build from November 13 and build 26 of GlassFish open source Java EE application server, you also need JDK 1.5.
I should repeat that support for Java EE 5 is NOT part of NetBeans 5.0, it will be in the next release of NetBeans and is now being developed on a "javaee5" branch.
Register GlassFish in NetBeans using Tools>Server Manager menu. To register GlassFish use the Sun Java Systems Application Server as a server type. NetBeans will help you to create a new domain for the server, use the "Create Personal Domain" option.
Create a new web application project, select GlassFish server and set J2EE version "Java EE 5". Click next and add Java Server Faces support.
Now let's add persistence support into this project. This will be just a few clicks in a wizard, but since I did not cover the persistence unit in the intro let me first describe it here.
Persistence unit defines where and how to persist your classes. The definition of persistence unit consists of:
In this example we will only specify the data source. We will use defaults for everything else.
We could specify the parameters of data source directly in your persistence.xml, setting properties like jdbc.connection.string, jdbc.driver, jdbc.user, etc.
But in this example I will be using a data source that is defined by the application server, the persistence unit will just reference it by its name. This will make it easier to move the application from one application server and database to another, for example between development, test and production environments.
Now back to our demo. Select menu File>New File (Ctrl+N). Select Persistence Unit template in category Persistence:
Type the name of persistence unit ("demo1"), and keep the default for provider:
Persistence units are defined in persistence.xml file. The content of generated file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="demo1" transaction-type="JTA">
<jta-data-source>jdbc/__default</jta-data-source>
<properties/>
</persistence-unit>
</persistence>
|
Let's keep the default name of data source jdbc/__default for the demo. Normally you'd go to server admin tool and setup a data source there. But GlassFish has a data source with name jdbc/__default and a derby database connection pool registered. Setup up of database is the next step.
GlassFish bundles derby open source database and this is what I will use for this demo. NetBeans has a module that supports derby. Download the "Derby Database Support" module from the development update center. Use menu Tools>Update Center.
First, start the database using menu Tools>Derby Database>Start Derby Server. Then connect to the database:
The data source jdbc/__default in application server uses the following database URL: jdbc:derby://localhost:1527/sun-appserv-samples;create=true. Use sa for both user name and password and on the next panel select schema APP. This will add a the new connection in Runtime tab and you can execute database scripts on it:
Note the new SQL editor with syntax highlighting, toolbar, etc. that we added in NetBeans 5.0:
Here is the complete script for the table and for inserting some data (insert each line separately). Refresh the list of tables and select View Data from popup menu on DIVECHARTERS table to verify everything is fine. Yes, these are some cool scuba diving trips with my favorite dive shop in Beaufort, NC :-)
Ok, we are done with the database.
Click New.. (Ctrl+N) and select Persistence Entity template in category Persistence. Type class name "Divecharters" and package name "data" and click Finish.
Now add properties for table columns:
@Entity
public class Divecharters {
private int id;
private Date departure;
private String boat;
private String diveSite;
private String description;
private int maxDepth;
private double price;
private int capacity;
...
}
|
Use menu Refactor>Encapsulate Fields to create getters and setters for these fields.
Later on NetBeans will support generation of persistence classes from DB tables, similarly as the "CMP from Database" in NetBeans 4.1 and 5.0.
We will create a JSF managed bean that will select all instances of Divecharters using EJBQL query. Then we will edit the welcomeJSF.jsp to display them.
Use New.. (Ctrl+N), select the template JSF Managed Bean in category Web. Type class name "PersonController", package "beans" and set scope to "session".
We will use EntityManager the get the persistent data. EntityManager is the central access point to the persistence API. It allows you to create and run queries, persist objects, manage transactions, etc.
The server will inject EntityManager for the PersistenceUnit we defined in Step 2. We will use it to query for all instances of Divecharters class:
public class PersonController {
@PersistenceContext(unitName="demo1")
EntityManager em;
public List getCharters() {
return em.createQuery("select object(d) from Divecharters as d").getResultList();
}
...
|
This means that our JSF managed bean PersonController will have a property charters. We can now add a JSF data table into welcomeJSF.jsp to render the list of charters. We started to work on a set of templates and palette items to simplify development and prototyping of simple CRUD applications. This is experimental at this point but it comes handy for the demo (give me feedback!). The first preview are two JSF palette items: JSF Form and JSF Data Table:
The code generated in page looks like this:
<f:view>
<h:form>
<h1><h:outputText value="List"/></h1>
<h:dataTable value="#{PersonController.charters}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Departure"/>
</f:facet>
<h:outputText value="#{item.departure}"/>
</h:column>
...
|
Run the application:
One question: When you wrote, "Later on NetBeans will support generation of persistence classes from DB tables, similarly as the "CMP from Database" in NetBeans 4.1 and 5.0." did you mean in a later build? In other words, the ability to generate the persistence classes will be in NetBeans.next, right?
We won't have to wait for NetBeans.next.next :-)
Posted by Gregg Sporar on nëntor 15, 2005 at 02:05 MD EST #
Posted by 192.18.42.10 on nëntor 15, 2005 at 02:25 MD EST #
Eventually, would be really nice to right click on a Domain Model object and be able to request the generation of all CRUD artifacts (a la RoR).
One thing, I keep getting the following exception in the JSP editor:
javax.swing.text.BadLocationException: Misuse at 34 at org.netbeans.editor.ext.html.HTMLSyntaxSupport.getNextElement(HTMLSyntaxSupport.java:525) at org.netbeans.editor.ext.html.SyntaxElement.getNext(SyntaxElement.java:93) at org.netbeans.modules.web.core.syntax.folding.JspFoldManager.generateFolds(JspFoldManager.java:220) at org.netbeans.modules.web.core.syntax.folding.JspFoldManager.updateFolds(JspFoldManager.java:321) at org.netbeans.modules.web.core.syntax.folding.JspFoldManager.access$200(JspFoldManager.java:60) at org.netbeans.modules.web.core.syntax.folding.JspFoldManager$2.run(JspFoldManager.java:124) [catch] at java.lang.Thread.run(Thread.java:595)
Thanks again for the great work!
-- Pierre
Posted by 68.126.195.181 on nëntor 16, 2005 at 08:10 PD EST #
Pierre, I am thinking about the CRUD generation. I started to do it piece by piece, the controller generation is still missing.
Thanks for actually trying this! Please ignore the exception. I know this is annoying but should be harmless. I need to merge from trunk where this is fixed.
Posted by 192.18.42.10 on nëntor 16, 2005 at 09:32 PD EST #
Posted by Will on nëntor 17, 2005 at 09:33 MD EST #
Posted by Leonard Sitongia on nëntor 21, 2005 at 06:42 MD EST #
Posted by Vimal on nëntor 23, 2005 at 11:55 MD EST #
I want to say to the developers thank you for a sleepless night.
Another point ( it's not a bug it's a feature ):
What should I change so that the application always show me the newest data particularly with regard to an UPDATE in the database ?
Finally, Pavel thank you for the great article. Unfortunately, it's very hard to find this kind of information.
Posted by 195.37.69.34 on nëntor 25, 2005 at 05:25 PD EST #
Thank you very much for all this info. It really helps a lot.
But I still have some problems when I deploy de application using EJB 3.
The error message is:
"Deploying application in domain failed; Error loading deployment descriptors for teste7 Line 4 Column 22 -- archive Teste7-EJBModule.jar in deployment descriptor file META-INF/ejb-jar.xml Error loading deployment descriptors for teste7 Line 4 Column 22 -- archive Teste7-EJBModule.jar in deployment descriptor file META-INF/ejb-jar.xml"
Do somebody have any idea?
Thank you very much,
Yara
Posted by Yara on nëntor 29, 2005 at 07:19 PD EST #
Posted by Peter Boivin on nëntor 30, 2005 at 08:15 MD EST #
Posted by Peter Boivin on dhjetor 01, 2005 at 09:57 MD EST #
Posted by Naoki Kishida on dhjetor 03, 2005 at 03:03 MD EST #
Posted by Naoki Kishida on dhjetor 05, 2005 at 03:58 PD EST #
Posted by Sivakumar Thyagarajan on dhjetor 05, 2005 at 11:00 PD EST #
Posted by gerard doets on dhjetor 17, 2005 at 03:06 MD EST #
Posted by Gerard Doets on dhjetor 21, 2005 at 04:55 MD EST #
There is bug in netbeans that puts the persistence.xml file in the wrong folder (should be in WEB-INF/classes/META-INF)
See this thread:
http://forums.java.net/jive/thread.jspa?forumID=56&threadID=2466&messageID=35108#35108
And this bug report: http://www.netbeans.org/issues/show_bug.cgi?id=71287
Posted by Warren Strange on janar 15, 2006 at 12:30 PD EST #
Posted by Gerard Doets on janar 18, 2006 at 04:36 MD EST #
Very nice and easy demo. A note about the datatable palette though.
The datatable creation wizard requires a bean name when it's actually looking for a class and requires a backing bean for the data where only a bean should be asked for. A backing bean, by JSF specification, is a managed bean that provides UIComponent instances for the JSF tag, which is not the case here, a simple managed bean works.
Posted by Simon Lessard on janar 20, 2006 at 04:33 MD EST #
Posted by William Mann on janar 23, 2006 at 03:20 PD EST #
Posted by Steve on shkurt 10, 2006 at 02:02 MD EST #
Posted by sujal on mars 01, 2006 at 09:35 PD EST #
Posted by Emotional whore on janar 26, 2007 at 06:22 MD EST #
Posted by 55 on qershor 04, 2007 at 04:35 PD EDT #
Posted by 55 on qershor 04, 2007 at 04:38 PD EDT #
whom ever wrote this statement < _I_FUCK_GOD > is such an idiot, amazing how people can get be such stupid arrogant with the little information that they have.
Posted by Ahmed Ghali on dhjetor 25, 2008 at 12:16 PD EST #
a
Posted by a on janar 09, 2009 at 11:29 MD EST #