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
- Configuring the Library
- Modifying the Sample Program to Test the Generated Java Model
Copying Generated Source and Library to Eclipse Project
You can copy the generated persistent code to Eclipse project and develop an application.
- Open Eclipse and select File > New > Project... from menu
- Select Java Project, click Next >.
- Enter "AirportProject" for the project name and select Create separate source and output folders for the Project layout option, click Next >.
- Modify the Default output folder from "AirportProjet/bin" to "AirportProject/classes" , click Finish.
- The AirportProject is created and shown on the Package Explorer.
- Copy the Java persistent code from the specified output folder.
- Paste the Java persistent code on the AirportProject in Package Explorer.
- All the generated Java persistent codes are copied to the AirportProject but errors exist in the 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.
- Locate the orm.jar in the lib folder
- Right click orm.jar, select Build Path > Add to Build Path from the pop-up menu.
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.
- Open the src/ormsamples/CreateAirportData.java file. The following is the original code
public void createTestData()
throws PersistentException {
PersistentTransaction t = airport.AirportPersistentManager.instance().getSession().beginTransaction();
try { airport.Flight lairportFlight = airport.FlightFactory.createFlight();
lairportFlight.save();
airport.Plane lairportPlane = airport.PlaneFactory.createPlane();
lairportPlane.save();
t.commit();
}
catch (Exception e) { e.printStackTrace();
t.rollback();
}
}
- Modify to create Flight and Plane instance and create the relationship between them
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();
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();
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.
- Right click the CreateAirportData.java, select Run As > Java Application from the pop-up menu to execute it.
- The execution result:
Create the Flight
Create the Plane
Create the relationship between Flight and Plane
Save the Plane and the Flight object
Commit the Transaction
- After executing the CreateAirportData.java, new records are created in tables.
|
|
| Visual Paradigm International Limited |
| Website: |
www.visual-paradigm.com |
| E-mail: |
support@visual-paradigm.com |
|