Developing Great Software

Monday, May 18, 2009

Wicket + EJB + DI With Netbeans - Part 2

Part 2 - Creating Entity Classes That Map To An Existing Database And Exposing Data Access Services Through EJBs With Netbeans For this part of the tutorial I am going to use the Sample database that comes with the Java DB which should have been installed when you installed Netbeans and Glassfish. You can verify that the Sample database is recognized by Netbeans by expanding the Database node in the Services pane where you should see a connection listed there that looks like the highlighted one below. If you fully expand the connection node you can see that there are a number of tables already defined. For this tutorial we want to create one entity class for the Customer table. However, we will not have to hand code this entity class because Netbeans has excellent tooling support for generating entity classes that map to tables in a database. Creating Our Customer Entity Class Please follow these steps to create the Customer entity class for the Customer table in the Sample database:
  1. In the Projects pane right click the WicketEETutorial1-ejb module node and select New|Other from the context menu. This will open the New File window. In the Categories pane select Persistence and in the File Types pane select Entity Classes From Database and click Next.
  2. Expand the Data Source drop-down menu in the Data Tables Pane and select the jdbc/sample option. Netbeans will fill the Available Tables list with all the tables defined in the Sample DB.
  3. Uncheck the Include Related Tables option.
  4. Select the CUSTOMER table from the list of Available Tables.
  5. Click the Add button and then click Next.
  6. Enter entities as the package name in the Package field and the click the Create Persistence Unit button. In the Create Persistence Unit window accept all the defaults and click the Create button. Now click the Next button.
  7. In the Mapping Options pane select java.util.List for the Collection Type and click the Finish button.
Upon completing the above steps, Netbeans will create the Customer entity class and place it in the entities package. Netbeans will also generate the persistence.xml file and place it under the Configuration Files tab. With our entity class and persistence unit file created we can now go on and encapsulate our database access code in EJBs. Like our entity class we will not have to hand code our database access code. Netbeans has excellent tooling support for generating database access code that map to existing entity classes. Creating Our Customer Data Access EJB Please follow these steps to create the EJB that will encapsulate the database access code:
  1. In the Projects pane right click the WicketEETutorial1-ejb module node and select New|Other from the context menu. This will open the New File window. In the Categories pane select Persistence and in the File Types pane select Session Beans For Entity Classes and click Next. As you can see, Netbeans has filled the Available Entity Classes list with the Customer entity class we created.
  2. Select the Customer entity in the list of Available Entity Classes.
  3. Click the Add buttom and then click the Next button.
  4. In the Generated Session Beans pane enter ejbs for the package name. We want to create a local interface so make sure the Local checkbox is checked.
  5. Now click the Finish button.
Upon completing the above steps, Netbeans will create the local interface and implementation class files for the Customer entity that we selected and place these files in the ejbs package. Summary This concludes Part 2 of the tutorial. As you can see, Netbeans greatly simplifies coding the service tear in an Enterprise Application. We have taken advantage of the tooling available in Netbeans to generate our Customer entity class, persistence.xml and CustomerFacade EJB class files for us. In the next part of this tutorial, Part 3, we will expand the web tier to consume the services exposed by the CustomerFacade EJB that we have just created.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

About Me

My photo
New York, NY, United States
Software Developer