Texo supports the generation of Dao classes which implement common Dao functions such as remove and cross referencing. Dao classes can be generated by right clicking on one or model files and selecting the 'Generate Model + Dao code' option.
As a default the DAO classes will be generated in a separate package inside the package containing the model code.
You can control the Dao code generation with several annotations. The generated Dao code can be changed manually, in the same way as standard EMF/Texo code.
The Dao generated code and the DaoRegistry make use of the EntityManagerProvider which is discussed in this wiki document.
The following sections describe in more detail how to control code generation and how to work with the Dao classes at runtime.
Note that the Dao's need to be able to read the model layer: the generated model package class needs to be loaded and initialized, just touching it is enough, for example (for the library example):
Required Jar files/Plugins
The Texo Dao classes are defined in the org.eclipse.emf.texo.server plugin which is part of the Texo runtime feature (see Download & Install).
So when generating Dao classes make sure to also download the server plugin/jar file to resolve the dependencies.
Annotation properties to control Dao Generation
Texo uses a so-called annotations model to control code generation. See here for detailed information on annotations models.
Dao generation can be controlled through a number of annotation properties on EPackage level.
- Dao Classes Package Path: the package name of the package in which the dao classes should be generated. As a default the model package is used with the dao pattern name appended.
- Dao Pattern Name: with the introduction of jpa a discussion has started if the word DAO is a correct term or that it should be replaced with the word Manager. This property allows you to control the suffix of the generated classes and of the package used for the dao classes.
- Dao Root Class: the generated Dao classes will inherit from the org.eclipse.emf.texo.server.store.BaseDao class, this property allows you to set the baseclass. This makes it possible to easily implement generic functionality for all generated Dao's, the root class should extend the BaseDao class.
Getting a Dao instance
The Dao classes are registered in the org.eclipse.emf.texo.server.store.DaoRegistry. The registration is done when the ModelPackage is initialized (call initialize on the generated ModelPackage when your application starts).
To get a Dao for a certain entity you can use the following code:
final BaseDao<Book> bookDao = DaoRegistry.getInstance().getDaoForEntity(Book.class);
Or you can use the Dao class directly to get an instance:
final BookDao bookDao = DaoRegistry.getInstance().getDao(BookDao.class);
And then you can execute Dao methods on the retrieved Dao:
final Book book = bookDao.get(bookId); bookDao.remove(book);
Note: the Dao needs an EntityManager to operate correctly. There are different ways to control how the Dao can get the EntityManager, see the EntityManager wiki page.
If no Dao has been registered for a certain entity class then the GenericDao class is returned. This class implements all the Dao methods using a model-driven approach.
Common Dao Methods: delete, cross referencing, findBy, get
All Dao's inherit from BaseDao which implements several methods:
- get: get by id
- findBy: find one or more instances using a property value
- isReferenced: is a certain object being referenced from other objects
- getReferingObjects: get the objects referencing a certain object
- remove: which also removes dependent objects which are referenced through single associations
- update and insert
- getAll and countAll: get all the instances, count all the instances
DAO generation versus a generic DAO util class
The main advantage of generating the DAO classes compared to using a generic DAO util class, is that with code generation you can customize much more:
- the DAO generation can be controlled using annotations
- the generated code can be changed manually, manual changes are preserved when regenerating
- you can change/override the code generation templates
Adding generic functionality to all generated Dao classes
Dao's offer great opportunities to implement functionality in a model driven way, just check out the implementation of the isReferenced method in BaseDao. Therefore Texo allows you to specify a common parent class for the generated Daos. This parent class can be/is a hand written class which inherits from BaseDao. The parent class can then be the location where your custom but generic code is located.
To set the parent Dao class, set the Dao Root Class in the annotations model, see the annotations model section above.
Cross Reference Queries
The Dao allows you to retrieve all the objects which reference a certain target object and to check if a certain object is referenced. This corresponds to the cross-referencing functionality available in EMF and CDO. The Texo approach uses queries to resolve references and allows you to limit the number of objects returned. For doing cross reference calls, check out these 2 methods in the Dao:
Some example code:
// is the book referenced, only check non-containment references bookDao.isReferenced(book, false)); // get maximum 10 referers and include containment references List<?> referers = bookDao.getReferingObjects(book, 10, true);