From JRapid

Jump to: navigation, search


DAO stands for Data Access Object. This design pattern abstracts the data source implementation while providing a persistent set of operations for working with the information. This allows the client to have access to the data even if the database or storage mechanism changes.

For every persistent entity defined in your application model, several files are created to implement the DAO components. As in every other case throughout the JRapid architecture, the Java classes are generated in two layers in order to allow code customization and the abstract class should not be touched as changes will be overwritten.


The files and classes are generated in different subpackages following this structure.


In the dao subpackage:

This file is empty when created and extends the <EntityName>DAOAbstract class. Custom code should be added here. This file will not get overwritten.
This class extends the DAO interface and implements specific methods required for filters, subsets and other elements.


In the dao.hibernate subpackage:

This file is empty when created and extends the Hibernate<EntityName>DAOAbstract class. Custom code should be added here. This file will not get overwritten.
Implements hibernate filter criteria.


In the dao.hbm subpackage:

Hibernate mapping file.

Auto-Generated Files

For example, if you create a simple Company entity, the following files and classes are generated.

In the dao subpackage:

  • CompanyDAO
  • CompanyDAOAbstract

In the dao.hibernate subpackage:

  • HibernateCompanyDAO
  • HibernateCompanyDAOAbstract

In the dao.hbm subpackage:

  • Company.hbm.xml

DAO Interface

The current DAO implementation generated by default for every JRapid project implements the following interface. Keep in mind you can always extend this and define your own methods as needed as long as you include them in the files intended for this.

public interface DAO<T extends Entity> {	
	int EXACT = 0;
	int IN = 1;
	int LIKE = 2;
	int BEGINS_WITH = 4;
	int ISNULL = 8;
	int LE = 16;
	int LT = 32;
	int GE = 64;
	int GT = 128;
	int NE = 256;
	int ISNOTNULL = 512;
	int CONTAINS = 1024;	
	int TEXT_LIKE = 2048;
	// standard finds
	T findBy(T pattern);
	T findById(String id);
	T findById(Serializable id);
	T findById(long id);
	List<T> findByIds(String[] id);
	Collection<T> findAll();
	Collection<T> findManyBy(String field, Object value);
	Collection<T> findManyByOrderedBy(String field, Object value, String property, boolean asc);
	T findOneBy(String field, Object value);
	// writeables
	void remove(Entity object);

	Long store(Entity object);

	Filter buildFiltersFromMap(Map<String, Object> params);

	List<T> findMany(Filter filter, String order, int first, int max, String hints);

	int count(Filter filter);


T findBy(T pattern);

Find persistent instance of entity by providing an entity object as parameter.


T findById(String id);

T findById(Serializable id);

T findById(long id);

Find instance of entity by id.


List<T> findByIds(String[] id);

Find list of instances identified by an array of ids.


Collection<T> findAll();

Retrieve collection of all existing records of an entity in the database.


Collection<T> findManyBy(String field, Object value);

Retrieve collection of instances filtered by value of field. Field is a string with the name of the property as defined in AML.


Collection<T> findManyByOrderedBy(String field, Object value, String property, boolean asc);

Similar to findManyBy, but adds the possibility to sort by a property and specify the sort order.


T findOneBy(String field, Object value);

Find one element that matches the criteria.


void remove(Entity object);

Remove instance from database.


Long store(Entity object);

Store instance in database. Creates a new record in the database table.


Filter buildFiltersFromMap(Map<String, Object> params);

Builds a filter element based on parameters in the params map.


List<T> findMany(Filter filter, String order, int first, int max, String hints);

Find collection of instances filtering by advanced criteria, sorting results and specifying offset and number of results.


  • filter: filter element that implements query search criteria.
  • order: property names for result sorting. Uses SQL syntax.
  • first: position of first element to retrieve
  • max: maximum number of results to return
  • hints: entity names that should be retrieved from the database by using a SQL join.


int count(Filter filter);

Count number of records matching the filter criteria.


Reusing filter generated methods

A Company entity with name, status and country properties and filters for each of these properties.

    <entity label="Company" menu="Menu" name="Company">
        <filter display="primary" label="Name" name="name" property="name"/>
        <filter display="primary" label="Status" name="status" property="status"/>
        <filter display="primary" label="Country" name="country" property="country"/>

        <property display="primary" label="Name" name="name"/>
        <property label="Status" name="status" type="enum">
            <enum value="N">New</enum>
            <enum value="C">Confirmed</enum>
            <enum value="A">Archived</enum>
        <property entity="Country" label="Country" name="country"/>

Generated methods

  • In dao.CompanyDAOAbstract
public interface CompanyDAOAbstract extends DAO<Company> {

	public Filter getNameFilter(String name);
	public Filter getStatusFilter(String status);
	public Filter getCountryFilter(Country country);	
  • In dao.hibernate.HibernateCompanyDAOAbstract
	public Filter getNameFilter(String name) {
		return new HibernateFilter("me.name LIKE CONCAT(?, '%')", name);
	public Filter getStatusFilter(String status) {
		return new HibernateFilter("me.status = ?", status);
	public Filter getCountryFilter(Country country) {
		return new HibernateFilter("me.country = ?", country);

Using the methods

This automatically generated logic could be reused in other parts of your application. For example, we could write a new method in the services class that finds a collection of new companies based in the USA:

public Collection<Company> findNewFromUSA() {
	CompanyDAO dao = Company.DAO();
	Country usa = Country.DAO().findOneBy("name", "USA");
	Filter filter = dao.getStatusFilter("N");
	return Company.DAO().findMany(filter, "name", 0, Integer.MAX_VALUE, null);

Note the usage of the filters, combining them with the and() method. Results will be retrieved ordered by name. The first element to retrieve is the one with offset zero and by using the maximum possible value of Integer we expect to retrieve all the records present in the table that match the filter criteria. Hints are set to null as no special join is required.

Finding first element

Get last Invoice for a certain Company.

    <entity label="Invoice" menu="Menu" name="Invoice">
        <property display="primary" label="Invoice Number" name="invoiceNumber" type="integer"/>
        <property display="primary" label="Date" name="date" type="date"/>
        <property display="primary" entity="Company" label="Company" name="company"/>
        <property label="Decription" name="decription" type="text"/>
        <property display="primary" label="Amount" name="amount" type="double"/>

        <filter display="primary" label="Company" name="company" property="company"/>

public Invoice getLastInvoice(Company company) {
		Filter filter = Invoice.DAO().getCompanyFilter(company);
		List<Invoice> list = Invoice.DAO().findMany(filter, "date desc", 0, 1, null);
		return list.isEmpty() ? null: list.iterator().next();

See also

Personal tools