1

Hibernate can be used to generate all DAO's and hibernate properties files using reverse engineering. But the DAO code it generates is not appropriate in terms of spring dependency injection.

So, how can we use hibernate reverse engineering technique to generate spring beans based on principle of dependency injection?

Rohit Jain
  • 159
  • 1
  • 4
  • 12
  • Do you mean creating DAOs or Entities, and how many of them? – Ralph Feb 20 '12 at 10:59
  • Yes, creating DAO. Basically when we create DAO's using hibernate it creates "SessionFactory" object in all DAO's. Where as when using it with spring we want to it follow spring dependency injection and inject object(instead of creating one) of "SessionFactory" to all DAO's. – Rohit Jain Feb 20 '12 at 11:37

1 Answers1

2

You should use a generic DAO, where you only need to create a Subclass that specify the entity class. Or a more modern approach like Spring Data JPA or its predecessor Hades. Then the concrete dao in noting more than an empty class (or in case of Spring Data JPA/Hades an empty interface).

So it is not worth spending time in searching and addapting a generator approach for the DAOs. You can write more than sixty of them in one hour by hand (if you have a generic DAO)

I have not so much time to search for an example of an Generic DAO, so this is the best I found: http://www.codeproject.com/Articles/251166/The-Generic-DAO-pattern-in-Java-with-Spring-3-and it has at least one "mistake" the concreate DAO should have the annotation @Repository but not @Component. -- Anyway the example illustrate what I mean by generic DAO.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • I got your idea. Thanx for that. If possible can you provide me with small code snippet showing what you exactly mean. That would give me a better understanding. Thanx for your help :) – Rohit Jain Feb 20 '12 at 11:51
  • 1
    @Rohit Jain: you noticed the 3 links? – Ralph Feb 20 '12 at 11:59
  • Yes i am looking through them. Thanx for your help. So its better to write your own DAO's rather than depending upon Hibernate to do so. And a very good way to do it is using Generic-DAO pattern that you just mentioned above. Am i correct ?? – Rohit Jain Feb 20 '12 at 12:05
  • The idea is not to spend time with some strange source code generator if you will have an other way that is more natural (to java) and need less time. – Ralph Feb 20 '12 at 12:11
  • got your idea. Thanx for suggestions and help. :-) – Rohit Jain Feb 20 '12 at 12:13