How to use a JNDI DataSource with JPA in Netbeans 6.01

When you create an Entity Class in Netbeans 6.01, you have to choices:

  1. New –> Entity Class
  2. New –> Entity Class from Database

Choosing “Entity Class from Database” brings up a wizard to choose which database table you would like to map. If your Netbeans project is a Web Application, you get a list of Data Sources from which to choose. It shows all the JDBC Resources that are configured in your application server. Piece of cake. You’re good to go.


This is great if you’re setting up your data model inside your web application. However, if you are setting up your data model in a Java Application project (like in the tutorial), you get the list of Database Connections that are configured on your Netbeans Services tab under the Databases node.


Perhaps Netbeans has a easy/slick way of switching from Database Connection to Data Source mode on this wizard, but I haven’t found it yet. Here’s my workaround:

1.) Go ahead and select your Database Connection and complete the wizard

2.) Open persistence.xml in XML mode

3.) Remove the following properties from the properties tag:
<property name=”toplink.jdbc.user” value=”app”/>
<property name=”toplink.jdbc.password” value=”app”/>
<property name=”toplink.jdbc.url” value=”jdbc:derby://localhost:1527/sample”/>
<property name=”toplink.jdbc.driver” value=”org.apache.derby.jdbc.ClientDriver”/>

4.) Add the following tag after the provider tag


Then, we you drop the jar file for your JPA model into your web application, it will know to access the database through with a JNDI lookup at runtime.

2 cents from a RAJP:

cent 1

If you need to use this persistence unit (PU) in a web application, you ought to be using a data source (no question about it!). If you need to use the PU from a Java SE application as well, then set up two persistence units: 1 for the web that uses a Data Source and 1 for the SE app that uses a Database Connection. I haven’t tested this out, but it seems feasible.

cent 2

DO separate your JPA data model into a separate Netbeans Java Application project.  Don’t build your model in your web application.  You want a clear separation between the web tier and the persistence tier.

[EDIT:  I’ve rethought this advice about having a separate Netbeans project for the persistence tier.  It’s kind of a hassle.  I don’t do it anymore.  I just build it all into one Netbeans Web Application Project.  It’s simpler.]

2 cents is about all this is worth because I’m a JPA newb.


  1. Hi Mr Burns

    You’re an expert in netbeans
    Can i make some question abaut netbeans?

    I’m sorry i’m very new in java
    Before i usualy use Visual Basic as my application developer.
    Now i want try to make application using java web net beans and database mysql

    How can i make frame for java web? there is any control available?
    That frame is important to make a 2 side of web pages. one for menus and the other for the application form.
    How can i make it?

    I’am usualy use mysql procedure as my database querys.
    There is any code from java web netbeans for executing stored procedure in mysql?

    Thanks for your help
    your information is so much mean for me

    • Heng! Sorry I’ve been offline here for awhile. Concerning a frame…hmmm. Don’t use frames. DO use some sort of templating framework. Stripes Framework has good support for this. Tiles is another popular choice. But if you just want to do some plan old web layout stuff (side bar with links and a main content area), just use a thing called HTML and CSS. You can use the

      tag to layout or get with the modern standard and use CSS and


      As far as Netbeans and web “drag n’ drop” layout. I do not recommend this, Heng. You can do it with Visual JSF, but I think this is way too complex for what you want.

      Good luck!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s