Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Texo/Dao

< Texo
Revision as of 23:23, 23 September 2011 by Mtaal.springsite.com (Talk | contribs) (EntityManager handling)

Introduction

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.


Org.eclipse.emf.texo.dao.menu.option.png


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 following sections describe in more detail how to control code generation and how to work with the Dao classes at runtime.

Note that for 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):

LibraryModelPackage.initialize();

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.


Org.eclipse.emf.texo.dao.annotations.png


  • 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 section further below.

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

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.

EntityManager handling

The Dao class can get to an EntityManager in different ways:

  • the EntityManager member in the DAO has the @PersistenceContext annotation, so depending on the environment this can work automatically.
  • an EntityManager can be explicitly set in the Dao, or
  • if no EntityManager is set then the Dao class will use the EntityManagerProvider.getEntityManager method (described in the next section).

The EntityManagerProvider

The Dao classes make use of the EntityManagerProvider which is the central provider of EntityManager instances for the Dao classes and ObjectStore.

There is a single instance of the EntityManagerProvider available, it can be retrieved using getInstance. You can set your own instance by calling setInstance (both methods are static).

The EntityManagerProvider can be initialized in different ways:

  • setting an EntityManagerFactory in the instance, or
  • by setting the persistence properties needed to create/initialize an EntityManagerProvider

The EntityManagerProvider supports a special way of creating and managing an EntityManager: getCurrentEntityManager. This method will return an EntityManager which is stored in a ThreadLocal. This corresponds to the getCurrentSession method on a Hibernate SessionFactory.

You can let the EntityManagerProvider work in a 'getCurrentEntityManager' mode by calling setUseCurrentEntityManagerPattern with the parameter true. Then getEntityManager will return the EntityManager stored in the ThreadLocal. This means that subsequent calls in one thread will make use of the same EntityManager. This often make sense.

It is very important to clean/clear up the EntityManager when using the getCurrentEntityManager approach. This because Tomcat (and maybe also other servlet containers) will re-use Threads for different requests. To clean up an EntityManager call EntityManagerProvider.getInstance().clearCurrentEntityManager();

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:

  • isReferenced
  • getReferingObjects

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);

See the ObjectStore getReferingObjects api and the isReferenced method.

Back to the top