View source | Discuss this page | Page history | Printable version   

ERP 2.50:Developers Guide/How to work with the Data Access Layer/it

Contents

Obiettivo

In this short howto we will introduce the main Data Access Layer interfaces, to query, create, update and delete Business Objects.

The Openbravo Data Access Layer provides Java methods to get data (Business Objects that directly correspond to database tables) from the database and to modify (insert, update and delete) that information.

All the possible issues like security, transactions, etc are handled by this layer, so the you don't need to worry about it.

Esempio 'Hello World'

As a kind of 'Hello World' example let's create a new Business Partner Group and store it in the database:

 
 // create the object through the factory
 final Category bpg = OBProvider.getInstance().get(Category.class);
 
 // set some values
 bpg.setDefault(true);
 bpg.setDescription("testdescription");
 bpg.setName("testname");
 bpg.setSearchKey("testvalue");
 
 // it is safer to set active to true explicitly
 bpg.setActive(true);
 
 // store it in the database
 OBDal.getInstance().save(bpg);

There are a number of things which are important to note:

The code snippet above also shows that you don't need to work with SQL or JDBC to work with the data from the database anymore. As a developer you work directly with objects and the available data is directly visible through the getters and setters.

As a next step, lets query for the business partner group, and change its description:

 
 // create an OBCriteria object used to retrieve data
 // think of this as an analogy to the SELECT statement
 final OBCriteria<Category> obCriteria = OBDal.getInstance().createCriteria(Category.class);
 
 // add a filter (analogous to a WHERE clause)
 obCriteria.add(Expression.eq(Category.PROPERTY_NAME, "testname"));
 
 // execute the actual query and return a typed list
 final List<Category> bpgs = obCriteria.list();
 
 // Get the first element of the list
 final Category bpg = bpgs.get(0);
 
 // and set a new name
 bpg.setName("another testname");

This code snippet introduced a number of new concepts:

The above code snippets also shows that you, as a developer, do not need to bother about explicit transaction handling. Transaction handling is done by the Openbravo application. If explicit transaction handling is required then the OBDal service class provides the required methods.

Il Risultato

This was a short introduction which shows how the DAL can be used to create, store and retrieve one (simple) business object.

You can get further details on the DAL's Developers Manual

Tips & Tricks and Troubleshooting

For tips and tricks and common issues (and solutions) which you may encounter please visit the trouble shooting section.

Retrieved from "http://wiki.openbravo.com/wiki/ERP_2.50:Developers_Guide/How_to_work_with_the_Data_Access_Layer/it"

This page has been accessed 3,436 times. This page was last modified on 14 June 2011, at 11:04. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.