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

Difference between revisions of "Texo/Dao"

(Annotation properties to control Dao Generation)
Line 36: Line 36:
 
* '''Dao Root Class''': the generated Dao classes will inherit from the org.eclipse.emf.texo.server.dao.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.
 
* '''Dao Root Class''': the generated Dao classes will inherit from the org.eclipse.emf.texo.server.dao.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.
  
== The base Dao and Generic Dao ==
+
== Getting a Dao instance ==
  
All generated classes inherit from the org.eclipse.emf.texo.server.dao.BaseDao class.
+
The Dao classes are registered in the org.eclipse.emf.texo.server.dao.DaoRegistry. The registration is done when the ModelPackage is initialized (call initialize on the generated ModelPackage when your application starts).  
  
== Adding generic functionality to all generated Dao classes ==
+
To get a Dao for a certain entity you can use the following code:
 +
<source lang="java">final BaseDao<Book> bookDao = DaoRegistry.getInstance().getDao(Book.class);
 +
</source>
 +
And then you can execute Dao methods on the retrieved Dao:
 +
<source lang="java">final Book book = bookDao.get(bookId);
 +
bookDao.remove(book);
 +
</source>
  
== Getting a Dao instance ==
+
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 ==
 
== 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 ==
  
 
== EntityManager handling ==
 
== EntityManager handling ==

Revision as of 08:09, 23 September 2011

Introduction

This page is under construction

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.

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.dao.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.dao.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().getDao(Book.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

EntityManager handling

The EntityManagerProvider

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

Cross Reference Queries

The ObjectStore allows you to retrieve all the objects which reference a certain target object:

// get maximum 3 referers and include containment referers
List<Object> referers = objectStore.getReferingObjects(target, 3, true);

See the ObjectStore getReferingObjects api and the isReferenced method.

Back to the top