ERP 2.50:Developers Guide/How to work with the Data Access Layer/it
Languages: |
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 above code does not set an explicit user context. The user context is set automatically when running the above code in Openbravo ERP. However in other environments it has to be set explicitly, see here for more information.
- There is a Category class which models the data of the C_BP_Group table. This class has type safe getters and setters for all the data in this table. See here for the java code of this class.
- A factory is used to create an instance of the Category class. Remember to not instantiate these classes directly. See here for more information: Creating a new instance of a Business Object.
- The OBDal service is the main entry point into the Data Access Layer, it offers save, remove and query functionality.
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 OBDal service is used to create a OBCriteria object.
- The OBCriteria object represents the query, implements the Hibernate Criteria interface and can be used as a standard Hibernate Criteria object. The OBCriteria object also supports sorting and paging parameters.
- The OBCriteria list method performs the actual query, it returns a type safe list of the requested objects.
- After changing the name of the business partner group you don't need to do an explicit save. At commit time Hibernate will automatically detect dirty objects and save those.
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.
Languages: |