My favorites | Sign in
Project Home Downloads Issues Source
Project Information
Members

TUTORIAL

Description

Runtimedao or runtime DAO is a library for generating your DAO classes at runtime. It has already have Log4j, Hibernate, JPA, PostgreSQL, MySQL and other dependencies included.

For example, let TestVO be a VO basic entity class and TestDAO a DAO interface related to TestVO:

TestVO.java

package org.runtimedao.sampleproject;

//imports

@Entity
public class TestVO {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;
    private String login;
    private String password;  
  
    //setters and getters  
}

TestDAO.java

package org.runtimedao.sampleproject;

//imports

@DAOEntity(TestVO.class)
public interface TestDAO extends DAO<TestVO> {

    @Query("FROM TestVO t WHERE t.login = ?")
    TestVO findByLogin(String login);

    @Query("FROM TestVO t WHERE t.login = ? AND t.password = ?")
    TestVO findByLoginAndPassword(String login, String password);
}

Then, if you call:

TestDAO testDAO = DAOFactory.getDAO(TestDAO.class)

The TestDAO implementation will be generated and compiled at runtime!

You just need to write the interface of your DAO classes and runtimedao will implement your new methods and the ones of the following interface:

public interface DAO<T> {    

    T find(Object id);
    T find(Object id, Map<String, Object> properties);
    void persist(T t);
    void remove(T t);
    void merge(T t);
    void flush();
    List<T> findAll();
}

How to use runtimedao in a maven based project?

1. Add in your pom.xml:

<project>
...
    <repositories>
        <repository>
            <id>runtimedao</id>
            <url>https://runtimedao.googlecode.com/svn/maven/repository/</url>
        </repository>
        ...
    </repositories>

    <dependencies>
	<dependency>
            <groupId>com.googlecode</groupId>
	    <artifactId>runtimedao</artifactId>
	    <version>1.0.0</version>
	    <scope>compile</scope>
	</dependency>
        ...
    </dependencies>
...
</project>

2. Add a persistence.xml in META-INF directory. Runtimedao uses by default the persistence unit named "runtimeDAO". For example:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">

<persistence-unit name="runtimeDAO">
    <properties>
	<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
	<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
	<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test" />
	<property name="hibernate.hbm2ddl.auto" value="create-drop" />
	<property name="hibernate.connection.username" value="postgres" />
	<property name="hibernate.connection.password" value="postgres" />
    </properties>
</persistence-unit>

</persistence>

Also, you can see a maven sample project -> http://code.google.com/p/runtimedao/source/browse/#svn%2Fbranches%2Frelease-1.0.0%2Fsampleproject

How to use runtimedao in a "normal" project?

1. Download http://code.google.com/p/runtimedao/source/browse/maven/repository/com/googlecode/runtimedao/1.0.0/runtimedao-1.0.0.jar

2. Add runtimedao library to project's classpath

How to build runtimedao

1. svn checkout http://runtimedao.googlecode.com/svn/trunk/runtimedao runtimedao-read-only

2. Configure runtimedao/src/test/resources/META-INF/persistence.xml

3. mvn clean install

Powered by Google Project Hosting