Saturday, March 01, 2008

JPA and Web Examples using Project Woodstock and Netbeans

I was looking at some of the examples from the Netbeans site and trying to find a basic example to demonstrate. After looking at the examples I found on the web, I thought I would create a quick example myself.

First I created a project in Netbeans. I picked Java Library. Then I selected create entity classes from database and used the sample JavaDB database that comes with Netbeans. It prompted me to create the persistence.xml and I accepted the defaults.

Next I created a Web project in Netbeans. During the project creation wizard, I selected Visual JSF to include the JSF and Woodstock libraries.

Next I clicked on the properties for the project and added my library to the project.

Next I created a created a new persistence.xml file. After it was created, I modified it so that it would include the specific entity classes that I created in the library project.

Note: This is required otherwise the entity classes will not be detected.


Next I added this code snippet to SessionBean1

This fetches my data so that I can display it later in a table.

Next I compile the project (no deployment yet). This refreshes the beans so that I can manipulate them visually.

Next I create a table on Page1 from the palette. I right click the table and select binding and point it from the DefaultTableProvider to customers in the SessionBean.

Now I deploy it. Poof the magic is complete.

The link to my example Netbeans 6.5.1 project on Bitbucket: SamplePersistenceWeb

11 comments :

Zambizzi said...

That's pretty cool, thanks for posting. However, I'm struggling to figure out how to use Woodstock/Visual JSF in Netbeans 6.0 with EJBs that are in an EJB project, in an enterprise project setting. Can this even be done? There seems to be a huge lack of integration there!
Thanks!

John Yeary said...

Certainly!

I use EJBs all the time in my projects. I will see if I can write a simple example and post it.

Zambizzi said...

Any luck? ;)

I hear a near-future release of Netbeans will allow visual drag/drop w/ JPA and Woodstock?

Roberto Aguirre said...

I have the follow error
xception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/incidencia
Error Code: 0
at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:305)

John Yeary said...

The exception is an indication that you do not have the mySQL driver installed. You need to add the driver to your project, or ensure that the mySQL driver is in the lib directory on the server.

Javier Sinnada said...

hi you said "u need to add the Mysql driver" but where? in a java class library or a web project?

thanks

kquizak said...

why do you have 2 projects? art sake? it works just fine in a single Web project... at the first glimpse.
Anyway great tutorial... I tried the much detailed tutorial from netbeans and i failed at the binding data stage... simply the property in SessionBean1 refused to apeare.

John Yeary said...

The mySQL driver needs to be a part of the project.

John Yeary said...

I split the project up so that I would have a separation of concerns. I could then reuse the JPA portions in other tutorials.

You are right. You could combine them into one project if you wanted. You do have the source code. ;-)

kwedeer said...

Hi,

I was eager to develop JSF/EJB together as well - I wonder - this page is almost the only resource about this and Yeary's saying that 'I use EJBs all the time in my projects. I will see if I can write a simple example and post it.
' is the only success story about NB as far as I have read, I guess Yeary talked about NB, not about e.g. Oracle JDeveloper, where this integration is perfect.

So - for me the main problem is - I am using NB 6.1 and there is said in the help, that EJB drag and drop for tables is available from Services page 'Enterprise Java Beans' item (in NB help there is even the picture which shows that this is really a truth) - but for my installation and I have tried 2 NB6.1. isntallations on 2 separate compouters (XP and Business Vista) and I applied all the latest updates (as of today) - but still - there is no item 'Enterprise Java Beans' in the services page, but there is item 'Enterprise Beans 2.x' instead and naturally, it doesn't allow me to import EJB 3.0 beans from application client in the IDE. So - I will go this exmple - maybe I can make integration by hand. This application would be greate help, because there is no description ho to use EJB from Visual Web Faces in the JSP code level either.

I would stronlgy suggest to make dome tutorial about this - everybody is trying to do some nice and up-to-date architecture for his job assignments, or for study projects, but there is so few materials for this. I guess - programming is about creativity and not about hacking IDE to achieve the same result which can be achieved in some minutes if good documentation is at place.

John Yeary said...

I am saddened that Project Woodstock has been abandoned by Sun. There will likely be no drag-n-drop functionality for Woodstock and JPA.

Popular Posts