Daily Archives: July 29, 2011

genericDAO

in my jpa project i needed DAOs… Data Access Objects… and the concept to implement those should be generic… initially this concept is confusing as hell… but after that its kinda cool 🙂

Each Entity needs an DAO and for every entity there are 6 object involved:

  • interfcae GenericDao<T, K>: the main Interface with the basic DAO functions (CRUD(create, read, update, delete))
  • class GenericDaoImpl<T, K>: implements the GenericDao interface.
  • interface <entity>Dao: interface of the entity DAO, implements the GenericDao<<entityType>, <entityPrimaryKeyType>>
  • class <entity>DaoImpl: implementation of the entity Dao. it is extended by the GenericDaoImpl
  • class <entity>: the entity class
  • class <entity>DaoTest: testing rocks 🙂

confused? ok heres the example: partly in pseudo code…

Basic things:
public interface GenericDao<T, K extends Serializable>{
T findById(K id, boolean lock);
List<T> findAll();
T save(T entity);
void delete(T entity);
}

public abstract class GenericDaoImpl<T, K extends Serializable> implements GenericDao<T, K>

public Person{
  int id;
  string name
}

Person Dao:
public interface PersionDao extends GenericDao<Persion, Integer>

public class PersonDaoImpl extends GenericDaoImpl<Person, Integer> implements PersionDao

And thats it… in the GenericDaoImpl are all the methods for all entites and in the PersonDaoImpl the persion stuff…

spring

in my project there are a lot of technologies involved and as a .net-developer everything is new… yeah… its java again 🙂 ok jpa, mule, spring, maven, pojos… and i finally understood the main concept of spring :)… or lets say the part with beans… ok i didnt understand, when (timewise) springbeans ware created… but i think this is kinda less important… ok its starts to be important, when something goes wrong, but can do…

 <bean id=”[objectId]” class=”[className]”>
<constructor-arg>
<value>[value]</value>
</constructor-arg>
</bean>

With this declaration an object of typ className will be generated with the name objectId. The constructor is filled with set values… the big question is: where the hell is this object? Actually i dont know, but it can be get in java with:

@Autowired
[className] [objectId];

done.

eclipselink with spring

we decided to use jpa with eclipselink… i actually think this was a good decition, but there are a few problems, when using it with spring… eclipselink needs a LoadTimeWeaver… which can only be loaded by spring, when run with an spring agent… add following argumentline to the VM arguments when run…

-javaagent:${where.ever.it.is}/spring-agent-2.5.6.jar

so unit test need this command… the question, how it will work with maven is completely different… and with mule its an other story…