2

I've generic DAO:

@NoRepositoryBean
interface ICrudDao<T, ID extends Serializable> extends Repository<T, ID> {
    void delete(T deleted);

    List<T> findAll();

    T findOne(ID id);

    T save(T persisted);
}

To allow services to work on that I have to create interface that allows custom entities get persistence, f.e.:

interface TodoDao extends ICrudDao<Todo, Long> {

}

I've a lot of daos like TodoDao. Then don't deliver any special methods.

Creating a lot of empty interfaces seems a dumb idea. How can create a Generic one?

masterdany88
  • 5,041
  • 11
  • 58
  • 132

3 Answers3

2

Edit: I don't think what you are trying to do is a good idea. At first to register a repository for each Entity seems like boiler plate code, but as the application grows, it will help you to maintain it. Imagine your application to evolve over time like this:

  1. You create a simple entity Person and the Interface PersonRepository. Luckily all basic CRUD operations are included, so far it fits your needs so there is nothing else to do.
  2. As your application grows, Person gets a lot of associated relations, like Address, Job, Hobbies and it would be very inefficient to fetch all associated data everytime you access it, because not always every association is needed. To encounter that, you create your own method in PersonRepository which executes your own NamedQuery to only load certain fields and store it in your DTO needed for the specific view ("SELECT new package.PersonDto(x,y) FROM PERSON WHERE ...).
  3. As time passes by, you find yourself in a situation where you need queries to get executed in dynamic fashion, like pagination or restrictions that only need to be added on certain conditions. So you create a new interface PersonCustomRepository and PersonCustomRepositoryImpl where you write queries in a programatic way:

    @PersistenceContext
    private EntityManager entityManager;
    
    @Transactional
    public List<Person> foo() {
       // example for accessing hibernate directly, you could also use QueryDSL and so on
       Criteria basicCriteria = entityManager.unwrap().createCriteria(Person.class);
       if (someCondition) {
          criteria.add(Restrictions.eq("foo", foo));
          ...
       }
       ...
       return criteria.list();
    }
    

Bottom line: Spring data repositories already do a lot of work for you and they are easy to extend, don't try to fight your framework, even it maybe saves you some clicks in the first place.

Journeycorner
  • 2,474
  • 3
  • 19
  • 43
1

You can avoid this by making your entities generic.

//you can annotated with @MappedSuperclass
public class BaseBean{
 //you can specify the id here
}

public class Todo extends BaseBean {

}

@NoRepositoryBean
interface ICrudDao<T exntends BaseBean, ID extends Serializable> extends Repository<T, ID> {
    void delete(T deleted);

    List<T> findAll();

    T findOne(ID id);

    T save(T persisted);
}
SEY_91
  • 1,615
  • 15
  • 26
0

I don't think it's possible. See How to create a Generic DAO class using Hibernate Context sessions and Hibernate: CRUD Generic DAO these might help. I can also think of the Hibernate Session as an example of a single class that deals with the persistence of all types of objects, it just deals with Object type.

Community
  • 1
  • 1
Dhananjay
  • 544
  • 3
  • 7