Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

persistent objects.

Former Member
0 Kudos

Hi,

Can anyone explain why persistent objects are used and its purpose.also some advantages and performance etc.

Regards.

Shiva lakshmi.

3 REPLIES 3

Former Member
0 Kudos

Hi Lakshmi,

Working with Persistent Objects

Use

You can use the persistent manager instance to perform the following operations on persistent objects:

· Create an object

· Retrieve a single or more persistent objects

· Update an object

· Delete an object

Activities

Creating a Persistent Object

You can create an object with particular parameters. To do this, you have to:

...

1. Get a persistent manager instance.

2. Start a persistence manager transaction.

3. Instantiate the object.

4. Invoke the makePersistent() method of the persistence manager.

5. Commit the transaction

6. Close the persistence manager.

private void createDepartment(int depId, String name) {

PersistenceManager pm = null;

try {

pm = pmf.getPersistenceManager();

Transaction tx = pm.currentTransaction();

tx.begin();

Department dep = new Department(depId);

dep.setName(name);

pm.makePersistent(dep);

tx.commit();

} finally {

if (pm != null && !pm.isClosed())

pm.close();

}

}

Retrieving a Persistent Object by ID

The ID is the primary key field for the Employee and the Department classes, therefore it has to be a unique identifier. You can use it to retrieve a single object from the data store:

Employee emp = (Employee)pm.getObjectById(new Employee.Id(0), false);

Updating a Persistent Object

To update an object, you have to:

...

1. Retrieve it from the data store.

2. Change the values of the persistent fields of the JDO instance. The JDO runtime automatically tracks changes on persistent objects and transparently sends the updates back to the data store at commit time.

private void raiseSalary(int empId, double percent) {

PersistenceManager pm = null;

try {

pm = pmf.getPersistenceManager();

Transaction tx = pm.currentTransaction();

tx.begin();

Employee emp = (Employee)pm.getObjectById(new Employee.Id(empId), false);

double oldSalary = emp.getSalary();

double newSalary = oldSalary + percent * oldSalary;

emp.setSalary(newSalary);

tx.commit();

}

finally {

if (pm != null && !pm.isClosed())

pm.close();

}

}

The method raiseSalary() raises the salary of an employee by a given percentage. First, the employee is retrieved from the database with the getObjectById() method. Then the new salary is computed from the old one. The salary of the employee is changed by calling the method setSalary() on the employee with the new salary as the parameter. When the transaction is committed (tx.commit()), the update is sent to the database.

Deleting a Persistent Object

To delete an object, you have to:

...

1. Retrieve it from the data store.

2. Invoke the removePersistent() method of the persistence manager.

private void deleteEmployee(int empId) {

PersistenceManager pm = null;

try {

pm = pmf.getPersistenceManager();

Transaction tx = pm.currentTransaction();

tx.begin();

Employee emp = (Employee)pm.getObjectById(new Employee.Id(empId), false);

Department dep = emp.getDepartment();

dep.removeEmployee(emp);

pm.deletePersistent(emp);

tx.commit();

}

finally {

if (pm != null && !pm.isClosed())

pm.close();

}

}

The method deleteEmployee()deletes an employee that is retrieved from the data store with getObjectById(). The relationship between the employees and the department is many-to-one.

Since in your JDO implementation the Java model does not reflect the removal of the object in the database, you have to remove the employee from the employee set of its department manually to keep the Java model consistent with the persistent data.

Retrieving All Persistent Objects of a Persistence Capable Class

The extent of a persistent capable class is the collection of all persistent instances of this class in the data store. JDO supports the notion of extent using the javax.jdo.Extent interface. You can consider an instance of javax.jdo.Extent as a handle to a collection of persistent objects in the data store. The persistence manager acts as a factory for extents. In order to loop over all persistent objects of an extent, you have to obtain an iterator from the extent and use the iterator over all objects.

PersistenceManager pm = null;

try {

pm = pmf.getPersistenceManager();

Transaction tx = pm.currentTransaction();

tx.begin();

Extent ext = pm.getExtent(Department.class, false);

Iterator it = ext.iterator();

while (it.hasNext()) {

Department dep = (Department)it.next();

// Do something with the department

}

ext.close(it);

tx.commit();

}

finally {

if (pm != null && !pm.isClosed())

pm.close();

}

}

In this example, the extent is obtained using the getExtent() method of the persistence manager. The Boolean argument of getExtent determines whether persistent instances of subclasses have to included into the extent or not. You get an iterator by invoking the method iterator() on the extent. If you do not do any transactional work on any element of the iterator u2013 that is, if you do not change persistent objects u2013 you do not need to declare a transaction. Finally, you close the extent and the iterator by calling the close()method on the extent. This enables the JDO implementation to release all the resources it has used during the iteration.

Concepts Used in Persistence

Transient and persistent data

