When you create an Entity Class in Netbeans 6.01, you have to choices:
- New –> Entity Class
- 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:
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.
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.