Simple library to generate Java JPA Dao's from Dao interface type annotated with query string.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
daogen-core
daogen-processor
daogen-sample
dist/daogen-1.0
gradle/wrapper
.gitignore
README.md
build.gradle
gradlew
gradlew.bat
lgpl.txt
settings.gradle

README.md

daogen

Small library to generate Java JPA Dao's from Dao interface type annotated with query string. There are no runtime dependencies other the javax.persistence. Compile-time dependencies are freemarker and jalopy.

One of the drawbacks of using Named Queries in JPA is that you have to attach the query definitions either as annotation to an Entity class or put them in separate orm mapping files (XML). Daogen lets you attach query string as annotation to a method in your Dao interface type like this:

@DaoType
public interface CustomerDao {    
	@JpaNamedQuery(query = "select c from Customer c where name = :name")
	List<Customer> findByName(String name);

	@JpaNamedQuery(query = "select c from Customer c where name = :name and c.creationDate < :date", throwException = false)
	Customer findByUniqueNameAndBeforeDate(String name, @JpaTemporal(TemporalType.DATE)Date date);

	@JpaNamedQuery(query = "select c from Customer c where name = :name", throwException = true)
	Customer findByUniqueNameMandatory(String name);

	@JpaNamedQuery(query = "select o from Order o where o.customer.id = :id")
	List<Order> findOrdersByCustomerId(Long id);    
}

Daogen will create Dao implementations for all Dao interface types, in this case CustomerDaoImpl. Method parameters are matched against query parameter names. If you don't want named queries you can use dynamic queries by using @JpaQuery instead of @JpaNamedQuery. Furthermore it generates one queries-orm.xml file for all named queries.

@Generated(...)
public class CustomerDaoImpl extends AbstractDao implements CustomerDao {
	private final EntityManager entityManager;

	public CustomerDaoImpl(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Override
		protected EntityManager getEntitymanager() {
    		return entityManager;
		}

	@Override
		public List<Customer> findByName(String name) {
    		Query query =
        			getEntitymanager()
            		.createNamedQuery("CustomerDao.findByName");
    		setParam("name", name, null, query);
    		return query.getResultList();
		}
...
}

queries-orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
            	http://java.sun.com/xml/ns/persistence/orm
                 	http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<!-- @Generated(value="Generated by nl.koelec.daogen.gen.DaoProcessor", date = "Mon May 28 21:43:32 CEST 2012") -->

<named-query name="CustomerDao.findByName">
	<query><![CDATA[
		 select c from Customer c where name = :name
	 ]]></query>
</named-query>
<named-query name="CustomerDao.findByUniqueNameAndBeforeDate">
	<query><![CDATA[
		 select c from Customer c where name = :name and c.creationDate < :date
	 ]]></query>
</named-query>
<named-query name="CustomerDao.findByUniqueNameMandatory">
	<query><![CDATA[
		 select c from Customer c where name = :name
	 ]]></query>
</named-query>
<named-query name="CustomerDao.findOrdersByCustomerId">
	<query><![CDATA[
 		select o from Order o where o.customer.id = :id
 	]]></query>
</named-query>
</entity-mappings>

The queries-orm file is referenced from persistence.xml like so:

<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>queries-orm.xml</mapping-file>
<class>nl.koelec.daogen.sample.Customer</class>
<class>nl.koelec.daogen.sample.Order</class>

see http://koelec.blogspot.nl/2012/06/daogen-generator-for-jpa-dao.html for more info