In principle, ABAP programs work with local program data, which resides in the programu2019s internal session. This data lives only as long as its context u2013 that is, as long as its associated procedure (for local procedure data); its object (for attributes of classes); or its program (for global program data). This data is known as transient. Data that can be preserved beyond the runtime of the program is known as persistent. In SAP Systems, persistent data usually occurs as the content of database tables, but also as the content of files on application and presentation servers.

To work with persistent data, the system has to load it into transient data objects belonging to the ABAP program while that program is being executed. Then, after processing has been completed, it stores the data in a persistent form again. During this time, the content of the data exists twice: once in the ABAP program (transiently), and once in the appropriate storage medium (persistently). A typical process would be reading data from a database table using the SELECT statement into a transient work area; modifying the work area; and then updating the database table (using UPDATE). In such cases, the contents of transient and persistent data are different in the interim during this process.

Data in object-oriented programming

In an ideal object-oriented application, data occurs only as the attributes of objects (if we ignore the local data in methods for the time being). Objects are an aggregation of functions (in methods) and data (in attributes). The description of an object u2013 that is, the class u2013 occurs persistently as a piece of source code, but its attributes exist only as long as the object. However, an object in ABAP Objects is transient in principle. It exists in the internal program session only from the time it is generated (using CREATE OBJECT) until it is deleted by the Garbage Collector. Therefore, to work with persistent data in objects, you must program access to where those objects are stored within the methods of the class.

However, in completely object-oriented business application programming, then it is pointless simply to transfer the classical separation of data and functions to the methods u2013 that is, to work with objects, but use procedural programming within the objects themselves. Ideally you could save the encapsulation of data and functions persistently within the object instead. A program could then leave an object in a certain state and a second program could continue working on the object in that state. Classes of objects are already persistent anyway, but you need some way of saving the attributes of an object persistently and then make reference to the appropriate class. The Persistence Service allows you to do exactly that.

The Persistence Service for Persistent Objects

Technically speaking, ABAP Objects are always transient, just like the data objects in ABAP programs. There are no persistent objects in ABAP Objects. However, the Persistence Service within Object Services allows application developers to work with persistent objects. The Persistence Service can be thought of as a software layer between the ABAP program and the data repository (that is, the database), which allows you to save the attributes of objects with a unique identity, and then load them again when you need them.

Put simply, the Persistence Service ensures that an object is initialized in a specified state, and saves the state of that object when required. The relationship between the object and the description of its state in the database is similar to the relationship between transient and persistent data outlined above. The state of the object when it is instantiated reflects the state of the data in the database at that time. Changes to the object state in the ABAP program are not written to the database immediately, but only after the appropriate request has been made (that is, the COMMIT WORK statement has been executed). Thus, a persistent object exists as an original in the database and as a copy in one or more ABAP programs. If several programs use the Persistence Service to instantiate objects of the same class before one of these programs has changed the state using COMMIT WORK, all the objects will have the same initial state. At present, we have not implemented a Persistence Service lock concept, which would ensure that there was only one transient mapping for each persistent object. So ultimately, ABAP programmers are not really working with persistent objects as such; rather, the Persistence Service makes it appear as if they are.

Persistent Classes

To use the Persistence Service for objects, the classes of these objects must be created as persistent classes in the Class Builder. The term persistent class does not imply that a class is persistent. (As a template for objects, every class is persistent). Rather, it means that the objects of that class and their state are managed by the Persistence Service. For example, the objects of these classes are instantiated in the ABAP program with a method of the Persistence Service, which ensures that the initialization is correct (not with the usual CREATE OBJECT statement). When the Class Builder creates a persistent class, it automatically generates an associated class, known as the class actor or class agent, whose methods manage the objects of persistent classes. As well as their identity, persistent classes can contain key attributes, which allow the Persistence Service to ensure that the content of each persistent object is unique.

Managed Objects

The objects of persistent classes are managed by the Persistence Service. This means, among other things, that these objects are instantiated with a method of the class actor, not with the CREATE OBJECT statement. These objects are known as managed objects. Objects managed by the Persistence Service can be either persistent or transient.

Persistent objects must be managed by the Persistence Service. The Persistence Service connects the object and the database.

Transient objects of persistent classes are also managed by the Persistence Service. For example, the Persistence Service ensures that the object is unique within a program (by checking its key attributes), but not for a connection to the database.

for oops persistent objects see this link

http://www.mininova.org/tor/1360525

thankns

abdul

reward me if usefull

Former Member
0 Kudos

Persistent Objects: These are the objects for which the scope remains asusual when you exit from a block or thread or say class. These objects resides permanently in the memory so that any further program can use these objects.

Please have a look on this link:

http://help.sap.com/saphelp_nw04/helpdata/en/f5/a3682ebc6911d4b2e80050dadfb92b/content.htm

reward if useful.

BR,

Alok

Former Member