站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Using Hibernate with NetBeans4.1 and SJSAS 8.1

Using Hibernate with NetBeans4.1 and SJSAS 8.1

摘要: Using Hibernate with NetBeans4.1 and SJSAS 8.1

What is Hibernate?

Hibernate is a popular open source object/relational mapping tool. It provides easy means for mapping from Java classes to database tables. Using Hibernate can considerably reduce development and maintenance of JDBC related Java code. Test driven development with Hibernate is quite simple irrespective of the database you use.

Sun Java System Application Server 8.1 Platform Edition(SJSAS 8.1) provides enterprise features and free for production use. The administration console makes configuration of JDBC resources very easy.

This tutorial demonstrates how to use Test Driven Development using Hibernate and NetBeans 4.1 and deploy it in the Sun Java System Application Server 8.1.


Setting up your environment

This tutorial assumes you have already downloaded and deployed NetBeans4.1 with SJSAS4.1. This location will be referred to as <netbeans-home>.

You may download Hibernate 2.1.8 from Hibernate All Downloads and unzip it in a suitable location(we will refer to this location as <hibernate_home>).


Developing the application


Creating a web project from the provided source files

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Web. Under Projects, select Web Application. Click Next.
  2. Type in the details as below and click Finish. Replace <project-location> with suitable location.


Create database table.

Our tutorial application will access data from BOOK table in the database. It will perform create, read, update and delete operations. We will use the PointBase database shipped with SJSAS.

  1. Start the pointbase database, by selecting Tools > Pointbase Database > Start Local Pointbase Database.
  2. Create a custom ant script to create and populate the database table:
    Click on the File tab. Create an Ant script in HibernateTutorial folder: New > File/Folder > Ant Build Scripts > Empty Ant Script. Name the script custom. Copy the contents of custom.xml to complete this script.
  3. Create a properties file(custom.properites) in HibernateTutorial folder. (New > Other > Properties File). Copy the contents of custom.properties in this file. Replace <netbeans-home> with the directory where NetBeans is installed.
  4. Right mouse click on custom.xml and select Run Target > All. This will create the table BOOKS and populate with data.

Create the persistent class and O/R mapping.

  1. Add the following jars from <hibernate_home>/lib to the project library - jta.jar, ehcache-0.9.jar, dom4j-1.4.jar, cglib-full-2.0.2.jar, commons-collections-2.1.1.jar, commons-lang.1.0.1.jar, commons-logging-1.0.4.jar, commons-pool-1.2.jar, and odmg-3.0.jar. Also add hibernate2.jar from <hibernate_home>
  2. Hibernate uses Plain Old Java Objects(POJO) for persistent classes. A POJO is very similar to a JavaBean, with properties of the class accessible via getter and setter methods. Create the persistent Book class in book.business package. Complete the rest of the source from Book.java.
  3. The Book.hbm.xml mapping file contains the metadata required for the object/relational mapping. The metadata includes declaration of Book class and the mapping of properties (to columns) to the database table BOOKS. Create the xml file Book.hbm.xml in book.business package and copy the contents from Book.hbm.xml.

Create facade and JUnit tests

NetBeans supports test driven development. Creating JUnit test case class is straightforward. Just right click the source file and Tools > JUnit Tests > Create Tests. Bingo! A JUnit test class with the name <classname>Test is created in the Test Packages. All the public methods have a corresponding test method. We will create the facade interface and before completing the Hibernate implementation we will create JUnit tests.

  1. Create the DatabaseException class - Right mouse click on Source Packages, and select New > Java class. Key in DatabaseException for Class Name and book.exceptions for Package.
  2. Copy the soure code from DatabaseException.java and paste it on the newly created class.
  3. Create the BookFacade interface in the book.business package and copy the source from BookFacade.java.
  4. Create the Hibernate facade implementation HibernateFacade in the book.hibernate package. Refer to HibernateFacade.java. Note this only has the skeleton methods.
  5. Highlight the HibernateFacade.java, right mouse click, Tools > JUnit Tests > Create Tests to create a JUnit test case in the Test Packages. Open the TestHibernateFacade class and check its contents.
  6. We will complete the generated methods in the TestHibernateFacade class as in HibernateFacadeTest.java
  7. Now we will complete the HibernateFacade class and write all the implementation code.here.
  8. Finally we will add a setup() method in the TestHibernateFacade class as in HibernateFacade.java
  9. Add the <netbeans-home>/SunAppServer8.1/pointbase/lib/pbclient.jar and <hibernate_home>/lib/jta.jar to the Test Libraries.
  10. Now you are ready to run the JUnit tests - Highlight the HibernateTutorial project. Select Run > Test "HibernateTutorial". You should see a large output and the following lines indicating that the JUnit tests ran successfully.
    Testsuite: book.hibernate.HibernateFacadeTest
    Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 11.419 sec

The servlet and jsps

Now we will create the servlet, integrating the facade class we created.

  1. Right click the Source Packages choose New > File/Folder. Under Categories, select Web. Under File Types, select Servlet. Click Next.
  2. Enter FrontController in the Class Name text box and book.servlet in the Package combo box. Click Next.
  3. Change the value of the URL Mappings text box to *.htm and click Finish. FrontController.java opens in the Source Editor.
  4. Now we will edit the FrontController.java file to use the various business methods we created in the HibernateFacade class. Complete the various methods from FrontController.java.
  5. Expand the Web Pages node and the WEB-INF node. Double-click the web.xml node. The content of the deployment descriptor file, web.xml, is displayed in the Source Editor. Complete the rest of the entries from web.xml.
  6. Create index.jsp, bookDetails.jsp, listBooks.jsp and errorpage.html in the Web Pages node. Copy the code from index.jsp, bookDetails.jsp, listBooks.jsp and errorpage.html.

Server settings

SJSAS provides a security feature that prevents classes from unauthorised jar files being executed. So you need to grant permission using the policytool provided with J2SE. Start the policytool console. server.policy file for SJSAS is located in <netbeans-home>/SunAppServer8.1/domains/domain1/config. Open this file and add following policy at the end:

      grant codeBase "file:${com.sun.aas.installRoot}/domains/domain1/applications/j2ee-modules/-" {
permission java.security.AllPermission;
};

Note: You may grant fine grained permissions at the jar level. Also instead of using policytool you may use an editor and add the lines at the end of the policy file.

We will create the Hibernate configuration file hibernate.cfg.xml file in the Source Packages. The properties to added are as in hibernate.cfg.xml. This file specifies the datasource jndi name(jdbc/BookDB) cofigured using the SJSAS Admin console.

Finally, configure the JDBC Resourse jdbc/BookDB. Select PointBasePool for Pool Name.

Stop and start the SJSAS server so that the changes we made take effect.

Run HibernateTutorial application.


  • Once all the above steps are completed, the Project should look like this:

    FinalFileStructure
  • Right-click on HibernateTutorial and select Deploy Project.
  • Once deployed successfully, access the following URL in your browser. http://localhost:8080/hibernateTutorial. This should display screen as below:


  • Click on "List all books" to display all the books:


  • Click on "Create Book" and key the values as shown and press save:


  • Now you will se the list of books with the book you created:


  • You can try Delete and Edit functions, by selecting the radio button in one of the rows.
  • The whole NetBeans project can be downloaded from HibernateTutorial.zip

↑返回目录
前一篇: Using CVS in NetBeans IDE 5.0
后一篇: Using Hibernate with the Java Persistence API