Java O/R Mapping
O/R Mapping
Tips and Tricks

Using Java Persistence Class with Factory Class Persistent API

Using factory class persistent API, not only the persistence class, but also the corresponding factory class will be generated for each ORM-Persistable class. The generated factory class is capable of creating a new instance of the persistence class, which assists in inserting a new record to the database, and retrieving record(s) from the database by specifying the condition for query. After an instance is created by the factory class, the persistence class allows setting the values of its property and updating into the database. The persistence class also supports the deletion of records.

The following class diagram shows the relationship between the client program, persistent object and factory object.

factory_method-1.jpgNote: The PersistentObject refers to the ORM-Persistable class defined in the class diagram while the PersistentObjectFactory refers to the generated factory class of the ORM-Persistable class

Example :

factory_method-2.jpg

From the above example, the CustomerFactory class supports the creation of Customer persistent object, the retrieval of Customer records from the Customer table while the Customer persistence class supports the update and deletion of customer record.

Creating a Persistent Object

An instance of a persistence class represents a record of the corresponding table, creating a persistent object corresponds to inserting a new record to the table.
In order to insert a new row to the database table, implement the program with the following steps:

  1. Create a new instance of the class by the factory class.
  2. Set the properties of the object by the persistence class.
  3. Insert the object as a row to the database by the persistence class.

Example :

factory_method-3.jpg

From the above example, an ORM-Persistable object model of Customer maps to an ORM-Persistable Java class with methods for setting the properties. An ORM-Persistable factory class is generated with method for creating a new instance and; and thus these methods allow inserting the instance as a row of the database table.

To insert a new Customer record to the table, Customer of the database, the following lines of code should be implemented.

Customer customer = CustomerFactory.createCustomer();
customer.setCustomerID(3);
customer.setCustomerName("Peter Chan");
customer.setAddress("6C, Pert Court");
customer.setEmail("peter.chan@gmail.com");
customer.save();

After executing these lines of code, a row of record is inserted to the database table.
Note: From the above example, Customer customer = CustomerFactory.createCustomer() can be replaced by Customer customer = new Customer() to create a Customer object.

Loading a Persistent Object

As an instance of a persistence class represents a record of the corresponding table, a record retrieved from the table will be stored as an object.

In order to retrieve a record from the database table, you have to specify the condition for finding the record. To retrieve a record, implement the program with the following steps:

  1. Specify the condition for searching the record by the factory class.
  2. Load the retrieved record to an object.

Example :

factory_method-4.jpg

From the above example, an ORM-Persistable object model, Customer maps to an ORM-Persistable Java class generated with methods for retrieving a matched record.

There are two ways to retrieve a record from the Customer table:

  • By Primary key:
Customer customer = CustomerFactory.loadCustomerByORMID(2);
  • By Query:
Customer customer = CustomerFactory.loadCustomerByQuery(
                    "Customer.CustomerName='Peter'", "Customer.CustomerName");

After executing the code, a matched row is retrieved and loaded to a Customer object.

Updating a Persistent Object

As a record can be retrieved from the table and loaded as an instance of the persistence class, setting a new value to the attribute by its setter method supports the update of record. In order to update a record, you have to first retrieve the row to be updated, and then set a new value to the property, finally update to the database. To update the record, implement the program with the following steps:

  1. Retrieve a record from database table and load as an object by the factory class.
  2. Set the updated value to the property of the object by the persistence class.
  3. Save the updated record to the database by the persistence class.

Example :

factory_method-5.jpg

From the above example, an ORM-Persistable object model, Customer maps to an ORM-Persistable Java class generated with the methods for setting the properties and updating the row.

To update a Customer record, the following lines of code should be implemented.

customer.setCustomerName("Peter Pang");
customer.save();

After executing the above lines of code, the customer name is updated to “Peter Pang” in the database.

Deleting a Persistent Object

As a record can be retrieved from the table and loaded to an object of the persistence class, the record can be deleted by simply using the delete method of the persistence class.

In order to delete a record, implement the program with the following steps:

  1. Retrieve a record from database table and load as an object by the factory class.
  2. Delete the retrieved record by the persistence class.

Example :

factory_method-6.jpg

From the above example, an ORM-Persistable object model, Customer maps to an ORM-Persistable Java class generated with the methods for deleting the specified record from the database.

To delete a Customer record, the following lines of code should be implemented.

Customer customer = CustomerFactory.loadCustomerByORMID(2);
customer.delete();

After executing the above code, the specified customer record is deleted from the database.

Loading a Collection of Persistent Objects

In order to retrieve a number of records from the table, you have to specify the condition for querying the records. To retrieve a number of records, implement the program with the following steps:

  1. Specify the condition for searching the record by the factory class.
  2. Load the retrieved records as an object array by the factory class.

Example :

factory_method-7.jpg

From the above example, an ORM-Persistable object model, Customer maps to an ORM-Persistable Java factory class generated with methods for retrieving records.

To retrieve records from the Customer table, the following line of code should be implemented.

Customer[] customer = CustomerFactory.listCustomerByQuery(
                      "Customer.CustomerName='Peter'", "Customer.CustomerName");

After executing the code, the matched rows are retrieved and loaded to an object array of Customer.

Querying by ORM Qualifier

ORM Qualifier is the extra data retrieval rule defined to query the database. By defining the ORM Qualifier in a class model, the persistence class will be generated with the additional data retrieval methods, load and list methods by the ORM Qualifier.

Example :

factory_method-8.jpg

In the above example, a customer object model is defined with an ORM Qualifier named as Name and qualified with the attribute, CustomerName.
To query the Customer table with the ORM Qualifier in one of the two way

  • By Load method
Customer customer = CustomerFactory.loadByName("Peter");

After executing the code, the first occurrence of “Peter” in the CustomerName column in the Customer table will be loaded to the object identified as customer.

  • By List method.
Customer[] customer = CustomerFactory.listByName("Peter");

After executing the code, all rows which contain “Peter” in the CustomerName column in the Customer table will be retrieved and stored in an array of Customer object.

Querying by Criteria Class

Criteria class is a helper class which supports searching the records from the database. Criteria class can be generated with the persistence code.

You can get the retrieved records from the criteria class in one of the two ways:

  • Use the retrieval methods of the criteria class.

For more information on the generation and usage of the criteria class, please read Using Criteria Class.

  • Use the retrieval by criteria methods of the factory class.

Example :

factory_method-9.jpg

To retrieve records from the Criteria Class by the factory class in one of the two ways:

  • By Load method
 CustomerCriteria customerCriteria = new CustomerCriteria();
 customerCriteria.CustomerName.like("%Peter%");
 Customer customer = CustomerFactory.loadCustomerByCriteria(customerCriteria);

After executing the code, the first occurrence of “Peter” in the CustomerName column in the Customer table will be loaded to the object identified as customer.

  • By List method.
CustomerCriteria customerCriteria = new CustomerCriteria();
CustomerCriteria.CustomerName.like("%Peter%");
Customer[] customer = CustomerFactory.listCustomerByCriteria(customerCriteria);

After executing the code, all rows which contain “Peter” in the CustomerName column in the Customer table will be retrieved and stored in an array of Customer object.

Related Articles

Resources

 
 
Last modified: 2006/03/31 10:39
 
 
Home | Recent Topics | Highlights | UML Diagrams | Tips and Tricks | Object-Relational Mapping
visual-paradigm.com Home | Training Center | UML Center | VP Gallery | Discussion Forum | UML Open Directory