Previous Next
Programmers Guide for Java Chapter 2 - Configuring Source and Library in Eclipse

Chapter 2 - Configuring Source and Library in Eclipse

In Chapter 1, you have generated Java code, exported database schema (DDL) and persistent library. In this chapter, we will show you how to import the generated code from Chapter 1 to Eclipse. You will create a new Java project and copy the generated persistent code and library to Eclipse, and you can further your development with the generate model efficiently in Eclipse.

In this chapter:

Copying Generated Source and Library to Eclipse Project

You can copy the generated persistent code to Eclipse project and develop an application.

  1. Open Eclipse and select File > New > Project... from menu
  2. Figure 2.1 - Create a project
  3. Select Java Project, click Next >.
  4. Figure 2.2 - Select the project type as Java Project
  5. Enter "AirportProject" for the project name and select Create separate source and output folders for the Project layout option, click Next >.
  6. Figure 2.3 - Enter the project name
  7. Modify the Default output folder from "AirportProjet/bin" to "AirportProject/classes" , click Finish.
  8. Figure 2.4 - Configure the project path
  9. The AirportProject is created and shown on the Package Explorer.
  10. Figure 2.5 - A blank new project created
  11. Copy the Java persistent code from the specified output folder.
  12. Figure 2.6 - Copy all generated files
  13. Paste the Java persistent code on the AirportProject in Package Explorer.
  14. Figure 2.7 - Paste in the Eclipse project
  15. All the generated Java persistent codes are copied to the AirportProject but errors exist in the project.
  16. Figure 2.8 - The files is copied to the eclipse project

Configuring the Library

You can observe that the generated Java persistent code in the AirportProject has errors because the persistent library for the AirportProject has not been specified.

  1. Locate the orm.jar in the lib folder
  2. Figure 2.9 - The orm.jar
  3. Right click orm.jar, select Build Path > Add to Build Path from the pop-up menu.
  4. Figure 2.10 - Add the orm.jar to project classpath

As the library has been configured with the persistent library, the errors found in the Java persistent code are cleared.

Modifying the Sample Program to Test the Generated Java Model

Now the AirportProject is able to compile, you can modify the code in generated sample to test the persistent class interact with the created database.

  1. Open the src/ormsamples/CreateAirportData.java file. The following is the original code
  2. public void createTestData() throws PersistentException {
    PersistentTransaction t = airport.AirportPersistentManager.instance().getSession().beginTransaction();
    try {
    airport.Flight lairportFlight = airport.FlightFactory.createFlight();
    // Initialize the properties of the persistent object
    lairportFlight.save();

    airport.Plane lairportPlane = airport.PlaneFactory.createPlane();
    // Initialize the properties of the persistent object
    lairportPlane.save();

    t.commit();
    }
    catch (Exception e) {
    e.printStackTrace();
    t.rollback();
    }
    }
  3. Modify to create Flight and Plane instance and create the relationship between them
  4. public void createTestData() throws PersistentException {
    PersistentTransaction t = airport.AirportPersistentManager.instance().getSession().beginTransaction();
    try {
    System.out.println("Create the Flight");
    airport.Flight lairportFlight = airport.FlightFactory.createFlight();
    // Initialize the properties of the persistent object lairportFlight.setArrivingAirport("Hong Kong International Airport");
    lairportFlight.setDepartingAirport("Kansai International Airport");
    lairportFlight.setDepartureTime(new Date());

    System.out.println("Create the Plane");
    airport.Plane lairportPlane = airport.PlaneFactory.createPlane();
    // Initialize the properties of the persistent object
    lairportPlane.setPlaneType("747 plane");
    lairportPlane.setMaxSpeed(967);
    lairportPlane.setMaxDistance(8232);

    System.out.println("Create the relationship between Flight and Plane");
    lairportFlight.setPlane(lairportPlane);

    System.out.println("Save the Plane and the Flight object");
    lairportPlane.save();

    System.out.println("Commit the Transaction");
    t.commit();
    }
    catch (Exception e) {
    e.printStackTrace();
    t.rollback();
    }
    }

    There is no record for the Flight and Plane tables before the execution of the sample application.

    Figure 2.11 - no record in the table
  5. Right click the CreateAirportData.java, select Run As > Java Application from the pop-up menu to execute it.
  6. Figure 2.12 - Run the application
  7. The execution result:
  8. Create the Flight
    Create the Plane
    Create the relationship between Flight and Plane
    Save the Plane and the Flight object
    Commit the Transaction
  9. After executing the CreateAirportData.java, new records are created in tables.

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com