DAO

From JRapid

Jump to: navigation, search

Description

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.

Packages

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

dao

In the dao subpackage:

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

dao.hibernate

In the dao.hibernate subpackage:

Hibernate<EntityName>DAO
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.
Hibernate<EntityName>DAOAbstract
Implements hibernate filter criteria.

dao.hbm

In the dao.hbm subpackage:

<EntityName>.hbm.xml
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);
}


findBy

T findBy(T pattern);

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

findById

T findById(String id);

T findById(Serializable id);

T findById(long id);

Find instance of entity by id.

findByIds

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

Find list of instances identified by an array of ids.

findAll

Collection<T> findAll();

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

findManyBy

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.

findManyByOrderedBy

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.

findOneBy

T findOneBy(String field, Object value);

Find one element that matches the criteria.

remove

void remove(Entity object);

Remove instance from database.

store

Long store(Entity object);

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

buildFiltersFromMap

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

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

findMany

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.

Params:

  • 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.

count

int count(Filter filter);

Count number of records matching the filter criteria.

Example

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>
        <property entity="Country" label="Country" name="country"/>
    </entity>

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
@FilterMethod("name")
	public Filter getNameFilter(String name) {
		return new HibernateFilter("me.name LIKE CONCAT(?, '%')", name);
	}
	
	
	@FilterMethod("status")
	public Filter getStatusFilter(String status) {
		return new HibernateFilter("me.status = ?", status);
	}
	
	
	@FilterMethod("country")
	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");
	filter.and(dao.getCountryFilter(usa));
	
	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"/>
    </entity>


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