How do I make a DAO standard abstract java class to inherit in dao classes?

Asked

Viewed 719 times

1

I want to make an Abstract Class in Java to use in the Dao classes that I will use in several projects, not to repeat the methods of selection, insertion, deletion and update in all dao classes.

I didn’t want to use Ibernate and others.

Grateful

  • You want to create a Template to reuse methods from an Abstract class for CRUD operations - Create, Read, Update, Delete? And then don’t have to have these methods rewritten in your DAO classes? Are you using a relational database? What’s the problem with having to implement these methods? And what’s the problem with using JPA?

  • Take a look in this Tutorial’s point topic. it explains well what you are looking for:

  • Use Relational Database, I have no problem, but I wanted to develop a own for specific methods because I will not use inheritance but relational tables. rs

  • 2

    Tiago, it’s still not clear to me what its purpose is. Don’t need to write methods with select, update? Or do you want to learn how DAO works? If you want a layer abstraction, it seems to me that you want to build a small API to abstract CRUD operations, but you will end up having to map the entities. Explain your purpose better.

  • is not to write DAO methods in all classes, for me to make my standard of consultations

  • A few years ago, I had a Genericdao that was an abstract class with exactly those characteristics, and it seemed like a good idea. In the end I ended up abandoning this, because I had a strong tendency to create more problems than to solve.

  • Eh like this Genericdao msm Victor

Show 2 more comments

1 answer

2


Apparently you’d like to implement a Crudrepository which is part of Spring Data, but is used as a strategy for persistent entities (JPA).

Since you don’t want to use any JPA files, we go to an alternative:

The Spring Data JDBC Generic DAO implementation which seeks a light and simple Generic approach to RDBMS, was based on the Jdbctemplate (Spring Framework).

It delivers the full implementation of Spring Pagingandsortingrepository abstraction, without using JPA, XML.

public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
             T  save(T entity);
    Iterable<T> save(Iterable<? extends T> entities);
             T  findOne(ID id);
        boolean exists(ID id);
    Iterable<T> findAll();
           long count();
           void delete(ID id);
           void delete(T entity);
           void delete(Iterable<? extends T> entities);
           void deleteAll();
    Iterable<T> findAll(Sort sort);
        Page<T> findAll(Pageable pageable);
    Iterable<T> findAll(Iterable<ID> ids);
}

Below is a usage reference:

Page<User> page = userRepository.findAll(
    new PageRequest(
        5, 10, 
        new Sort(
            new Order(DESC, "reputation"), 
            new Order(ASC, "user_name")
        )
    )
);

And a great subject with api creator Tomasz Nurkiewicz.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.