Extend a DAO to fit you needs

If you would like your DAO to have a particular method, you can add it by yourself. First, you must find the DAO implementation generated by ToPIA. It’s an empty class, for example:

package com.company.app.entities;

import javax.annotation.Generated;

@Generated(value = "org.nuiton.topia.templates.EntityDaoTransformer", date = "Fri Mar 06 16:32:09 CET 2015")
public class BookTopiaDao extends AbstractBookTopiaDao<Book> {
} //BookTopiaDao

Move it to your source directory and clean it. Don’t forget to leave it in the same package:

package com.company.app.entities;

public class BookTopiaDao extends AbstractBookTopiaDao<Book> {
}

You can now add a method to BookTopiaDao, it will be available without any further steps.

We provides dozens of protected methods to help you.

Add a method on all DAO

If you want to write a method and you want it to be available for all the entities, you can write it in AbstractMyLibraryTopiaDao. You should find this class in the generated folder and move it in your project.

Of course, since your method must work for every entities, you should use the generic E as the entity class.

public abstract class AbstractMyLibraryTopiaDao<E extends TopiaEntity> extends AbstractTopiaDao<E> {

    public Optional<E> findLastlyCreatedEntity() {
        return newQueryBuilder().setOrderByArguments(TopiaEntity.PROPERTY_TOPIA_CREATE_DATE).tryFindFirst();
    }

}

Run a native SQL query

In a DAO, you have access to a topiaSqlSupport field, you will be able to run an SQL query by calling any of the method proposed:

public class BookTopiaDao extends AbstractBookTopiaDao<Book> {

    public void aMethodOnBookDaoToRunSql() {
        topiaSqlSupport.
    }
}

Run a HQL queries

In a DAO, you have access to a topiaSqlSupport field, you will be able to run an SQL query by calling any of the method proposed:

public class BookTopiaDao extends AbstractBookTopiaDao<Book> {

    public void aMethodToRunPlainHqlAsString() {
        String hql = newFromClause(); // from Book
        hql += " where author = select a from Author a where a.name like 'Jules%'";
        Map<String, Object> hqlParameters = new HashMap<String, Object>();
        findAll(hql, hqlParameters);
    }
}

Use HqlAndParametersBuilder to ease generating queries

public class BookTopiaDao extends AbstractBookTopiaDao<Book> {

    public void aMethodToUseHqlBuilder(boolean doSomethingComplex) {
        HqlAndParametersBuilder<Book> builder = newHqlAndParametersBuilder();
        if (doSomethingComplex) {
            builder.addWhereClause("a very complex subquery...");
        }
        builder.addFetch(Book.PROPERTY_AUTHOR);
        String hql = builder.getHql();
        Map<String, Object> hqlParameters = builder.getHqlParameters();
        findAll(hql, hqlParameters);
    }
}

Add a method on PersistenceContext or ApplicationContext

Both of them can be extended by adding custom methods. We also recommend to find the corresponding generated file, move it on your project before you start coding.

public class MyLibraryTopiaPersistenceContext extends AbstractMyLibraryTopiaPersistenceContext {

    // constructor removed for brevity

    public void doSomethingOnThisPersistenceContext() {
        getAuthorDao();
        getBookDao();
        // do something
        commit();
    }
}
public class MyLibraryTopiaApplicationContext extends AbstractMyLibraryTopiaApplicationContext {

    // constructor removed for brevity

    public void doSomethingOnThisApplicationContext() {
        MyLibraryTopiaPersistenceContext myLibraryTopiaPersistenceContext = newPersistenceContext();
        myLibraryTopiaPersistenceContext.doSomethingOnThisPersistenceContext();
        myLibraryTopiaPersistenceContext.close();
    }

}