Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Populating the Database

To populate the database, we need to execute the main program, as illustrated in Listing 9.

Listing 9 Running the database—populating the data.

C:\java\jpwh-gettingstarted-070401\helloworld-jpa>ant run
Buildfile: build.xml

compile:

copymetafiles:

run:
   [java] 1 message(s) found:
   [java] Hello World with JPA

BUILD SUCCESSFUL

If you compare the program output in Listing 9 with the Java code in Listing 3, you'll see that the following Java code instantiates Message by executing the non-default constructor:

Message message = new Message("Hello World with JPA");
em.persist(message);

The object message is then written (persisted) to the database.

What does the database look like after running the code? Figure 2 illustrates the database contents.

Figure 2

Figure 2 The populated database.

Notice in Figure 2 that the row has been populated with the instance of Message. More of that JPA magic! Notice also that the column NEXT_MESSAGE_ID is blank, because no code was executed to populate the column. However, this is easily fixed by a call to the method setNextMessage().

You now have a great framework with which to explore JPA and persistence.

  • + Share This
  • 🔖 Save To Your Account